【问题标题】:Buffered Reader for Android Streaming from Bluetooth用于从蓝牙流式传输的 Android 缓冲阅读器
【发布时间】:2015-08-10 07:11:33
【问题描述】:

大家好,我正在尝试从蓝牙设备中读取一个流,连续流式传输整数,如下所示:

-11
121
123
1234
-11

我使用我在网上找到的一些代码进行了所有处理,但是要进行一些处理,数字需要是整数而不是字符串,parseInt 占用了太多 CPU,我尝试使用缓冲流但无济于事.

这是当前的方法:

 void beginListenForData()
        {
final Handler handler = new Handler(); 
  final byte delimiter = 10; //This is the ASCII code for a newline character

        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable()
        {
            public void run()
            {                
               while(!Thread.currentThread().isInterrupted() && !stopWorker)
               {
                    try 
                    {
                        int bytesAvailable = mmInputStream.available();                        
                        if(bytesAvailable > 0)
                        {
                            byte[] packetBytes = new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);
                            for(int i=0;i<bytesAvailable;i++)
                            {
                                byte b = packetBytes[i];
                                if(b == delimiter)
                                {
                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;

                                    handler.post(new Runnable()
                                    {
                                        public void run()
                                        {
                                            myLabel.setText(data);
                                        }
                                    });
                                }
                                else
                                {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }
                    } 
                    catch (IOException ex) 
                    {
                        stopWorker = true;
                    }
               }
            }
        });

        workerThread.start();
    }

如果有区别,数据来自 Arudino,我可以修改它的流式传输方式。

谢谢!

【问题讨论】:

    标签: java android bluetooth stream arduino


    【解决方案1】:

    使用包裹BufferedInputStreamDataInputStreamreadInt() 方法。当然,这假定网络字节顺序为整数。

    忘记所有这些arraycopy() 的东西。

    【讨论】:

    • 我试过用这个:final BufferedInputStream inputStream = new BufferedInputStream(mmInputStream); final DataInputStream dataInputStream = new DataInputStream(inputStream);仍然没有出现
    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多