0

在android中通过蓝牙低功耗编写十六进制命令后,有没有办法得到答案?我在 gatt 上写了十六进制命令,这是我的写函数:

/* 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);
}

写入完成后,回调告诉我它是成功还是失败,但接收者会发回一个答案。目前只有检查成功与否,但我不想显示接收者的答案。有没有办法显示答案?

    /*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() ?

如果此值与您设置和发送的值不同,则可能是您正在寻找的响应。

此外,请确保您正在写入的特性具有已读取或可通知的属性。您可以按如下方式执行此操作:

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 ";

一个服务可能有两个特征——一个是可写的(用于发送数据),一个是可通知的,用于接收数据。这使得接收方可以进行异步处理。确保服务中没有其他可通知或可读的特征

于 2014-02-26T19:48:08.287 回答