Java Android bluetooth配对、连接,但从未向TextView接收数据

Java Android bluetooth配对、连接,但从未向TextView接收数据,java,android,bluetooth,arduino,Java,Android,Bluetooth,Arduino,我正在尝试从非android蓝牙设备arduino和HC-06接收数据字符串。arduino内部的代码看起来很好,它在一个连续循环中发送一个字符串,我可以使用串行监视器看到它。 但当我尝试将它与我的android手机一起使用时,它会配对、连接并在祝酒词中显示Connect,但我始终无法接收它的字符串并将其显示在文本视图中。如果我做错了什么,请告诉我 我的活动类中的代码,onCreate方法中的代码: public void handleMessage(Message msg) {

我正在尝试从非android蓝牙设备arduino和HC-06接收数据字符串。arduino内部的代码看起来很好,它在一个连续循环中发送一个字符串,我可以使用串行监视器看到它。 但当我尝试将它与我的android手机一起使用时,它会配对、连接并在祝酒词中显示Connect,但我始终无法接收它的字符串并将其显示在文本视图中。如果我做错了什么,请告诉我

我的活动类中的代码,onCreate方法中的代码:

public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what)
            {
            case SUCCESS_CONNECT:
                ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
                Toast.makeText(getApplicationContext(), "Connect", 0).show()

                break;

            case MESSAGE_READ:
                byte[] readBuf = (byte[])msg.obj;
                String string = new String(readBuf);
                Toast.makeText(getApplicationContext(), string, 0).show();              
                tvTest.setText(string);
在ConnectedThread类内部:

public void run() {
        byte[] buffer;  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                buffer = new byte[1024];
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }

我打电话解决了这个问题

connectedThread.start();
之后,

ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);

连接成功案例中的线路

选中Toast.makeTextgetApplicationContext,string,0.show;我想这只是一个显示烤面包持续时间的问题?加上Toast对成功很有用\u CONNECT可能它永远不会出现在您阅读的案例消息中执行其操作尝试一下。因为你没有为你的祝酒词定义时间,也许它没有被显示出来。如果这不起作用,我们应该更深入地观察。我不认为是这样的顺便说一句,我试图删除所有的代码,以防信息_阅读,并保持只有tvetest.setTextMessage阅读;但是我的文本视图没有改变。我假设从未调用case MESSAGE_READ。在connectedThread类的run方法中放置一个断点,在活动的handleMessage方法中放置另一个断点,并对其进行调试。查看代码是否经过它,直到消息读取为止。我们将尝试一步一步地定位问题,但首先尝试这样做