0

我正在使用 BLE 开发一个 android 应用程序。此应用程序的要求是更新具有各种输入的特定硬件中的电压变化。

所以我将字符作为 8 位输入写入 BLE。每个位值都包含其自己的表示。硬件会根据每个请求做出响应并提供各种输出组合。输出包含 24 个字节的信息。每个字节位置代表不同的值。例如:位置 1 和 2 代表电流,3 和 4 代表电压等。我的问题是,我将输出分为 4 个部分。每条消息包含 6 个字节。是否有可能在一条消息中获得相同的信息?

执行

 public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {                      //Check that we have access to a Bluetooth radio
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        int test = characteristic.getProperties();                                      //Get the properties of the characteristic
        if ((test & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 && (test & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) { //Check that the property is writable
            return;
        }
        DebugLogs.writeToFile("BLE MODULE Before Write " + characteristic);
        if (mBluetoothGatt.writeCharacteristic(characteristic)) {                       //Request the BluetoothGatt to do the Write
            Log.v(TAG, "****************WRITE CHARACTERISTIC SUCCESSFULL**" + characteristic);                               //The request was accepted, this does not mean the write completed
            DebugLogs.writeToFile("BLE MODULE AFTER Write SUCCESS " + characteristic);
        } else {
            Log.d(TAG, "writeCharacteristic failed");                                   //Write request was not accepted by the BluetoothGatt
            DebugLogs.writeToFile("BLE MODULE AFTER Write FAIL " + characteristic);
        }
    }

响应进入 Gatt 回调

@Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            Log.w(TAG, "**ACTION_DATA_AVAILABLE**" + characteristic.getUuid());//Indication or notification was received
            broadcastUpdate(BLEConstants.ACTION_DATA_AVAILABLE, characteristic);                     //Go broadc

用特征数据作为一个意图}

4

1 回答 1

0

不幸的是没有。如果您是 BLE 外设中代码的作者,您可以减少传输次数,但您不能将 24 个位放入单个特征中,因为 BLE 将特征的宽度限制为 20 个字节。如果您是 BLE 外设作者,您或许可以将其更改为发送 2 个 12 字节数据包。

如果不是,那么您可能正试图在发送之前收集所有数据。一个简单的方法是在调用 onCharacteristicWrite 时创建一个长度为 24 的字节数组。然后,每次调用 onCharacteristicChanged 时,将数据添加到数组中。将所有 24 个字节添加到数组后,广播它。

希望这可以帮助!

于 2016-08-26T17:13:54.263 回答