Android-BLE - 将数据写入特性时出现问题

Android-BLE - Problems writing data to Characteristic

我正在尝试将数据写入 BLE 特性,但我在回调中得到状态 130,这意味着 GATT_WRONG_STATE。不幸的是,我不知道什么状态是错误的以及我该如何改变它。

我使用的是 Android-连接示例 (https://github.com/android/connectivity-samples/tree/master/BluetoothLeGatt) 中的 BLE-App,目前有一些小改动。

这是我的 DeviceControlActivity:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            updateConnectionState(R.string.disconnected);
            invalidateOptionsMenu();
            clearUI();
        } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            displayGattServices(mBluetoothLeService.getSupportedGattServices());
        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
            testWriteToCharacteristic();
        }
    }
};

private void testWriteToCharacteristic() {
        Log.d(TAG, "testWriteToCharacteristic");
        if (mNameService == null) {
            for (BluetoothGattService gattService: mBluetoothLeService.getSupportedGattServices()) {
                if (gattService.getUuid().equals(SampleGattAttributes.UUID_NAME_SERVICE)) {
                    mNameService = gattService;
                    Log.d(TAG, "rename service found");
                    break;
                }
            }
            mNameCharacteristic = mNameService.getCharacteristic(SampleGattAttributes.UUID_NAME_CHANNEL_CHARACTERISTICS);
        }
        mBluetoothLeService.writeCharacteristic(mNameCharacteristic);
    }
    [..]

..所以我试图在收到该特征的当前值后立即写入数据。 (在 ACTION_GATT_SERVICES_DISCOVERED 也不起作用之后立即这样做)

这是我对 BluetoothLeService 的更改:

 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
     [..]
     @Override
     public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
         Log.d(TAG, "onCharacteristicWrite, Status: " + status + ", " + characteristic.getUuid().toString());
         super.onCharacteristicWrite(gatt, characteristic, status);
         //Log.d(TAG, "onCharacteristicWrite value:"+characteristic.getStringValue(0)+" ");
     }

     @Override
     public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
         Log.d(TAG, "onReliableWriteCompleted");
         super.onReliableWriteCompleted(gatt, status);
     }
 };

 public void writeCharacteristic(BluetoothGattCharacteristic ch) {
     if (mBluetoothGatt == null) return;
     ch.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
     mBluetoothGatt.beginReliableWrite();
     String value = "TestValue" + new Random().nextInt(1000) + "[=12=]";
     ch.setValue(value);
     mBluetoothGatt.writeCharacteristic(ch);
     mBluetoothGatt.executeReliableWrite();
 }

回调方法 "onCharacteristicWrite" 被调用,状态为 130 GATT_WRONG_STATE。 "onReliableWriteCompleted" 永远不会被调用。 我尝试了所有 3 种写入类型设置都无济于事。

这是日志输出:

2020-04-08 15:03:25.540 4290-4315/com.example.android.bluetoothlegatt I/BluetoothLeService: Connected to GATT server.
2020-04-08 15:03:25.540 4290-4315/com.example.android.bluetoothlegatt D/BluetoothGatt: discoverServices() - device: E1:4B:30:8B:ED:AD
2020-04-08 15:03:25.541 4290-4315/com.example.android.bluetoothlegatt I/BluetoothLeService: Attempting to start service discovery:true
2020-04-08 15:03:26.484 4290-4307/com.example.android.bluetoothlegatt D/BluetoothGatt: onConnectionUpdated() - Device=E1:4B:30:8B:ED:AD interval=6 latency=0 timeout=500 status=0
2020-04-08 15:03:27.178 4290-4307/com.example.android.bluetoothlegatt D/BluetoothGatt: onSearchComplete() = Device=E1:4B:30:8B:ED:AD Status=0
2020-04-08 15:03:27.291 4290-4315/com.example.android.bluetoothlegatt D/BluetoothGatt: onConnectionUpdated() - Device=E1:4B:30:8B:ED:AD interval=171 latency=6 timeout=600 status=0
2020-04-08 15:03:27.340 4290-4298/com.example.android.bluetoothlegatt I/bluetoothlegat: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2020-04-08 15:03:28.742 4290-4290/com.example.android.bluetoothlegatt D/BluetoothAdapter: isLeEnabled(): ON
2020-04-08 15:03:28.742 4290-4290/com.example.android.bluetoothlegatt D/BluetoothLeScanner: could not find callback wrapper
2020-04-08 15:03:31.711 4290-4290/com.example.android.bluetoothlegatt D/BluetoothGatt: setCharacteristicNotification() - uuid: 33b5376e-0942-1f91-379b-ac5af36b9efa enable: true
2020-04-08 15:03:33.704 4290-4315/com.example.android.bluetoothlegatt D/BluetoothLeService: broadcast Update
2020-04-08 15:03:33.728 4290-4290/com.example.android.bluetoothlegatt D/DeviceControlActivity: testWriteToCharacteristic
2020-04-08 15:03:33.728 4290-4290/com.example.android.bluetoothlegatt D/DeviceControlActivity: rename service found
2020-04-08 15:03:34.130 4290-4307/com.example.android.bluetoothlegatt D/BluetoothLeService: onCharacteristicWrite, Status: 130, 33b5376e-0942-1f91-379b-ac5af36b9efa

读取所有特征值没问题

关于如何解决这个问题有什么建议吗?

此致, 迈克尔

编辑 - 问题已解决:

问题出在 ReliableWrite 上。将其删除,现在可以使用了。 谢谢 Emil 的提示!

要使用可靠写入,特征属性必须包括可靠写入位。您的特征没有,所以只需使用 "normal" 来代替。