Skip to content

Instantly share code, notes, and snippets.

@gebogebogebo
Last active September 2, 2020 01:07
Show Gist options
  • Save gebogebogebo/11540f69b5301ee8b92976651f9894b6 to your computer and use it in GitHub Desktop.
Save gebogebogebo/11540f69b5301ee8b92976651f9894b6 to your computer and use it in GitHub Desktop.
WindowsデスクトップアプリでBLEのGATTで体温計と血圧計と通信する ref: https://qiita.com/gebo/items/41da7474936845d77d06
byte[] c1 = data.Skip(1).Take(2).ToArray();
var val = Common.ConvertToFloat(c1, Common.ConvType.IEEE_11073_16bit_float);
Console.WriteLine($"Blood Pressure Measurement Compound Value - Systolic(最高血圧) = {val}mmHg");
// DeviceInformation.FindAllAsync()でDeviceInformationCollectionクラス(リスト)をGETする
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("00001809-0000-1000-8000-00805f9b34fb")));
//GattDeviceService.FromIdAsync()でGattDeviceServiceクラスを取得する
GattDeviceService service = await GattDeviceService.FromIdAsync(devices.First().Id);
// service使って処理する
・・・
public async void Start()
{
// アドバタイズパケットの受信開始
this.advWatcher = new BluetoothLEAdvertisementWatcher();
this.advWatcher.Start();
}
private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
// アドバタイズパケット受信→Health Thermometerサービスを検索
var bleServiceUUIDs = args.Advertisement.ServiceUuids;
foreach (var uuidone in bleServiceUUIDs) {
if (uuidone == new Guid("00001809-0000-1000-8000-00805f9b34fb")) {
// 発見
this.advWatcher.Stop();
BluetoothLEDevice dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
GattDeviceService service = dev.GetGattService(new Guid("00001809-0000-1000-8000-00805f9b34fb"));
// service使って処理する
break;
}
}
}
// Get Service
this.Service = await GattDeviceService.FromIdAsync(devices.First().Id);
var bleServiceUUIDs = args.Advertisement.ServiceUuids;
foreach (var uuidone in bleServiceUUIDs) {
if (uuidone == new Guid("00001809-0000-1000-8000-00805f9b34fb")) {
// 発見
this.advWatcher.Stop();
BluetoothLEDevice dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
this.Service = dev.GetGattService(new Guid("00001809-0000-1000-8000-00805f9b34fb"));
find = true;
break;
}
}
var characteristics = Service.GetCharacteristics(new Guid("00002A1C-0000-1000-8000-00805f9b34fb"));
this.Characteristic_Temperature_Measurement = characteristics.First();
if (this.Characteristic_Temperature_Measurement.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Indicate)) {
this.Characteristic_Temperature_Measurement.ValueChanged += characteristicChanged_Temperature_Measurement;
await this.Characteristic_Temperature_Measurement.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Indicate);
}
byte[] data = new byte[eventArgs.CharacteristicValue.Length];
Windows.Storage.Streams.DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(data);
// Parse
byte[] c1 = data.Skip(1).Take(4).ToArray();
var temperature = Common.ConvertToFloat(c1, Common.ConvType.IEEE_1073_32bit_float);
var characteristics = Service.GetCharacteristics(Common.CreateFullUUID("2A49"));
var chara = characteristics.First();
if (chara.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Read)) {
GattReadResult result = await chara.ReadValueAsync();
var reader = Windows.Storage.Streams.DataReader.FromBuffer(result.Value);
byte[] input = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(input);
...
}
byte[] c1 = data.Skip(1).Take(2).ToArray();
var val = Common.ConvertToFloat(c1, Common.ConvType.IEEE_11073_16bit_float);
Console.WriteLine($"Blood Pressure Measurement Compound Value - Systolic(最高血圧) = {val}mmHg");
private GattDeviceService Service;
public async void Start()
{
// アドバタイズパケットの受信開始
this.advWatcher = new BluetoothLEAdvertisementWatcher();
this.advWatcher.Start();
}
private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
// アドバタイズパケット受信→Health Thermometerサービスを検索
bool find = false;
var bleServiceUUIDs = args.Advertisement.ServiceUuids;
foreach (var uuidone in bleServiceUUIDs) {
if (uuidone == new Guid("00001809-0000-1000-8000-00805f9b34fb")) {
// 発見
this.advWatcher.Stop();
BluetoothLEDevice dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
this.Service = dev.GetGattService(new Guid("00001809-0000-1000-8000-00805f9b34fb"));
find = true;
break;
}
}
if (find) {
// Get Temperature Measurement Characteristic
// Requirement = M , Mandatory Properties = Indicate
{
var characteristics = Service.GetCharacteristics(new Guid("00002A1C-0000-1000-8000-00805f9b34fb"));
this.Characteristic_Temperature_Measurement = characteristics.First();
if (this.Characteristic_Temperature_Measurement.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Indicate)) {
this.Characteristic_Temperature_Measurement.ValueChanged += characteristicChanged_Temperature_Measurement;
await this.Characteristic_Temperature_Measurement.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Indicate);
}
}
// Get Intermediate Temperature Characteristic
// Requirement = O , Mandatory Properties = Notify
{
var characteristics = Service.GetCharacteristics(new Guid("00002A1E-0000-1000-8000-00805f9b34fb"));
this.Characteristic_Intermediate_Temperature = characteristics.First();
if (this.Characteristic_Intermediate_Temperature.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify)) {
this.Characteristic_Intermediate_Temperature.ValueChanged += characteristicChanged_Intermediate_Temperature;
await this.Characteristic_Intermediate_Temperature.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
}
}
}
}
// Temperature_Measurement 体温測定値
private GattCharacteristic Characteristic_Temperature_Measurement;
private void characteristicChanged_Temperature_Measurement(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
{
byte[] data = new byte[eventArgs.CharacteristicValue.Length];
Windows.Storage.Streams.DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(data);
// Parse
byte[] c1 = data.Skip(1).Take(4).ToArray();
var temperature = Common.ConvertToFloat(c1, Common.ConvType.IEEE_11073_32bit_float);
return;
}
// Intermediate_Temperature 体温の変化通知
private GattCharacteristic Characteristic_Intermediate_Temperature;
private void characteristicChanged_Intermediate_Temperature(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
{
byte[] data = new byte[eventArgs.CharacteristicValue.Length];
Windows.Storage.Streams.DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(data);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment