Android无法写入正确的数据
我正在尝试开发一个Android应用程序,使用BLE连接到基于NRF51822的系统。其目的是将3字节值(RGB)写入my自定义特征 Android是GATT客户端,基于NRF51的设备是GATT服务器 我能够成功地建立ble连接并发现我的特征 然而,数据发送部分(设置值)给我带来了麻烦。无论我写哪3个字节,我都会在NRF51端得到相同的常量数据Android无法写入正确的数据,android,bluetooth-lowenergy,gatt,nrf51,Android,Bluetooth Lowenergy,Gatt,Nrf51,我正在尝试开发一个Android应用程序,使用BLE连接到基于NRF51822的系统。其目的是将3字节值(RGB)写入my自定义特征 Android是GATT客户端,基于NRF51的设备是GATT服务器 我能够成功地建立ble连接并发现我的特征 然而,数据发送部分(设置值)给我带来了麻烦。无论我写哪3个字节,我都会在NRF51端得到相同的常量数据 R = 4 | G = 239 | B= 1 下面是我的相关代码(Android) 谢谢 我喜欢这个。答案不准确,但可能会对你有所帮助 @覆盖 发现
R = 4 | G = 239 | B= 1
下面是我的相关代码(Android)
谢谢
我喜欢这个。答案不准确,但可能会对你有所帮助
@覆盖
发现服务上的公共无效(Bluetooth gatt,int状态)
{
List services=gatt.getServices();
Log.i(“OnServicesDiscovery”,services.toString());
keepConnect=services.get(3.getCharacteristics().get(0);
if(keepConnect!=null){
书面特征(关贸总协定,保持联系);
}
}
私有无效写入特征(蓝牙gatt、蓝牙gatt特征特征)
{
字节[]字节数据=十六字节(“6fd362e40ebcd0945bf58dc4”);
byte[]writeData=新字节[byteData.length];
for(int i=0;i 数据[i/2]=(字节)((Character.digit(hexexpresentation.charAt(i),16))将字节1:1从byteData[]复制到writeData[]有什么特殊原因吗?
R = 4 | G = 239 | B= 1
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
List<BluetoothGattService> services = gatt.getServices();
Log.i("onServicesDiscovered", services.toString());
keepConnect=services.get(3).getCharacteristics().get(0);
if(keepConnect!=null){
writeCharacteristic(gatt,keepConnect);
}
}
private void writeCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
{
byte[] byteData = hexToBytes("6fd362e40ebcd0945bf58dc4");
byte[] writeData = new byte[byteData.length];
for (int i = 0; i < byteData.length; i++) {
writeData[i] = byteData[i];
}
characteristic.setValue(writeData);
gatt.writeCharacteristic(characteristic);
}
public static byte[] hexToBytes(String hexRepresentation) {
int len = hexRepresentation.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexRepresentation.charAt(i), 16) << 4)
+ Character.digit(hexRepresentation.charAt(i + 1), 16));
}
return data;
}