BluetoothGatt.writeCharacteristic()始终返回false. [英] BluetoothGatt.writeCharacteristic() always returns false.

查看:444
本文介绍了BluetoothGatt.writeCharacteristic()始终返回false.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing an Android app to talk with an Arduino using BLE. I have been able to scan devices, connect to the target, discover services, get characteristics, and read those that are readable. However, when I try to write a writeable characteristics, the method always returns false. When I debugged into the android.bluetooth code, the following sequence occurs: characteristic.getService().getDevice always returns null, which causes the writeCharacteristic to fail.

任何帮助将不胜感激!

推荐答案

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

Please check your objects in the corresponding sequence. I keep only the BluetoothGatt object and create BluetoothGattService and BluetoothGattCharacteristic every time I need to write to the BLE device.

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

这篇关于BluetoothGatt.writeCharacteristic()始终返回false.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆