Martin
Martin

Reputation: 2873

How to write characteristic fast and stable for BLE in Android?

I am developing for BLE in Android. The Android can send data to BLE device by Gatt.writeCharacteristic.

But When I write consecutive Characteristic to BLE device (5 times in 1 Second) , I will get GATT_DISCONNECTED and it return null when I try to getService and GetCharacteristic.

I have seen some BLE LED product in market. It use a color picker in Android like the following picture:

enter image description here

When user change the color fast on the color picker , the BLE LED also change the color immediately.

How to write characteristic fast and stable for BLE in Android like the BLE LED product in market ?

Upvotes: 3

Views: 4215

Answers (1)

Rob Gorman
Rob Gorman

Reputation: 3694

When you call gatt.writeCharacteristic you need to do 2 things:

  1. You need to queue up the gatt read and write requests so that when you make a read or write request, you must wait for the corresponding callback in BluetoothGattCallback before requesting the next read or write. This is an Android BluetoothGatt behavior that is not well documented.
  2. Somewhat related to 1. above, you should check the boolean return value of the gatt read and write requests. If it does not return true, then your request failed. This occurs when some other gatt operation is in still in progress at the time you make the next request.

I have found that the best way to read (or write) BLE characteristics fast is to make the request, wait for the callback and make the next request from the callback. If there is a faster or more reliable way, I am unaware of it.

Upvotes: 9

Related Questions