首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android特性setValue无法写入正确的数据

Android特性setValue无法写入正确的数据
EN

Stack Overflow用户
提问于 2017-09-03 13:33:22
回答 1查看 2.8K关注 0票数 3

我正在尝试开发一个使用BLE连接到基于NRF51822的系统的Android。其目的是将一个3字节值(RGB)写入我的自定义特性。

安卓是关贸总协定的客户端,基于NRF51的设备是关贸总协定的服务器。

我能够成功地建立自己的联系,发现自己的特点。

然而,数据发送部分(setValue)给我带来了麻烦。无论我写什么字节,在NRF51端都会得到相同的常量数据。

下面是我的相关代码(Android)

代码语言:javascript
复制
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
       Log.d("BLENotifier", "BLE GAT : SERVICES DISCOVERED");
       for(BluetoothGattService gs: gatt.getServices()) {
           Log.d("BLENotifier", "SERVICE = " + gs.getUuid().toString());
       }
       //SELECT MY CHARACTERSTIC
       ble_my_characterstic = gatt.getService(ble_service_uuid).getCharacteristic(ble_characterstic_uuid);
       Log.d("BLENotifier", "BLE SELECTED CHARACTERSTIC " + ble_my_characterstic.getUuid().toString());
       ble_connected = true;
    }

public void writedata(String data){
    //WRITE DATA TO MY CHARACTERSTIC
    if(ble_my_characterstic != null && ble_connected == true){

        my_gatt_handle.executeReliableWrite();
        //ble_my_characterstic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        ble_my_characterstic.setValue(hexStringToByteArray(data));
        my_gatt_handle.writeCharacteristic(ble_my_characterstic);

        Log.d("BLENotifier", "BLE WRITE DATA " + data);
    }

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i+1), 16));
    }
    Log.d("BLENotifier", "hexStringToByteArray " + Integer.toString((int)data[0]) + " " + Integer.toString((int)data[1]) + " " + Integer.toString((int)data[2]));
    return data;
}

我正在以ble_handle.writedata("0000FF")的形式调用ble_handle.writedata("0000FF")方法

这就是我在NRF51方面得到的

代码语言:javascript
复制
R = 4 | G = 239 | B= 1

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-09-04 04:27:16

我确实喜欢this.Not的确切答案,但可能对你有帮助。

代码语言:javascript
复制
    @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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46023939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档