写8位消息

时间:2014-07-24 16:20:54

标签: android bluetooth byte

我必须通过蓝牙发送一个字节,问题是我不能使用这个脚本,因为我必须写出大于127的数字:

mChatService.write(new byte[]{});

我该怎么办? 感谢。

修改

logcat的:

07-25 14:18:20.410: D/AndroidRuntime(1906): Shutting down VM
07-25 14:18:20.410: W/dalvikvm(1906): threadid=1: thread exiting with uncaught exception (group=0x41607ce0)
07-25 14:18:20.410: E/AndroidRuntime(1906): FATAL EXCEPTION: main
07-25 14:18:20.410: E/AndroidRuntime(1906): Process: com.example.android.BluetoothChat, PID: 1906
07-25 14:18:20.410: E/AndroidRuntime(1906): java.lang.NumberFormatException: Invalid int: ""
07-25 14:18:20.410: E/AndroidRuntime(1906):     at java.lang.Integer.invalidInt(Integer.java:137)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at java.lang.Integer.parseInt(Integer.java:358)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at java.lang.Integer.parseInt(Integer.java:331)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at com.example.android.BluetoothChat.BluetoothChat$2.handleMessage(BluetoothChat.java:517)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at android.os.Looper.loop(Looper.java:136)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at android.app.ActivityThread.main(ActivityThread.java:5141)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at java.lang.reflect.Method.invoke(Method.java:515)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
07-25 14:18:20.410: E/AndroidRuntime(1906):     at dalvik.system.NativeStart.main(Native Method)

接收代码

byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(readMessage);

1 个答案:

答案 0 :(得分:1)

Java中的字节已签名,因此最大范围为-128到+127。如果你真的需要1个字节,你可以将它转换为接收端的整数,然后加上128,使其在0-255范围内。