尝试在 android 上启用加速度计时,keyfob cc2541 蓝牙连接断开

keyfob cc2541 bluetooth connection drops when trying to enable acclerometer on android

我正在开发一个 Android 应用程序,它需要读取 Keyfob 的加速度计数据。到目前为止,我一直在学习本教程:https://thenewcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial

有了它,我能够连接到 Keyfob,搜索服务并阅读一些特征。问题是当我尝试启用钥匙扣的加速度计时,蓝牙连接就掉线了。

这是我用来尝试启用加速度计的代码:

private void enableAccelerometer(BluetoothGatt gatt){
        BluetoothGattCharacteristic characteristic;
        BluetoothGattService service;
        Log.d(TAG, "ligando acelerometro");
        service = gatt.getService(ACCELEROMETER_SERVICE);
        if(service == null){
            Log.d(TAG, "Not able to find the service");
        }
        else{
            Log.d(TAG, "Service found");
            characteristic = service.getCharacteristic(ENABLE_ACCELEROMETER);
            if(characteristic == null){
                Log.d(TAG, "Characteristic not found");
            }
            else{
                characteristic.setValue(new byte[] {0x01});
                if (!gatt.writeCharacteristic(characteristic)){
                    Log.d(TAG, "writing failed ");
                }
                else {
                    Log.d(TAG, "writing successful:  ");
                }
            }
        }

此方法在"onServicesDiscovered"回调函数中调用。

The Texas Instrument CC2540/41 Mini Development Kit User's Guide 指出要启用加速度计,必须在加速度计服务的启用加速度计特性中写入“01”,这就是我使用此代码所做的.

phone (LG G2 mini 运行 Android 4.4.2) 之间的连接在我写的时候掉线了:

  characteristic.setValue(new byte[] {0x01});

我确定是这一行导致连接断开,如果我将其注释掉或只是尝试写入字符串而不是字节,连接不会断开。

有谁知道我做错了什么吗?

事实证明,一周后我找到了打开加速度计的方法。我仍然不知道为什么它只能那样工作,但我只是改变了:

characteristic.setValue(new byte[] {0x01});

characteristic.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

我不知道 "BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE" 常量中到底有什么值,但它起作用了。