【发布时间】:2019-04-08 21:10:41
【问题描述】:
在我的 UWP 应用中,我想读取其他 BLE 设备的设备名称。所以我试图从设备中读取this 特征。我可以找到设备的广告 UUID 和蓝牙地址,但我无法从中找到默认的 GATT 服务。这是我获取服务的代码:
if (ulong.TryParse(deviceAddress, out ulong address))
{
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(address);
var genericAccessId = ConvertFromInteger(0x1800);
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesForUuidAsync(genericAccessId);
if (result?.Status == GattCommunicationStatus.Success)
{
var genericAccess = result.Services.FirstOrDefault(s => s.Uuid == genericAccessId);
// genericAccess is always null
if (genericAccess != null)
{
var deviceNameId = ConvertFromInteger(0x2A00);
var deviceName = await genericAccess.GetCharacteristicsForUuidAsync(deviceNameId);
if (deviceName?.Status == GattCommunicationStatus.Success)
{
var c = deviceName.Characteristics.FirstOrDefault(x => x.Uuid == deviceNameId);
if (c != null)
{
var v = await c.ReadValueAsync();
if (v?.Status == GattCommunicationStatus.Success)
{
var reader = DataReader.FromBuffer(v.Value);
byte[] input = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(input);
// Utilize the data as needed
string str = System.Text.Encoding.Default.GetString(input);
Log?.Invoke(str);
}
}
}
}
}
}
public static Guid ConvertFromInteger(int i)
{
byte[] bytes = new byte[16];
BitConverter.GetBytes(i).CopyTo(bytes, 0);
return new Guid(bytes);
}
Any idea where the problem is?
【问题讨论】:
-
我找不到是什么意思?是否发生错误?或者执行在哪里跳过其余代码?
-
方法
ConvertFromInteger很可能不正确。请改用GattDeviceService.ConvertShortIdToUuid,例如GattDeviceService.ConvertShortIdToUuid(0x1800). -
就是这样。尽管有人建议我改用
BluetoothUuidHelper.FromShortId,因为GattDeviceService.ConvertShortIdToUuid已过时。如果您将其发布为答案,我会接受。