« GraphicsMagick in Fil… | Home | GraphicsMagick in Fil… »

Bluetooth LE on Windows

Connecting to a bluetooth LE device on Windows and reading values is a multi step asynchronous procedure. You have to manage the state in your code to know where you are. You call plenty of functions and later you receive the callbacks to react to them, either as event or delegate method call. Here are the steps you may need to do:

Find device

Use WindowsBluetoothLEAdvertisementWatcherMBS class to watch for devices. You get the Received event call for each device found, so you can inspect the argument with the WindowsBluetoothLEAdvertisementReceivedEventArgsMBS object. Pick the bluetooth address from advertisement, an UInt64.

Open device

Use WindowsBluetoothLEDeviceMBS.FromBluetoothAddressAsync to create the device object from the address. If you alternatively have the Id, you can use FromIdAsync method instead. When your delegate is called later, take the device object and continue.

Request access to device

On the WindowsBluetoothLEDeviceMBS object, call RequestAccessAsync method. You receive a call to RequestAccessCompleted event later. You can decide if you like to subclass it or to use addHandler there.

Check services offered

Call GetGattServicesAsync on the device object. You get a GetGattServicesCompleted event with a WindowsGattDeviceServicesResultMBS object. Look through the list of services and pick the WindowsGattDeviceServiceMBS object you need.

Request access to service

Call RequestAccessAsync on the service object. You receive a RequestAccessCompleted event call later.

Open service

Next you call OpenAsync one the service to open a connection. Then OpenAsyncCompleted event gets called and you can check the status.

Get characteristics

Call GetCharacteristicsAsync to query characteristics. You receive a CharacteristicsCompleted event with the results. Check the result object for the WindowsGattCharacteristicMBS you need.

Get description for characteristics

You call GetDescriptorsAsync on the characteristic object. Later you get the DescriptorsCompleted event to look on the descriptions.

Set configuration descriptor

The client characteristic configuration descriptor can be set to tell whether the device should send notification. Call WriteClientCharacteristicConfigurationDescriptorAsync to set this setting.

Get ValueChanged event

When you use the copy constructor on WindowsGattCharacteristicMBS to get your own subclass using the characteristic of your choice, then we will in that event connect the event handler for ValueChanged. You may for e.g. a heart rate monitor see the ValueChanged event called with new values.

Read & Write value

If you need, you can send WriteValueAsync method to write some bytes to the device. Or use ReadValueAsync to read the current value. You may get matching event calls later when the reading or writing operation finished.

Troubleshooting

At any point in debugging this, the device may turn off, get out of reach or timeout. You may have to try again. The device may also be in use exclusively by some other application, so your application is denied.

16 12 22 - 13:59