Illuminati0x5B
Illuminati0x5B

Reputation: 602

C# - Bluetooth programming

In my program, I send a command to a device and it sends some data back. Whenever the data is available, the following event handler gets invoked.

private void notify_change(GattCharacteristic sender, GattValueChangedEventArgs args)
{
    lock (this._dataRec)
    {
        notCounter++;
        byte[] bArray = new byte[args.CharacteristicValue.Length];
                DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(bArray);                
                dataQ.Enqueue(Encoding.ASCII.GetString(bArray));
                Monitor.Pulse(this._dataRec);
    }
}

Sometimes, I noticed that this gets called before previous data has been read (or something like that; from the list of commands, data seems to be missing). Looks like the buffer gets overwritten whenever the function is invoked. Is there a way to add data to the buffer rather than overwriting it?

Upvotes: 1

Views: 315

Answers (2)

Darko Djuric
Darko Djuric

Reputation: 754

In my program, I send a command to a device and it sends some data back.

Since you are trigger response by your calls, be sure that you don't make buffer overflow on device side. Minimal theoretical gap between two packets is 7.5ms but in practice it is about 20-30ms. So if you are sending in a loop, your device will lost (overwrite) packets if gap is less than your HW setup can handle.

Upvotes: 1

bare_metal
bare_metal

Reputation: 1194

The GATT protocol has two options to receive unsolicited information. They are notifications and indications. notifications are one with out acknowledgement from the receiver where as indications require an acknowledgment from the receiver. so you probably need indications and if this is not an option you need to ensure that the data is copied before the next notification.

see the following from the Bluetooth specification.enter image description here

enter image description here

Upvotes: 0

Related Questions