簡體   English   中英

Android BLE:消息長度問題

[英]Android BLE: Message Length issue

我正在用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

對特征數據懷有意圖}

不幸的是沒有。 如果您是BLE外設的代碼作者,雖然可以減少傳輸的數量,但是您無法將24位填充為單個特征,因為BLE將特征的寬度限制為20個字節。 如果您是BLE外設的作者,則可以將其更改為發送2個12字節數據包。

如果不是,則可能是在發送之前嘗試收集所有數據。 一種簡單的方法是在調用onCharacteristicWrite時創建一個長度為24的字節數組。 然后,每次調用onCharacteristicChanged時,都將數據添加到數組中。 將所有24個字節添加到數組后,進行廣播。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM