Android BluetoothGatt writeCharacteristic with response

Android BluetoothGatt writeCharacteristic with response,android,ios,bluetooth-lowenergy,Android,Ios,Bluetooth Lowenergy,从这些文档中,我看到了如何在没有回应的情况下写作: BluetoothGattCharacteristic characteristic = ... characteristic.setValue(bytes); mBluetoothGatt.writeCharacteristic(characteristic); 如何使用响应执行写请求操作 (在iOS中,可以选择写入类型CBCharacteristicWriteWithResponse和CBCharacteristicWriteWithou

从这些文档中,我看到了如何在没有回应的情况下写作:

BluetoothGattCharacteristic characteristic = ...
characteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(characteristic);
如何使用响应执行写请求操作

(在iOS中,可以选择写入类型CBCharacteristicWriteWithResponse和CBCharacteristicWriteWithoutResponse)

  • 你的特征应该是可写的
  • 检查特征是否可写:

    (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))) != 0
    
  • 返回指示写入是否成功的布尔标志(如果成功,则为true)
  • BluetoothGatt.writeCharacteristic成功后,将使用指定值的特征执行回调

  • 从文件中:

    公共void setWriteType(int-writeType)

    在API级别18中添加设置此特性的写入类型

    设置特征的写入类型决定了 writeCharacteristic(BluetoothGattCharacteristic)函数写入以下内容 特色

    Parameters writeType将此特征的写入类型键入。可以是 以下选项之一:写入类型\默认值、写入类型\无响应或 写入\u类型\u签名


    是的,我没提过。但是,请注意,应用不同的写入类型的条件是,您将值写入的特征必须是可写入的,这是蓝牙设备特定的逻辑。首先找出你的特征是什么类型的,因为它可能与你需要的类型相同。一旦它满足了可写的条件,每个WRITE_类型都应该适合您

    通过分析BluetoothGatt的writeCharacteristic()方法,可以清楚地看到:

     public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
            && (characteristic.getProperties() &
                BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
    
        if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
        if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;
    
        BluetoothGattService service = characteristic.getService();
        if (service == null) return false;
    
        BluetoothDevice device = service.getDevice();
        if (device == null) return false;
    
        synchronized(mDeviceBusy) {
            if (mDeviceBusy) return false;
            mDeviceBusy = true;
        }
    
        try {
            mService.writeCharacteristic(mClientIf, device.getAddress(),
                service.getType(), service.getInstanceId(),
                new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
                new ParcelUuid(characteristic.getUuid()),
                characteristic.getWriteType(), AUTHENTICATION_NONE,
                characteristic.getValue());
        } catch (RemoteException e) {
            Log.e(TAG,"",e);
            mDeviceBusy = false;
            return false;
        }
    
        return true;
    }
    

    我知道这是我的第二个答案,但它为我的第一篇文章提供了详细说明。希望您能有所帮助。

    我正在完成所有这些工作,并成功地对write和onCharacteristicWrite进行响应,但外围设备未执行发送的请求。您好,david,您是否收到MBlueTothGatt的响应。writeCharacteristic(characteristic),如果是,请共享。