在我的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?发布于 2019-04-09 14:11:13
BLE设备、服务和特性具有用于标识的128位UUID。对于标准化的服务和特性,也有16位短版本,例如0x1800用于Generic Access。
为了将16位转换为128位UUID,必须在字节2和3处将16位值填充到以下UUID中(按小端顺序:
0000xxxx-0000-1000-8000-00805F9B34FB因此0x1800被转换为:
00000018-0000-1000-8000-00805F9B34FBWindows有一个函数可以帮你做到这一点:BluetoothUuidHelper.FromShortId
var uuid = BluetoothUuidHelper.FromShortId(0x1800);在以前的Windows版本中,您将使用GattDeviceService.ConvertShortIdToUuid。
所以用上面的函数替换你的ConvertFromInteger函数。您的函数填充全0,而不是上面的UUID值。
https://stackoverflow.com/questions/55581798
复制相似问题