I am new to android BLE development and to BLE development in general and I noticed that most of the time, Android might take 3-7 seconds before the call to onConnectionStateChange gets triggered after my connectGatt call. Is this normal? I am curious because I worked with Bluetooth 2.0 previously and everything was much faster there. Also, I did some test coding on iPhone and the establishment of initial connection was much faster there as well. I will post a sample code below along with the few android monitor messages indicating where the slowdown occurs.
class BluetoothConnection{
void connectToDevice(BluetoothDevice device) {
if (mBluetoothGatt == null) {
Log.d(TAG, "-----Trying to connect to GATT server.");
mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback);
}
}
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.d(TAG, "-----Connected to GATT server.");
Log.d(TAG, "-----Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.d(TAG, "-----Disconnected from GATT server.");
}
}
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.w(TAG, "-----Successfully discovered the services");
BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb"));
Log.d(TAG, "-- Service = " + gattService.getUuid());
characteristicsTxRx = gattService.getCharacteristic(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));
Log.d(TAG, "-- Characteristic = " + characteristicsTxRx.getUuid());
mBluetoothGatt.setCharacteristicNotification(characteristicsTxRx, true);
Intent intent = new Intent(CONNECTION_ESTABLISHED);
mContext.sendBroadcast(intent);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
Intent intent = new Intent(DATA_AVAILABLE);
String incomingMessage = new String(data);
intent.putExtra(DATA, incomingMessage);
mContext.sendBroadcast(intent);
}
}
};
}
And here are couple lines from my log, as you can see it took almost 5 seconds here to get confirmation that we are connected to GATT.
09-07 10:07:05.211 29907-29907/com.bubblewall.saik.bubblewall D/bubbleWallMessage: -----Trying to connect to GATT server.
09-07 10:07:09.898 29907-29920/com.bubblewall.saik.bubblewall D/bubbleWallMessage: -----Connected to GATT server.
09-07 10:07:09.901 29907-29920/com.bubblewall.saik.bubblewall D/bubbleWallMessage: -----Attempting to start service discovery:true
09-07 10:07:10.347 29907-29920/com.bubblewall.saik.bubblewall W/bubbleWallMessage: -----Successfully discovered the services
For anyone who might be interested in the solution that worked for me. I could not find any Android documentation that suggested any particular advertising intervals, but the IOs documentation suggested to use an interval of 152.5 ms. After setting my interval to this, my app started discovering and connecting much faster to the Bluetooth. Thank you, Emil, for pointing me in the right direction.
Here is the IOs documentation page that I mentioned above. https://developer.apple.com/library/content/qa/qa1931/_index.html