Android蓝牙接收器意外的行为

时间:2014-03-21 08:56:52

标签: android bluetooth camera

我想通过蓝牙流式传输相机帧。我设法让每个帧不超过4 KB。问题是,当我得到那个外面的时候(真实的)循环我得到了日志"完成接收"但屏幕上没有显示任何内容,程序滞后。如果我删除那个(真)在外面我成功接收并显示第一帧。但我想继续接受。

在发件人方面,我会在某些按钮上发送帧。我也尝试过连续发送,但也没有工作。

相机接收器线程:

public void run() {
    ByteArrayOutputStream dataOutputStream = new ByteArrayOutputStream();
    byte[] Size = new byte[4];
    boolean waitingForHeader = true;
    ProgressData progressData = new ProgressData();

    //while(true)
    //{
        while(true)
        {
            try{
                if(waitingForHeader)
                {
                    mmInStream.read(Size, 0, 4);
                    Log.d(TAG, "Received Size " + Utils.byteArrayToInt(Size) );
                    progressData.totalSize = Utils.byteArrayToInt(Size);
                    progressData.remainingSize = progressData.totalSize;
                    Log.d(TAG, "Data size: " + progressData.totalSize);
                    waitingForHeader = false;
                } else {
                    // Read the data from the stream in chunks
                    byte[] buffer = new byte[4000];
                    Log.d(TAG, "Waiting for data.  Expecting " + progressData.remainingSize + " more bytes.");
                    int bytesRead = mmInStream.read(buffer);
                    Log.d(TAG, "Read " + bytesRead + " bytes into buffer");
                    dataOutputStream.write(buffer, 0, bytesRead);
                    progressData.remainingSize -= bytesRead;

                    if (progressData.remainingSize <= 0) {
                        Log.d(TAG, "Expected data has been received.");
                        break;
                    }
                }
            }
            catch(Exception E)
            {
                Log.v(TAG, E.getMessage());
                break;
            }
        //}
        if(mmSocket != null)
        {
            final byte[] data = dataOutputStream.toByteArray();

            Log.d(TAG, String.valueOf(Utils.getDigest(data)));

            mImage.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));

            Log.d(TAG, "DoneReceiving");

            waitingForHeader = true;
        }
        else
        {
            Log.v(TAG, "Socket Closed");
            break;
        }
    }
}

0 个答案:

没有答案