Не могу найти сервис ГАТТ - PullRequest
0 голосов
/ 09 апреля 2019

В моем приложении UWP я хочу прочитать имя устройства других устройств BLE. Поэтому я пытаюсь прочитать эту характеристику с устройств. Я могу найти рекламный UUID и адрес Bluetooth устройства, но не могу найти из него стандартные сервисы 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?

1 Ответ

1 голос
/ 09 апреля 2019

Устройство BLE, службы и характеристики имеют 128-битный UUID для идентификации.Для стандартизированных услуг и характеристик также существует 16-битная короткая версия, например, 0x1800 для Универсальный доступ .

Для преобразования 16-битного в 128-битный UUID, 16-битные значениядолжен быть заполнен следующим UUID в байтах 2 и 3 (в порядке байтов:

0000xxxx-0000-1000-8000-00805F9B34FB

Так что 0x1800 преобразуется в:

00000018-0000-1000-8000-00805F9B34FB

В Windows есть функция, которая делает этодля вас: BluetoothUuidHelper.FromShortId

var uuid = BluetoothUuidHelper.FromShortId(0x1800);

В предыдущей версии Windows вместо этого вы использовали бы GattDeviceService.ConvertShortIdToUuid .

Поэтому замените свою функциюConvertFromInteger с указанным выше. Ваша функция заполняет все 0 вместо вышеуказанного значения UUID.

...