首页 > 解决方案 > 如何从 Android 设备向 BLE 设备发送无符号整数?

问题描述

我正在开发在 BLE 设备上设置时间的应用程序。为此,我需要发送大于 127 的值数组(值如 239,254 等)。但是BLE设置特性值支持字节数组,其值限制为128。那么如何从Android设备向BLE设备发送大于128的值?PFA 代码片段,但它不起作用。查看代码片段

public static boolean writeble_Set_time_Healthyu(BluetoothGatt mBluetoothGatt) {
    try {

        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);

        byte[] list_data = new byte[15];

        list_data[0] = new Integer(Integer.parseInt(Integer.toString(254, 16), 16)).byteValue();
        list_data[1] = new Integer(Integer.parseInt(Integer.toString(239, 16), 16)).byteValue();
        list_data[2] = new Integer(Integer.parseInt(Integer.toString(1, 16), 16)).byteValue();
        list_data[3] = new Integer(Integer.parseInt(Integer.toString(250, 16), 16)).byteValue();
        list_data[4] = new Integer(Integer.parseInt(Integer.toString(7, 16), 16)).byteValue();
        list_data[5] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.HOUR), 16), 16)).byteValue();
        list_data[6] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.MINUTE), 16), 16)).byteValue();
        list_data[7] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.SECOND), 16), 16)).byteValue();
        list_data[8] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.DATE), 16), 16)).byteValue();
        list_data[9] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.MONTH) + 1, 16), 16)).byteValue();
        list_data[10] = new Integer(Integer.parseInt(Integer.toString(Integer.parseInt((calendar.get(Calendar.YEAR) + "").substring(0, 1)), 16), 16)).byteValue();
        list_data[11] = new Integer(Integer.parseInt(Integer.toString(Integer.parseInt((calendar.get(Calendar.YEAR) + "").substring(2, 3)), 16), 16)).byteValue();
        list_data[12] = new Integer(Integer.parseInt(Integer.toString(Integer.parseInt((calendar.get(Calendar.YEAR) + "").substring(2, 3)), 16), 16)).byteValue();
        list_data[13] = new Integer(Integer.parseInt(Integer.toString(0, 16), 16)).byteValue();
        list_data[14] = new Integer(Integer.parseInt(Integer.toString(165, 16), 16)).byteValue();

        List<BluetoothGattService> serviceList = mBluetoothGatt.getServices();
        for (BluetoothGattService service : serviceList) {
            List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
            for (BluetoothGattCharacteristic characteristic : characteristics) {

                characteristic.setWriteType(FORMAT_UINT32);
                characteristic.setValue(list_data);
                boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

标签: androidarraysbluetooth-lowenergyunsigned-integer

解决方案


推荐阅读