首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将十六进制值发送到BLE设备?

如何将十六进制值发送到BLE设备?
EN

Stack Overflow用户
提问于 2018-03-26 20:31:06
回答 1查看 925关注 0票数 0

我正在开发一个ble应用程序,作为输入,我必须给BLE一个十六进制值作为时间参数,这样设备就可以运行那么多时间。我已经试过所有我知道的方法,但都不起作用。请帮我找到一个解决方案。

代码语言:javascript
复制
    AppConstants.pulse_time.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                        AppConstants.gatt.setCharacteristicNotification(AppConstants.pulse_time, true);
//                        AppConstants.pulse_time.setValue(3, BluetoothGattCharacteristic.FORMAT_UINT16, 0);
//                        AppConstants.pulse_time.setValue(toBytes(03));
//                        AppConstants.pulse_time.setValue(toHex(""+3));
//                        AppConstants.pulse_time.setValue("0x2");
//                        AppConstants.pulse_time.setValue(convertStringToHex("32"));
                        AppConstants.pulse_time.setValue("0x"+ Integer.toHexString(2));
//                        AppConstants.pulse_time.setValue(String.format("%02x","2"));
//                        AppConstants.pulse_time.setValue(Integer.toString(2, 16));
//                        AppConstants.pulse_time.setValue(hexStringToByteArray("0x2"));
//                        AppConstants.pulse_time.setValue(31,BluetoothGattCharacteristic.FORMAT_UINT8,0);
//                        AppConstants.pulse_time.setValue(toBytes(3));
                        AppConstants.gatt.writeCharacteristic(AppConstants.pulse_time);



private String convertStringToHex(String string)
    {
        StringBuilder newString = new StringBuilder();
        for (int i=0; i<string.length(); i++)
        {
            newString.append(String.format("%02X ", (byte)(string.charAt(i))));
        }
        return newString.toString();
    }

    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));
        }
        return data;
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
EN

回答 1

Stack Overflow用户

发布于 2018-03-26 20:41:11

晚上好,能不能请您解释一下,结果应该是哪种“十六进制”的-value?你需要这两个函数在字符串和十六进制(十六进制数字)之间进行转换吗?

如果代码是Java,请参阅:

代码语言:javascript
复制
public static int convert(int n) {
  return Integer.valueOf(String.valueOf(n), 16);
}

或者:

代码语言:javascript
复制
Integer.toHexString(int)

我知道这只是一个整数,不是字符串。请解释更多,"AppConstants.pulse_time.setValue“期望作为参数的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49491545

复制
相关文章

相似问题

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