0

AndroidでBluetooth Low Energy経由で16進コマンドを書いた後に答えを得る方法はありますか? gatt を介して 16 進コマンドを書き込みます。これが書き込み関数です。

/* set new value for particular characteristic */
public void writeDataToCharacteristic(final BluetoothGattCharacteristic ch, final byte[] dataToWrite) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null || ch == null) return;

    // first set it locally....
    ch.setValue(dataToWrite);
    // ... and then "commit" changes to the peripheral
    mBluetoothGatt.writeCharacteristic(ch);
}

書き込みが完了すると、Callback は成功したか失敗したかだけを通知しますが、受信者は応答を返します。今は成功したかどうかのチェックだけですが、受信者からの回答を表示したくありません。答えを表示する方法はありますか?

    /*The callback function*/
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        String deviceName = gatt.getDevice().getName();
        String serviceName = BleNamesResolver.resolveServiceName(characteristic.getService().getUuid().toString().toLowerCase(Locale.getDefault()));
        String charName = BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString().toLowerCase(Locale.getDefault()));
        String description = "Device: " + deviceName + " Service: " + serviceName + " Characteristic: " + charName;

        // we got response regarding our request to write new value to the characteristic
        // let see if it failed or not
        if(status == BluetoothGatt.GATT_SUCCESS) {
             mUiCallback.uiSuccessfulWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic, description);
        }
        else {
             mUiCallback.uiFailedWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic, description + " STATUS = " + status);
        }
    };
4

1 に答える 1

0

コールバックで試しましたか

characteristic.getValue() ?

この値が、設定して送信した値と異なる場合、これが探している応答である可能性があります。

また、書き込み先の Characteristic に読み取りまたは通知可能なプロパティがあることを確認してください。これは次のように行うことができます。

int props = characteristic.getProperties();
String propertiesString = String.format("0x%04X ", props);
if((props & BluetoothGattCharacteristic.PROPERTY_READ) != 0) propertiesString += "read ";
if((props & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) propertiesString += "write ";
if((props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) propertiesString += "notify ";
if((props & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) propertiesString += "indicate ";

サービスには 2 つの Characteristic が存在する可能性があります。1 つは書き込み可能 (データ送信用) で、もう 1 つはデータ受信用の通知可能です。これにより、受信者は非同期処理を行うことができます。通知可能または読み取り可能な別の特性がサービスにないことを確認してください

于 2014-02-26T19:48:08.287 に答える