BluetoothLE

Hi team,
Just wondering if anyone has a working example for sending data to a characteristic using the Monkeybread WindowsBluetoothLE classes?

I’ve managed to setup and read data from an endpoint, but can’t seem to get the syntax right for sending data.

for sending data to a characteristic

to what kind of device/software?

does your recipient need a special format? you need to prepare it?
basically data are just bytes but you can wrap all content in a struct as example json,xml.
you need to know what the other side will see.
high transfer rate can affect data loss.

https://www.monkeybreadsoftware.net/pluginpart-windowsbluetoothle.shtml

https://www.monkeybreadsoftware.net/example-bluetooth-windowsbluetooth-windowsbluetoothsocket.shtml

Hi Markus,

Thanks for the question. I’m actually stuck at the fundamentals, I’ve figured out how to read, but can’t seem to figure out how to address the GATT characteristics and actually call the write method.

Ie, I assume it’s something like this:
devices(0).GattServices(0).Characteristics(0).writevalue(MB)

but you know what they say about assumptions…

Ie, it’s not that it’s not actually working, I just don’t know how to even try to send data. My device is a custom microcontroller and I’m just sending a few bytes from a memoryblock for now.

As this is MBS-specific, the topic would be better placed in the “Add-Ons” forum section.

It doesn’t look like MBS provides an example program for sending, but from the docs

Thanks Julia,

More than happy if an admin wants to pop it in the Add-ons section. I guess my question might be a little generic in that I’m trying to figure out how to call those methods… Ie, what the code looks like to reference the plugin in the most basic form.

Well, this is a multi step process with async events.
Since you can read, I assume you got the WindowsGattCharacteristicMBS object. Then maybe query the WindowsGattDescriptorMBS like in the BluetoothLE Device example.

And it is probably important to keep references to the objects over time. Like if you call WriteValueAsync and the object is destroyed at the end of the method, the async process could get cancelled.

As said, I wrote this with a few customers together and I’ll ask them for an example.

1 Like

Thanks @Christian_Schmitz, following with interest :slight_smile:

i googled GattCharacteristic

“A GATT characteristic is a basic data element used to construct a GATT service”

yes it seems like a struct with a property name which contains a value.

it means your microcontroller collect/measure data and have a software running which provide this kind of data.

i have only arduino boards and i send data over bt serial as plain text back and forth via key value.
no experience with this GattCharacteristic.

So you got the WindowsGattDeviceServiceMBS object, either by picking from the array you got via GetGattServicesCompleted or IncludedServicesCompleted events.

Then you would query the characteristics, e.g.

me.GetCharacteristicsAsync(Me.CacheModeCached)

once you got CharacteristicsCompleted event, you can make new objects of your WindowsGattCharacteristicMBS subclass to catch events and then sends values:

Sub CharacteristicsCompleted(asyncStatus as Integer, Result as WindowsGattCharacteristicsResultMBS) Handles CharacteristicsCompleted
  Log CurrentMethodName
  
  If result <> Nil Then
    Dim theCharacteristics() As WindowsGattCharacteristicMBS = Result.Characteristics
    
    For Each theCharacteristic As WindowsGattCharacteristicMBS In theCharacteristics
      
      Dim Characteristic As New GattCharacteristic(theCharacteristic)
      Log "Characteristic: "+Characteristic.UserDescription+" "+Characteristic.UUID
      Characteristics.append Characteristic
      Characteristic.GetDescriptorsAsync
    Next
    
    
    Characteristics(1).WriteClientCharacteristicConfigurationDescriptorAsync(1)
    
    Var mb As New MemoryBlock(6)
    mb.Byte(0) = &H01 // Data Byte
    mb.Byte(1) = &HAA
    mb.Byte(2) = &H55
    mb.Byte(3) = &HAA
    mb.Byte(4) = &H55
    mb.Byte(5) = &HAA
    
    Characteristics(0).WriteValueAsync(mb, GattCharacteristic.WriteWithResponse)
    
  Else
    Log "Failed to get characteristics."
  End If
End Sub

For this example there are two characteristics. First it sets configuration descriptor to 1, which I think is WriteWithoutResponse.
Then it assembles a data package with 6 bytes and writes it.
Later the WriteValueAsyncCompleted event is called.

1 Like

Many thanks Christian, If anyone has any issues in the future, please reach out as I’ve got a basic system working

1 Like

some useful information about GATT for future use or others.

James, I am starting a new LE project and if you have sample code to share now for characteristic read and write it sounds like that would jump start my work. Thanks. David

Sure! I’m away atm but will post my code when I get back?

1 Like

Cool, that’d be very helpful.

Hi James,
Thanks for offering to share your BLE code. If you could post that it would be very helpful I am sure.
David

1 Like