2013-02-11 23 views
2

在蓝牙应用程序中,我以字节[]的形式从外部设备接收数据。 然后将数据放入字节缓冲区以供进一步使用BufferUnderflowException

像下面所示

 ByteBuffer localByteBuffer = null; 
     byte[] arrayOfByte = new byte[4096]; 

      int bytes = 0; 
      Log.d("ZeoTest", "++++ Listening..."); 
      while (true) { 

       try { 
        bytes = in.read(arrayOfByte); 
        localByteBuffer = ByteBuffer.wrap(arrayOfByte); 

        localByteBuffer.get(arrayOfByte, 0, arrayOfByte.length); 

        Log.d("Zeo", "input :"+((localByteBuffer))); 
        String data =bytesToHex(arrayOfByte) ; 
        Log.d("Zeo", "input stream :"+(new String(data))); 
        Log.d("ZeoTest", "++++ Read "+ bytes +" bytes"); 

       } catch (IOException e) { 
        e.printStackTrace(); 
        try { Thread.sleep(0); } catch (InterruptedException ie) {} 
       } Log.d("ZeoTest", "++++ Done: test()"); 

       onReceive(localByteBuffer);} } 

在的onReceive梅索德检查头有效或无...

如下所示

public static boolean isValidHeader(EnhancedByteBuffer paramEnhancedByteBuffer) 
    { 
    boolean bool = false; 
    if (paramEnhancedByteBuffer.remaining() < 12); 
    while (true) 
    { 

     paramEnhancedByteBuffer.order(ByteOrder.LITTLE_ENDIAN); 
     int i = paramEnhancedByteBuffer.position(); 
     long l = paramEnhancedByteBuffer.getUint32(); 
     paramEnhancedByteBuffer.getUint16(); 
     int j = paramEnhancedByteBuffer.getUint8(); 
     int k = paramEnhancedByteBuffer.getUint8(); 
     paramEnhancedByteBuffer.getBoolean(); 
     paramEnhancedByteBuffer.getUint8(); 
     int m = paramEnhancedByteBuffer.getUint16(); 
     paramEnhancedByteBuffer.position(i); 
     if ((l == 1196641608L) && (j <= 2) && (k < 14) && (m == MessageType.getMessageContentSize(k))) 
     bool = true; 
      return bool; 
    } 
    } 

但在getUint32方法下获得缓冲区下流异常

什么可能是可能的原因...... ????

logcat的....

FATAL EXCEPTION: main 
java.nio.BufferUnderflowException 
at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:117) 
at com.myzeo.zeo.utility.EnhancedByteBuffer.getUint32(EnhancedByteBuffer.java:87) 
at com.myzeo.bluetooth.ZeoTest.isValidHeader(ZeoTest.java:195) 
at com.myzeo.bluetooth.ZeoTest.isValidHeader(ZeoTest.java:183) 
at com.myzeo.bluetooth.ZeoTest.onReceive(ZeoTest.java:153) 
at com.myzeo.bluetooth.ZeoTest.setup(ZeoTest.java:125) 
at com.myzeo.bluetooth.ZeoTest.access$0(ZeoTest.java:64) 
at com.myzeo.bluetooth.ZeoTest$1.onClick(ZeoTest.java:47) 
at android.view.View.performClick(View.java:2629) 
at android.view.View$PerformClick.run(View.java:9374) 
at android.os.Handler.handleCallback(Handler.java:587) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:3683) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
at dalvik.system.NativeStart.main(Native Method) 
+0

原因是当我得到数据到bytebuffer(例如:5ytes)..位置将是5 ...所以在传递bytebuffer访问数据字节时,按字节设置位置为0 ....也就是说''localByteBuffer。位置(0);' – Kiran 2013-03-15 03:54:44

回答

3

可能的原因是,你从缓冲器读取更多的字节比可用。在调试器中每次读取()之后检查缓冲器的位置,以查看哪个调用使缓冲器提前超过它应该(可能是readBoolean())

1

您没有考虑读取头的位置

如果你想得到一个整数值,并且读头位于容量为4的ByteBuffer的位置4,那么你将得到一个BufferUnderflowException,因为他不能再读取4个字节的整数值被退回。

你必须给他阅读的位置(非持久性):getInt(position);例如:getInt(0);