簡體   English   中英

Android上的藍牙錯誤InputStream

[英]Error InputStream in Bluetooth on Android

我正在編寫一個需要與Arduino HC-06藍牙交換數據的應用程序。 這是數據交換方法:

void ListenForBluetoothData()
{
    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 {
                    if(BTinStream!=null)
                    {
                    int bytesAvailable = BTinStream.available();
                    if (bytesAvailable > 0) {
                        byte[] packetBytes = new byte[bytesAvailable];
                        BTinStream.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()
                                    {
                                        myLabel1.setText(data);
                                    }
                                });
                            } else {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }
                  }else {
                        continue;
                    }
                }
                catch (IOException ex)
                {
                    stopWorker = true;
                }
            }
        }
    });

    workerThread.start();
}

我收到的主要錯誤是:

06-19 11:21:23.011 17542-17910/com.example.farok.bluetoothcommunicationarduino E/AndroidRuntime: FATAL EXCEPTION: Thread-41375
Process: com.example.farok.bluetoothcommunicationarduino, PID: 17542
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.<init>(Handler.java:200)
    at android.os.Handler.<init>(Handler.java:114)
    at com.example.farok.bluetoothcommunicationarduino.MainActivity.ListenForBluetoothData(MainActivity.java:179)
    at com.example.farok.bluetoothcommunicationarduino.MainActivity$1.run(MainActivity.java:69)

請幫忙,我嘗試了許多解決方案,但沒有用。

您是從工作線程向UI發送數據,而不是使用Handler ,而是編輯和使用runOnUiThread

代替:

handler.post(new Runnable()
{
   public void run()
   {
      myLabel1.setText(data);
   }
});

用這個:

runOnUiThread(new Runnable(){
   public void run() {
      myLabel1.setText(data);
   }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM