我是Android开发的新手,所以如果描述不完全正确,请原谅。
我使用Android BTLE示例应用程序作为模板,并尝试将简单的十六进制值发送到Microchip RN4020模块。我可以成功发送数据(特别是使用日历功能的时间数据),它们出现在我的PuTTY窗口中。然而,问题在于数据正在被传输'从应用程序(在我的三星j5上)作为ASCII表示....换句话说,日历函数返回分钟,如20(20hex = 32十进制),这在我的PuTTY终端上以某种方式结束为ASCII 20(即32 30)。
我将数据作为字节数组发送,据我所知,它将值范围限制为-127到+127。所以为了解决这个问题,我试图将值分解为上下半字节并将它们作为单独的字节发送.....同样的问题。出现的值是ASCII 2 0 ....(即32 30)。
我冒昧地张贴在我的代码下面(为它的笨拙而道歉)。我强烈怀疑这是一个非常基本的错误,但我无法找到如何传输十六进制值'请帮忙
问候
Ĵ
public void onClickTest(View v){
if(mBluetoothLeService != null) {
Calendar calendar = Calendar.getInstance();
//Data_1[0] = (Integer.toHexString((byte)calendar.get(Calendar.SECOND) & 0xFF)); //Alarm Set
int temp = 0;
int temp1 = 0;
byte first = 0;
byte second = 0;
temp = calendar.get(Calendar.HOUR_OF_DAY);
temp1 = temp;
temp = temp & 0xf0;
temp = temp >> 4;
temp1 = temp1 & 0x0f;
first = (byte)temp;
second = (byte)temp1;
Data_1[0] = first;
Data_1[1] = second; //Alarm Set
//Data_1[1] = 0xff; //Update clock
// Data_1[2] = calendar.get(Calendar.SECOND);
// Data_1[2] = calendar.get(Calendar.SECOND);
// Data_1[3] = (char)calendar.get(Calendar.MINUTE);
// Data_1[4] = (char)calendar.get(Calendar.HOUR_OF_DAY);
// Data_1[5] = (byte)calendar.get(Calendar.DAY_OF_WEEK);
// Data_1[6] = (byte)calendar.get(Calendar.MONTH);
// Data_1[7] = (byte)calendar.get(Calendar.YEAR);
//myArray = (Arrays.toString(Data_1));
mBluetoothLeService.writeCustomCharacteristicString(Data_1);
这是实际的LE服务位
public void writeCustomCharacteristicString(byte[] value) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService1 = mBluetoothGatt.getService(UUID.fromString("12345678-9012-3456-7890-1234567890FF"));
if(mCustomService1 == null){
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mWriteCharacteristic1 = mCustomService1.getCharacteristic(UUID.fromString("12345678-9012-3456-7890-123456789011"));
mWriteCharacteristic1.setValue(value);
if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic1) == false){
Log.w(TAG, "Failed to write characteristic");
}
}