我无法接收蓝牙消息

时间:2016-03-14 11:28:11

标签: android bluetooth

我正在尝试从蓝牙板接收蓝牙消息,问题是我只收到某些消息...即" AT + QSPPSEND = 2,126"。

如果我尝试使用蓝牙终端应用程序,我会每隔5秒钟收到一次正确的消息,因此我每隔一分钟就收到一条消息。

我哪里出错了,那可运行的代码是否没有收到董事会发送的确切消息?

 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[10024];
    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();
}

1 个答案:

答案 0 :(得分:0)

没关系,我修好了..真的很奇怪但是它并不喜欢把它们中的任何一个放进文本视图中,我使用吐司并且消息看起来很好,谢谢你的帮助