【问题标题】:onCharacteristicWriteRequest the @Override is red underlinedonCharacteristicWriteRequest @Override 带有红色下划线
【发布时间】:2018-01-05 15:45:44
【问题描述】:

我在一个带有蓝牙的 Android Things 项目上工作,@Override 带有红色下划线。

@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
    super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
    Log.d(TAG, "onCharacteristicwriterequest UUID: " + characteristic.getUuid().toString());
    mGattServer.notifyCharacteristicChanged(mBluetoothDevice, characteristic, true);
}

【问题讨论】:

  • 这意味着 onCharacteristicWriteRequest 未在您的基类中定义(因此它不是覆盖),或者您的签名错误。
  • 这意味着你的类没有扩展BluetoothGattServerCallback

标签: android android-studio bluetooth-lowenergy android-things


【解决方案1】:

您是否期待 BluetoothGattCallback 方法?如果是,则没有这样的回调方法。这是BluetoothGattCallback中可用的回调方法

 @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //A request to Write has completed
                    if (status == BluetoothGatt.GATT_SUCCESS) {                                 //See if the write was successful
                        AppLog.logError(TAG, "**ACTION_DATA_WRITTEN**" + characteristic);
                        broadcastUpdate(BLEConstants.ACTION_DATA_WRITTEN, characteristic);                   //Go broadcast an intent to say we have have written data
                    }

  }

如果你想写一些东西到 BLE,你可以使用下面的方法

 public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        try {

            if (mBluetoothAdapter == null || mBluetoothGatt == null) {                      //Check that we have access to a Bluetooth radio

                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;
            }
            if (mBluetoothGatt.writeCharacteristic(characteristic)) {                       //Request the BluetoothGatt to do the Write
                AppLog.logInfo(TAG, "****************WRITE CHARACTERISTIC SUCCESSFUL**" + (characteristic.getValue()[0] & 0xFF));//The request was accepted, this does not mean the write completed
              /*  if(characteristic.getUuid().toString().equalsIgnoreCase(getString(R.string.char_uuid_missed_connection))){

                }*/
            } else {
                AppLog.logInfo(TAG, "writeCharacteristic failed  "+ (characteristic.getValue()[0] & 0xFF));                                   //Write request was not accepted by the BluetoothGatt
            }

        } catch (Exception e) {
            AppLog.logInfo(TAG, e.getMessage());
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2022-10-17
    • 2020-03-24
    • 2020-07-29
    • 1970-01-01
    • 2023-01-02
    • 2011-09-19
    相关资源
    最近更新 更多