Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bytes greater than 127 #9

Closed
hamidnaghi opened this issue Jul 3, 2016 · 7 comments
Closed

Bytes greater than 127 #9

hamidnaghi opened this issue Jul 3, 2016 · 7 comments

Comments

@hamidnaghi
Copy link

Once I send the bytes with value of 128 to 255 by Bluetooth to Android then I see different bytes. I receive -17, -65, -67 instead of 128 to 255. But, it works with 0 to 127. Something happens to "(byte[]) msg.obj" with negative values in the
private final Handler mHandler = new Handler()
in the section
case MESSAGE_READ:
byte[] bufferRead = (byte[]) msg.obj;

Would you please resolve it?
Best regards
Hamid Naghizadeh

@MacroYau
Copy link
Owner

MacroYau commented Jul 3, 2016

This is due to the fact that primitives in Java, including byte, are signed. You can implement int unsignedByte = signedByte & 0xFF as instructed in the Stack Overflow answer as a workaround.

@hamidnaghi
Copy link
Author

Thank you Macro,
But, If I send any negative value then it only receive -17, -65, -67.

@hamidnaghi
Copy link
Author

Even, If I implement int unsignedByte like your comment then I will receive 3 bytes 239, 191, 189 instead of 1 byte for each negative value I send out. And it is same as negative one, just in unsigned value.

@MacroYau
Copy link
Owner

MacroYau commented Jul 7, 2016

Can you specify your expected input and output?

@hamidnaghi
Copy link
Author

I'm sending 0 to 255 from another bluetooth to android and I'm expecting to receive same number on Android, but I'm receiving 0 to 127 for numbers 0 to 127 I send which is match and OK, but for other numbers between 127 to 255 I'm receiving only 63, not same number that I sent.

@hamidnaghi
Copy link
Author

hamidnaghi commented Jul 11, 2016

The input from other bluetooth is an array with 256 elements contains 0 to
255 that I'm sending to Android, But, the output on Android is 0 to 127 and
only 63 for the data input between 128 to 255.

On Thu, Jul 7, 2016 at 8:15 AM, Macro Yau [email protected] wrote:

Can you specify your expected input and output?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#9 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AQt0hOwYCAtMlXYMni9QF2aajN4HfwJsks5qTO33gaJpZM4JD0EK
.

@MacroYau
Copy link
Owner

@hamidnaghi Now I understand your issue after testing with an Arduino Bluetooth module. It is caused by the handling of the received bytes in the SPPService class, which incoming bytes are buffered into a String (and hence distorted the bytes since some of them cannot be properly encoded).

A simple workaround would be encoding the values as Strings on the device sending the data, if possible. Alternatively, you can consider modifying the SPPService class to enable it to appropriately handle the incoming bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants