如何将BLE BluetoothGatt.writeCharacteristic的字符串转换为字节?

时间:2014-05-23 10:14:17

标签: android string byte bluetooth-lowenergy

我正在Android BLE开发。

我尝试将字符串发送到BLE device(like TI CC2541),似乎无法将字符串直接发送到BLE device。 它需要将字符串转换为字节

我搜索了一些信息,有人使用URLEncoder.encode。 但我不确定我需要的答案是什么。

但是如何将String转换为Byte?

writeCharacteristic

的以下代码为BLE
public void writeString(String text) {
        // TODO Auto-generated method stub
        BluetoothGattService HelloService = mBluetoothGatt.getService(HELLO_SERVICE_UUID);

        BluetoothGattCharacteristic StringCharacteristic = HelloService.getCharacteristic(UUID_HELLO_CHARACTERISTIC_WRITE_STRING);

        mBluetoothGatt.setCharacteristicNotification(StringCharacteristic , true);

            int A = Integer.parseInt(text);

            //How to convert the String to Byte here and set the Byte to setValue ?????

        StringCharacteristic .setValue(A, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        mBluetoothGatt.writeCharacteristic(StringCharacteristic );

        Log.d(TAG, "StepCount Characteristic End!");        

    }

如何将字符串转换为字节?

2 个答案:

答案 0 :(得分:1)

你得到你的字符串:

                    byte[] strBytes = text.getBytes();  
byte[] bytes = context.yourmWriteCharacteristic.getValue(); 

请添加一个空检查:

 if (bytes == null) {

                        Log.w("Cannot get Values from mWriteCharacteristic.");
                        dismiss();// equivalent action
                    }

 if (bytes.length <= strBytes.length) {
                        for(int i = 0; i < bytes.length; i++) {
                            bytes[i] = strBytes[i];
                        }
                    } else {
                        for (int i = 0; i < strBytes.length; i++) {
                            bytes[i] = strBytes[i];
                        }
                    }

现在,像:

 StepCount_Characteristic.setValue(bytes);
        mBluetoothGatt.writeCharacteristic(StepCount_Characteristic);

答案 1 :(得分:0)

我发现以下代码可以帮助我转换字符串。

private byte[] parseHex(String hexString) {
        hexString = hexString.replaceAll("\\s", "").toUpperCase();
        String filtered = new String();
        for(int i = 0; i != hexString.length(); ++i) {
            if (hexVal(hexString.charAt(i)) != -1)
                filtered += hexString.charAt(i);
        }

        if (filtered.length() % 2 != 0) {
            char last = filtered.charAt(filtered.length() - 1);
            filtered = filtered.substring(0, filtered.length() - 1) + '0' + last;
        }

        return hexStringToByteArray(filtered);
    }

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

    private int hexVal(char ch) {
        return Character.digit(ch, 16);
    }

如果要转换字符串值。你只需要像下面那样打电话:

String text;
byte[] value = parseHex(text);