onCharacteristicWriteRequest @Override是红色下划线

时间:2018-01-05 15:45:45

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

我使用蓝牙处理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);
}

1 个答案:

答案 0 :(得分:0)

您是否期待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());
        }
    }
相关问题