连续读取缓冲区停止App

发布于 2024-11-03 18:35:16 字数 880 浏览 1 评论 0原文

我的应用程序从蓝牙设备接收源源不断的数据流。 我正在 while(true) 循环中读取此流,并且可以在日志中看到读取的数据。 问题是,我的设备不再响应。有没有一种(希望)简单的方法让应用程序在后台读取流?

谢谢! 基督教。

@博尔德: 抱歉,我不太明白 AsynkTask 类。 :( 你能帮我把这段代码放在后台吗? 非常感谢!

                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);
                }

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);
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-11-10 18:35:16

可能这会有所帮助 http://android-developers.blogspot.com /2009/05/painless-threading.html

处理程序示例:

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);
    }
};

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);
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文