I wrote a simple application to be able to write to specific characteristic. I based my app on google example - https://github.com/googlesamples/android-BluetoothLeGatt . I added buttons that upon connection gives possibility to write to specific characteristic a byte.
Now what I noticed is that after connection for few seconds (always less than 5) it works fine, but then function writeCharacteristic ( https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#writeCharacteristic(android.bluetooth.BluetoothGattCharacteristic) ) starts to return false. I debugged and it turns out that device is busy. I am able to successfully call writeCharacteristic every 1.5 second which compared to no delay in first few seconds of connection is very slow.
Here is my snippet with onClick function:
public void onClick(View v) {
byte value[] = {0};
switch (v.getId()) {
case R.id.button1:
value[0] = 1;
mBulbCharacteristic.setValue(value);
mBluetoothLeService.writeCharacteristic(mBulbCharacteristic);
break;
case R.id.button2:
value[0] = 2;
mBulbCharacteristic.setValue(value);
mBluetoothLeService.writeCharacteristic(mBulbCharacteristic);
break;
case R.id.button3:
value[0] = 3;
mBulbCharacteristic.setValue(value);
mBluetoothLeService.writeCharacteristic(mBulbCharacteristic);
break;
default:
break;
}
}
That the device is "busy" does just mean that a response is pending. Android's API requires that you wait for the corresponding callback (like onCharacteristicWrite for writes) after you issue a new request. If you think it takes too much time you can lower the connection interval.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.