私は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
特性データを使用してインテントを設定する }