Created
June 14, 2020 00:47
-
-
Save dlech/6a063b67b52c74f88f5d7f59c2b64b76 to your computer and use it in GitHub Desktop.
Pybricks BLE UART C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Windows.Devices.Bluetooth; | |
using Windows.Devices.Bluetooth.GenericAttributeProfile; | |
using Windows.Devices.Bluetooth.Advertisement; | |
using Windows.Security.Cryptography; | |
using Windows.Storage.Streams; | |
using Buffer = Windows.Storage.Streams.Buffer; | |
using System.IO; | |
using System.Runtime.InteropServices.WindowsRuntime; | |
namespace BluetoothTest | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var watcher = new BluetoothLEAdvertisementWatcher(); | |
watcher.Received += (s, e) => | |
{ | |
watcher.Stop(); | |
Console.WriteLine("found device {0} {1}", e.Advertisement.LocalName, e.BluetoothAddress); | |
Task.Run(async () => | |
{ | |
Console.WriteLine("task"); | |
try | |
{ | |
var device = await BluetoothLEDevice.FromBluetoothAddressAsync(e.BluetoothAddress); | |
var services = await device.GetGattServicesAsync(BluetoothCacheMode.Uncached); | |
var service = services.Services.Single(x => x.Uuid == Guid.Parse("6e400001-b5a3-f393-e0a9-e50e24dcca9e")); | |
var characteristics = await service.GetCharacteristicsAsync(); | |
var tx = characteristics.Characteristics.Single(x => x.Uuid == Guid.Parse("6e400003-b5a3-f393-e0a9-e50e24dcca9e")); | |
tx.ValueChanged += (s2, e2) => | |
{ | |
var value = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, e2.CharacteristicValue); | |
Console.WriteLine(value); | |
}; | |
var result = tx.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); | |
Console.WriteLine(result); | |
var rx = characteristics.Characteristics.Single(x => x.Uuid == Guid.Parse("6e400002-b5a3-f393-e0a9-e50e24dcca9e")); | |
var value = new Buffer(4) as IBuffer; | |
var writer = new DataWriter(value.AsStream().AsOutputStream()) { ByteOrder = ByteOrder.LittleEndian }; | |
writer.WriteUInt32(4); | |
var writeResult = await rx.WriteValueWithResultAsync(value, GattWriteOption.WriteWithoutResponse); | |
value = CryptographicBuffer.ConvertStringToBinary(" ", BinaryStringEncoding.Utf8); | |
writeResult = await rx.WriteValueWithResultAsync(value, GattWriteOption.WriteWithoutResponse); | |
value = CryptographicBuffer.ConvertStringToBinary("1234567890123456789012345678901234567890", BinaryStringEncoding.Utf8); | |
writeResult = await rx.WriteValueWithResultAsync(value, GattWriteOption.WriteWithoutResponse); | |
Console.WriteLine(writeResult.Status); | |
} catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
}); | |
}; | |
watcher.Start(); | |
await Task.Delay(-1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment