Java将字符串十六进制值转换为byte [],重新创建了obj-c功能 [英] Java convert string hex values to byte[], recreating this obj-c functionality

查看:129
本文介绍了Java将字符串十六进制值转换为byte [],重新创建了obj-c功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个与特定的蓝牙低功耗设备通信的应用程序.它需要特定的握手,并且在iOSObjective-C中都可以正常使用,但是,在Java

I am making an app that communicates with a specific Bluetooth Low Energy device. It requires a specific handshake and this is all working perfectly in Objective-C for iOS, however, I am having trouble recreating this functionality in Java

任何想法都值得赞赏!

工作Objective-C代码:

uint8_t bytes[] = {0x04,0x08,0x0F,0x66,0x99,0x41,0x52,0x43,0x55,0xAA};
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
[_btDevice writeValue:data forCharacteristic:_dataCommsCharacteristic type:CBCharacteristicWriteWithResponse];

到目前为止,对于android,我具有以下等效项:

So far for android I have the following as an equivalent:

byte[] handshake = {0x04,0x08,0x0F,0x66,(byte)0x99,0x41,0x52,0x43,0x55,(byte)0xAA};

characteristic.setValue(handshake);
boolean writeStatus = gatt.writeCharacteristic(characteristic);
Log.d(TAG,"Handshake sent: " + writeStatus);

如前所述,iOS运作良好,但Java中的等效功能却未从设备获得任何响应,这使我认为发送的数据有误/无法识别

As mentioned, iOS works great, but the equivalent in Java is getting no response from the device, leading me to think that the data being sent is wrong/not recognised

更新 因此,经过大量的努力,我对我认为!"发生的事情有了更多的了解.

UPDATE So, after plenty of wrestling with this I have a little more insight into what is going on 'I think!'

就像下面提到的可怕袋熊一样,一个int的最大值是127,所以0x99和0xAA数组中的2个值当然不在此范围内

As Scary Wombat mentioned below the maximum value of an int is 127 so the 2 values in the array of 0x99 and 0xAA are of course out of this range

以下是我对这些值所处的位置:

The below is where I am at with the values:

byte bytes[] = {0x04,0x08,0x0F,0x66,(byte)0x99,0x41,0x52,0x43,0x55,(byte)0xAA};
Log.d(TAG, Arrays.toString(bytes));

生产

[4, 8, 15, 102, -103, 65, 82, 67, 85, -86]

但是期望值必须是

[4, 8, 15, 102, 153, 65, 82, 67, 85, 170]

我尝试过隐式转换这些麻烦的字节,还尝试了以下方法:

I have tried casting these troublesome bytes implicitly and also tried the below below:

byte bytes[] = {0x04,0x08,0x0F,0x66,(byte)(0x99 & 0xFF),0x41,0x52,0x43,0x55,(byte)(0xAA & 0xFF)};

但是,数组中的结果值始终相同.

However the resulting values in the array are always the same.

请帮助!!! :)

更新2

经过一天的挖掘,看来尽管这些值记录有误,但蓝牙设备感知的值仍应正确,因此我修改了此问题,并继续进行

After a day of digging it appears that although the values are logging incorrectly the values perceived by the Bluetooth device SHOULD still be correct, so I have modified this question and continuing over here

推荐答案

String handshakeString = "0x04,0x08,0x0F,0x66,0x99,0x41,0x52,0x43,0x55,0xAA";
byte[] value = handshakeString.getBytes();

也会考虑,,因此会创建很多bytes,并且不会在您的c代码中包含相同的byte AS.

will also account the , and so creates to much bytesand will not geht the same bytes AS in your c code.

尝试直接使用byte[].

byte[] value=new byte[]{0x04,0x08,0x0F,0x66,0x99,0x41,0x52,0x43,0x55,0xAA};

这篇关于Java将字符串十六进制值转换为byte [],重新创建了obj-c功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆