问题:onServicesDiscovered never called while connecting to GATT Server
Something that has been really useful for me is to wait for about 600ms after the connection has been established and then start the service discovery.
项目中出现蓝牙连接上之后,始终不进onServicesDiscovered 回调,mBluetoothGatt.discoverServices()做如下延时即可
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
//有时候发现服务不回调,需延时 https://stackoverflow.com/questions/41434555/onservicesdiscovered-never-called-while-connecting-to-gatt-server#comment70285228_41526267
try {
Thread.sleep(600);
Log.i(TAG, "Attempting to start service discovery:"
+ mBluetoothGatt.discoverServices());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}