在华硕设备上的readCharacteristic之后,BluetoothGatt断开连接

时间:2018-01-24 20:57:16

标签: android bluetooth disconnect

连接 connectGatt readCharacteristic BluetoothGatt断开连接并在ASUS设备上 onConnectionStateChange 。在sony xperia z2断开连接不会发生。我花了很多时间......有没有人有任何想法?

连接

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            mBluetoothGatt = mDevice.connectGatt(ActivityExamination.this, true, mCallback2 , BluetoothDevice.TRANSPORT_LE);
                        } else {
                            mBluetoothGatt = mDevice.connectGatt(ActivityExamination.this, true, mCallback2 );
                        }

和听众

BluetoothGattCallback mCallback2 = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            Log.e("onConnectionStateChange", "STATUS: " + status + " STATE: " + newState);
            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:
                    Log.i("gattCallback", "STATE_CONNECTED");
                    gatt.discoverServices();
                    break;
                case BluetoothProfile.STATE_DISCONNECTED:
                    //status is 8
                    Log.e("gattCallback", "STATE_DISCONNECTED");
                    Log.i("gattCallback", "reconnecting...");
                    gatt.connect();
                    break;
                default:
                    Log.e("gattCallback", "STATE_OTHER");
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.e("onServicesDiscovered", "STATUS: " + status);
            BluetoothGattService mainService = mBluetoothGatt.getService(UUID_SERVICE_MAIN);
            BluetoothGattService deviceInfoService = mBluetoothGatt.getService(UUID_SERVICE_DEVICE_INFO);
            BluetoothGattCharacteristic mainCharacteristic = mainService.getCharacteristic(UUID_MAIN_DATA);
            BluetoothGattCharacteristic modelCharacteristic = deviceInfoService.getCharacteristic(UUID_DEVICE_MODEL);
            mBluetoothGatt.readCharacteristic(modelCharacteristic);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.e("onCharacteristicRead", "STATUS: " + status + " CHAR: " + characteristic.toString());

        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.e("onCharacteristicChanged", "DESCRIPTOR: ");
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            Log.e("onDescriptorWrite", "DESCRIPTOR: " + descriptor.toString());
        }
    };

1 个答案:

答案 0 :(得分:0)

我过去遇到过类似的问题,而且大部分时间都与在实际回调方法中做大量工作有关,如果我记得的话,通常会在Binder线程上调用它。正确。

您是否尝试过遵循与Android tutorials类似的模式?当从蓝牙堆栈接收事件时,它们发送广播事件以通知更新。这会将线程上下文更改为主线程,这将否定阻止绑定线程的任何可能性 - 这可能会解决您的问题。

通常我设置一个服务来处理我的蓝牙LE通信,并创建2个Handler,一个主线程和一个后台线程。后台线程处理程序用于解析事件,另一个用于启动与远程BLE设备的通信,即连接调用,写入,读取等。这有助于猜测您在哪个线程上调用的内容。

希望这有帮助。