0

我通过 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 - 这应该足够了。

4

2 回答 2

0
  try {
                s = new String(picRaw.buffer(), "US-ASCII");
            } 
catch (ArrayIndexOutOfBoundsException e) {
            // TODO: handle exception
        }
        catch (IndexOutOfBoundsException e) {
            // TODO: handle exception
        }

catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

我希望上面的代码对你有帮助。

于 2012-10-25T08:43:29.463 回答
0

我想我在这里找到了解决方案。ByteArrayBuffer.append 的工作方式与我预期的不同。引用这个新文档:

无效附加(字符 [] b,int 关闭,int len)

      Appends len chars to this buffer from the given source array starting at index off.

从 char[]b 中的 index off 开始。追加总是在 ByteArrayBuffer 的末尾完成。

因此我的代码应该是这样的:

picRaw.append(intent.getExtras().
    getByteArray(BtConstants.btBytePayload), picRaw.length(), 
        intent.getExtras().getByteArray(BtConstants.btBytePayload).length);
于 2012-10-25T10:28:29.700 回答