简体   繁体   English

Java将字符串十六进制值转换为byte [],重新创建了obj-c功能

[英]Java convert string hex values to byte[], recreating this obj-c functionality

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 它需要特定的握手,并且在iOS Objective-C都可以正常工作,但是,在Java重新创建此功能时遇到问题

Any thoughts greatly appreciated! 任何想法,不胜感激!

WORKING Objective-C code: 工作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];

So far for android I have the following as an equivalent: 到目前为止,对于android,我具有以下等效项:

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

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 如前所述,iOS运作良好,但与Java相当的设备没有任何响应,这使我认为发送的数据错误/无法识别

UPDATE So, after plenty of wrestling with this I have a little more insight into what is going on 'I think!' 更新因此,经过大量的努力之后,我对“我认为!”发生的事情有了更多的了解。

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 正如下面提到的可怕的袋熊,int的最大值是127,所以0x99和0xAA数组中的2个值当然不在此范围内

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

Produces 产生

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

However the expected values need to be 但是,期望值必须是

[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. 但是,数组中的结果值始终相同。

Please help!!! 请帮忙!!! :) :)

UPDATE 2 更新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 经过一天的挖掘,看来尽管这些值记录有误,但蓝牙设备感知的值仍应正确,因此我修改了此问题,并在此处继续进行

Why are you not doing it the same way as for C 为什么不像C那样做

In this code 在这段代码中

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

this is just making a text String where the first char is 0 and the second is x etc 这只是使文本字符串,其中第一个字符为0 ,第二个字符为x

try 尝试

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

edit 编辑

You maybe need to reconsider values such as 0x99 as in java the byte values are as per javadocs 您可能需要重新考虑诸如0x99值,如java中的字节值是根据javadocs

It has a minimum value of -128 and a maximum value of 127 (inclusive). 最小值为-128,最大值为127(含)。

See Can we make unsigned byte in Java 请参阅我们可以在Java中制作无符号字节吗

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

will also account the , and so creates to much bytes and will not geht the same byte s AS in your c code. 也将占了,所以要创建多bytes ,并不会geht相同byte S作为在C代码。

Try to use a byte[] directly. 尝试直接使用byte[]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM