开发者

Continuously read buffer stops App

My App recieves a neverending datastream from a bluetooth device. I am reading this stream in a while(true) loop and can see the read data in my log. The problem is, that my device is not responding anymore. Is there a (hopefully) simple way to let the application read the stream in the background?

Thanks! Christian.

@boulder: Sorry, I don't really understand that AsynkTask class. :( Can you please help me with this code putting it in the background? Thank you very much!

                try {
                while (true) 
                {               
                    read = isBT.read(msgBuffer);
                    connected = true;
                    StringBuilder strBuffer = new StringBuilder();
                    for (int i = 0; i<read; i++) 
                    {
                        int b = msgBuffer[i];
             开发者_高级运维           strBuffer.append(b);
                    }
                    Log.d(TAG,"++++++ Read "+ read + " Bytes: " + strBuffer.toString());
                }
            }
                catch (IOException e) {
                    Log.d(TAG," +++ IOException ++++", e);
                }


May be this will be helpful http://android-developers.blogspot.com/2009/05/painless-threading.html

Handler example:

private static final String CONTENT_TAG = "content";

// Call this from datastream thread to post data 
private void postProgress(String aBufferContent) {
    // Wrapping data in bundle
    final Bundle bundle = new Bundle();
    bundle.putString(CONTENT_TAG, aBufferContent);

    // Sending message to handler
    final Message message = mProgressHandler.obtainMessage();
    message.setData(bundle);
    mProgressHandler.sendMessage(message);
}

// This will be executed in UI thread. Do you GUI update job here
private final Handler mProgressHandler = new Handler() {
    public void handleMessage(Message msg) {
        final String streamContent = msg.getData().getString(CONTENT_TAG);
        myTextView.setText(streamContent);
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜