8

In linux we have the tool gatttool to query bluetooth low energy devices. If we run gatttool --device=[MAC] --characteristics we get a list of characteristics that the corresponding device has. Example output:

handle = 0x0002, char properties = 0x02, char value handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, char properties = 0x02, char value handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, char properties = 0x0a, char value handle = 0x0007, uuid = 00002a02-0000-1000-8000-00805f9b34fb
handle = 0x0008, char properties = 0x02, char value handle = 0x0009, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000d, char properties = 0x22, char value handle = 0x000e, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0011, char properties = 0x1a, char value handle = 0x0012, uuid = 00000001-0000-1000-8000-00805f9b34fb
handle = 0x0014, char properties = 0x02, char value handle = 0x0015, uuid = 00000002-0000-1000-8000-00805f9b34fb
handle = 0x0016, char properties = 0x12, char value handle = 0x0017, uuid = 00000004-0000-1000-8000-00805f9b34fb
handle = 0x0018, char properties = 0x08, char value handle = 0x0019, uuid = 00000007-0000-1000-8000-00805f9b34fb
handle = 0x001a, char properties = 0x08, char value handle = 0x001b, uuid = 00000010-0000-1000-8000-00805f9b34fb
handle = 0x001c, char properties = 0x0a, char value handle = 0x001d, uuid = 00000013-0000-1000-8000-00805f9b34fb
handle = 0x001e, char properties = 0x02, char value handle = 0x001f, uuid = 00000014-0000-1000-8000-00805f9b34fb
handle = 0x0020, char properties = 0x10, char value handle = 0x0021, uuid = 00001001-0000-1000-8000-00805f9b34fb
handle = 0x0024, char properties = 0x0a, char value handle = 0x0025, uuid = 8082caa8-41a6-4021-91c6-56f9b954cc34
handle = 0x0026, char properties = 0x0a, char value handle = 0x0027, uuid = 724249f0-5ec3-4b5f-8804-42345af08651
handle = 0x0028, char properties = 0x02, char value handle = 0x0029, uuid = 6c53db25-47a1-45fe-a022-7c92fb334fd4
handle = 0x002a, char properties = 0x0a, char value handle = 0x002b, uuid = 9d84b9a3-000c-49d8-9183-855b673fda31
handle = 0x002c, char properties = 0x0e, char value handle = 0x002d, uuid = 457871e8-d516-4ca1-9116-57d0b17b9cb2
handle = 0x002e, char properties = 0x12, char value handle = 0x002f, uuid = 5f78df94-798c-46f5-990a-b3eb6a065c88
handle = 0x0032, char properties = 0x0a, char value handle = 0x0033, uuid = 00001a00-0000-1000-8000-00805f9b34fb
handle = 0x0034, char properties = 0x1a, char value handle = 0x0035, uuid = 00001a01-0000-1000-8000-00805f9b34fb
handle = 0x0037, char properties = 0x02, char value handle = 0x0038, uuid = 00001a02-0000-1000-8000-00805f9b34fb
handle = 0x003b, char properties = 0x02, char value handle = 0x003c, uuid = 00001a11-0000-1000-8000-00805f9b34fb
handle = 0x003d, char properties = 0x1a, char value handle = 0x003e, uuid = 00001a10-0000-1000-8000-00805f9b34fb
handle = 0x0040, char properties = 0x02, char value handle = 0x0041, uuid = 00001a12-0000-1000-8000-00805f9b34fb

Under windows i have been able to get handle, char properties and uuid with the following code

IEnumerable<GattCharacteristic> characteristics = device.GattServices.
                                                         SelectMany(s => s.GetAllCharacteristics());
foreach (GattCharacteristic characteristic in characteristics)
{
    Console.WriteLine($"handle = 0x{characteristic.AttributeHandle:x4}, char properties = 0x????, uuid = {characteristic.Uuid}");
}

But how do i get char value handle, the only pattern i have found for char value handle is characteristic.AttributeHandle + 1 but i only have one device to test with, so i don't know if that always is the case or just with this specific device.

So how do get the char value handle under windows?

Peter
  • 37,042
  • 39
  • 142
  • 198
  • I think i found the char value handle in the c++ api https://msdn.microsoft.com/en-us/library/windows/hardware/hh450840(v=vs.85).aspx CharacteristicValueHandle now its just a question on how to get it in c#. – Peter Apr 13 '17 at 06:27
  • If you're fine with UWP its way simpler - see https://github.com/kpathakota/Build2016BluetoothCodeSamples/tree/master/BluetoothInAppGATT/cs – Ondrej Svejdar Apr 19 '17 at 08:45
  • @OndrejSvejdar exactly where in that example does it get the `char value handle`? – Peter Apr 19 '17 at 15:42
  • 1
    I made an initial translation (untested), maybe this will help you: https://pastebin.com/Jvc2Dn75 – Essigwurst Apr 21 '17 at 05:45
  • 2
    Encapsulation is getting in the way of making it look *exactly* the same. In general strongly pursued in UWP object models. You only need the characteristic value handle in order to read or write the value. But that is already possible without providing the handle, the only thing you need to bring to make GattCharacteristic.Read/WriteValueAsync() work is a buffer. – Hans Passant Apr 21 '17 at 09:23
  • @Essigwurst that could work the question is just how do i get the btDevice handle? – Peter Apr 22 '17 at 23:38
  • I will try to provide a working example the net hours. The first link in the comment may do it. – Essigwurst Apr 23 '17 at 08:00
  • I'm not sure about Line 13, but since im on a Windows 7 machine at the moment i can't test the code. I hope this will help: https://pastebin.com/vLHDY9Yy Maybe you can get the path from sysinternal tools? – Essigwurst Apr 23 '17 at 10:42
  • @Essigwurst thanks for your continued support, but im still a bit confused where do i find the string you are hardcoding on line 13? i tried to get the DeviceId from `GattDeviceService` but i only get a few `characteristics` and none of them seem to match the ones im expecting.. – Peter May 05 '17 at 12:09

0 Answers0