android - 如何从 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;
}
解决方案
推荐阅读
- javascript - 在 Django 上渲染一个使用的 API 到 React 前端
- php - 需要一个特定的包提交 - composer 似乎忽略了 composer.json 的要求
- python - 在后台启动的 tshark 任务不会生成任何文件
- c++ - 当我尝试加载 PNG 纹理 (Direct3D 11) 时出现“找不到组件”错误
- javascript - 将js脚本添加到django项目的问题
- html - 使用 R 跨多个链接进行网络抓取
- ios - 防止 SwiftUI 子视图重绘
- swift - Editbutton 在 SwiftUI 中以一种有趣的方式从 List 中删除项目(带有绑定)
- javascript - 如何从客户端访问服务器目录中的文件?
- java - 重定向到 Thymeleaf 中的登录页面后显示警报信息