使用(ESP-32)读取的Android蓝牙数据不正确

时间:2019-02-25 20:24:20

标签: android arduino android-bluetooth

我正在使用ESP-32蓝牙通信,但是当我发送串行数据时,它会在android studio中显示垃圾数据。但是通过android向arduino发送消息就可以了。唯一的android的RX正在制造错误。我尝试了PlayStore上的另一个应用程序,它在RX和TX上都能正常工作

android Activity.java

 handler = new Handler() {
                public void handleMessage(android.os.Message msg) {

                    switch (msg.what) {
                        case RECIEVE_MESSAGE:                                                     // if receive massage
                            byte[] readBuf = (byte[]) msg.obj;
                            String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                           // Log.d(TAG, strIncom+"hhh");
                            sb.append(strIncom);                                                // append string
                            int endOfLineIndex = sb.indexOf("\n");                            // determine the end-of-line
                            if (endOfLineIndex > 0) {                                            // if end-of-line,
                                String sbprint = sb.substring(0, endOfLineIndex);
                               // sb.setLength(0);
                                // extract string
                                sb.delete(0, sb.length());                                      // and clear
                                textView.setText("Data from Arduino: " + sbprint );            // update TextView
                                Log.d(TAG, sbprint);
                            }
                            Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");

                            break;
                    }
                }
            };
private class ConnectedThread extends Thread {
        private final InputStream mInStream;
        private final OutputStream mOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mInStream = tmpIn;
            mOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[256];  // 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
                    bytes = mInStream.read(buffer);        // Get number of bytes and message in "buffer"
                    handler.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
                 //   handler.handleMessage(new Message());
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(String message) {
            Log.d(TAG, "...Data to send: " + message + "...");
            byte[] msgBuffer = message.getBytes();
            try {
                mOutStream.write(msgBuffer);
            } catch (IOException e) {
                Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
            }
        }
    }

    }

串行输入:您好 输出:有时是“ heo”“ hll”

谁能建议我我做错了什么地方

0 个答案:

没有答案