While connecting to BLE113 from android 4.3 is logging “Client registered, waiting for callback”

北战南征 提交于 2019-12-06 13:44:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!