问题
I'm trying to connect to a BlueGiga BLE113 device and my Samsung Galaxy S4(Android 4.3). I can successfully discover the device but unable to connect and discover services. This is the log after pushing the button to connect.
12-30 16:38:34.012: D/BluetoothGatt(11280): registerApp()
12-30 16:38:34.012: D/BluetoothGatt(11280): registerApp() - UUID=5a5ac8ad-7583-457f-ba60-373c3beaf1b2
12-30 16:38:34.022: D/BluetoothGatt(11280): onClientRegistered() - status=0 clientIf=8
12-30 16:38:34.022: I/BluetoothGatt(11280): Client registered, waiting for callback
12-30 16:38:34.022: D/BluetoothGatt(11280): onClientConnectionState() - status=0 clientIf=8 device=FF:FF:FF:FF:FF:FF
The callback passed to the connectGatt method is follwing.
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.i(TAG, "Trying to connect...");
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected to GATT server.");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected from GATT server.");
}
}
};
回答1:
This is most likely a threading issue. I ran into a very similar issue with BLE on Samsung Galaxy S4 devices. It appears that Samsung's implementation of Android handles BLE differently than others (Nexus 7 device worked fine). Albeit, you must explicitly run your BLE connectGatt method from the UI thread. Here is an example:
// Create handler for main thread where mContext is application context
mHandler = new Handler(mContext.getMainLooper());
...
// Connect to BLE device from mHandler
mHandler.post(new Runnable() {
@Override
public void run() {
mBTGatt = mBTDevice.connectGatt(mContext, false, mGattCallback);
}
});
来源:https://stackoverflow.com/questions/20839018/while-connecting-to-ble113-from-android-4-3-is-logging-client-registered-waiti