我通过 LocalBroadcastManager 在 Android 中接收 ByteArray。它工作正常,但是我分块发送。
当我想合并块时,我得到一个错误。
这是代码:
else if (message.equals(BtConstants.BtStateUpdates.BT_UPDATE_STATE_RXRAW_COMPLETE.getString())) {
Log.i(TAG, "length [" + Integer.toString(picRaw.length()) + "] / capacity [" + Integer.toString(picRaw.capacity()) + "]");
Log.i(TAG,"append" + Integer.toString(intent.getExtras().getByteArray(BtConstants.btBytePayload).length));
picRaw.append(intent.getExtras().getByteArray(BtConstants.btBytePayload), picRaw.length(),
intent.getExtras().getByteArray(BtConstants.btBytePayload).length);
String s = "";
try {
s = new String(picRaw.buffer(), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "RCVD RAW: " + s);
}
这是错误。请注意,首先它会附加较小的 650 字节,但随后会在附加更大的值时失败。
**10-25 11:21:40.670: I/BluetoothService(9537): length [0] / capacity [15000]
10-25 11:21:40.675: I/BluetoothService(9537): append665**
Works FINE so far
10-25 11:21:40.680: V/BluetoothSocket.cpp(9537): readNative
10-25 11:21:40.680: I/BluetoothService(9537): RCVD RAW: �..edited out for brevity.�
10-25 11:21:40.680: D/BluetoothService(9537): Rcvd broadcast: com.test.ppt.bt_state_raw_connected
**10-25 11:21:40.695: I/BluetoothService(9537): length [665] / capacity [15000]
10-25 11:21:40.695: I/BluetoothService(9537): append5320
10-25 11:21:40.695: D/AndroidRuntime(9537): Shutting down VM
10-25 11:21:40.695: W/dalvikvm(9537): threadid=1: thread exiting with uncaught exception (group=0x40c3c1f8)
10-25 11:21:40.730: E/AndroidRuntime(9537): FATAL EXCEPTION: main
10-25 11:21:40.730: E/AndroidRuntime(9537): java.lang.IndexOutOfBoundsException**
10-25 11:21:40.730: E/AndroidRuntime(9537): at org.apache.http.util.ByteArrayBuffer.append(ByteArrayBuffer.java:68)
10-25 11:21:40.730: E/AndroidRuntime(9537): at com.test.ppt.btService.BluetoothService$1.onReceive(BluetoothService.java:217)
10-25 11:21:40.730: E/AndroidRuntime(9537): at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
10-25 11:21:40.730: E/AndroidRuntime(9537): at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
10-25 11:21:40.730: E/AndroidRuntime(9537): at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
10-25 11:21:40.730: E/AndroidRuntime(9537): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 11:21:40.730: E/AndroidRuntime(9537): at android.os.Looper.loop(Looper.java:137)
10-25 11:21:40.730: E/AndroidRuntime(9537): at android.app.ActivityThread.main(ActivityThread.java:4507)
10-25 11:21:40.730: E/AndroidRuntime(9537): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:21:40.730: E/AndroidRuntime(9537): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 11:21:40.730: E/AndroidRuntime(9537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-25 11:21:40.730: E/AndroidRuntime(9537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-25 11:21:40.730: E/AndroidRuntime(9537): at dalvik.system.NativeStart.main(Native Method)
初始容量为 15000 - 这应该足够了。