gpt4 book ai didi

android - BluetoothGatt.writeCharacteristic() 总是返回 false。

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:12 27 4
gpt4 key购买 nike

我正在编写一个 Android 应用程序以使用 BLE 与 Arduino 对话。我已经能够扫描设备,连接到目标,发现服务,获取特征,并读取那些可读的。但是,当我尝试编写可写特性时,该方法总是返回 false。当我调试到 android.bluetooth 代码时,会发生以下序列:characteristic.getService().getDevice 始终返回 null,这会导致 writeCharacteristic 失败。

非常感谢任何帮助!

最佳答案

请按相应的顺序检查您的对象。每次我需要写入 BLE 设备时,我只保留 BluetoothGatt 对象并创建 BluetoothGattService 和 BluetoothGattCharacteristic。

byte[] data_to_write; // Your data
BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SREVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
BluetoothDevice mDevice = mBluetoothAdapter.getRemoteDevice(....);
BluetoothGatt mBG = mDevice.connectGatt(....);

BluetoothGattService mSVC = mBG.getService(service_uuid);
BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(characteristic_uuid);
mCH.setValue(data_to_write);
mBG.writeCharacteristic(mCH);

关于android - BluetoothGatt.writeCharacteristic() 总是返回 false。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23879869/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com