簡體   English   中英

藍牙SPP(串行)故障(Android)

[英]Bluetooth SPP (serial) glitchs (Android)

我遇到一個奇怪的問題。 我編寫了一個應用程序,它將與arduino建立藍牙SPP鏈接。 Arduino上的藍牙設備配置為9600波特。 我可以從arduino接收數據,但是似乎我收到了一些值為0或高峰值的故障。 這很煩人,因為我確實需要圖形部分的精確值,並且我知道arduino發送好的數據是因為我將其發送的內容記錄在文件中。

我正在尋找解決或理解為什么會發生這種情況的方法,而不是創建平均值或類似的方法來制作“補丁”。

謝謝你的幫助。

這是一張可以解釋我的問題的圖片,arduino數據范圍在101到103之間:

屏幕截圖

這是我創建連接並接收數據的代碼:

private class ConnectedThread extends Thread {
    private final DataInputStream mmInStream;
    private final DataOutputStream mmOutStream;

    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) { }

        mmInStream = new DataInputStream(tmpIn);
        mmOutStream = new DataOutputStream(tmpOut);
    }

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

private void connectDevice() {


    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    try {
        btSocket = createBluetoothSocket(device);
    } catch (IOException e) {
        errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }


    mBluetoothAdapter.cancelDiscovery();


    // Establish the connection.  This will block until it connects.
    Log.d(TAG, "...Connecting...");
    try {
      btSocket.connect();
      Log.d(TAG, "....Connection ok...");
      // Create a data stream so we can talk to server.
      Log.d(TAG, "...Create Socket...");

      mConnectedThread = new ConnectedThread(btSocket);
      mConnectedThread.start();
      mActionBar.setSubtitle("Connecté");

      //If fail, we disconnect or display an error warning regarding the situation
    } catch (IOException e) {
      try {
        btSocket.close();
        mActionBar.setSubtitle("Deconnecté");
      } catch (IOException e2) {
        errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
      }
    }

    return;
    }  

最后是處理程序:

hBluetooth = new Handler() {
    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
        case RECEIVE_MESSAGE:                                                   // If we receive a message
            byte[] readBuf = (byte[]) msg.obj;
            String stringIncome = new String(readBuf, 0, msg.arg1);             // Create string from byte array
            stringBuilder.append(stringIncome);                                             
            int endOfLineIndex = stringBuilder.indexOf("\r\n");                 // Determine the end-of-line
            if (endOfLineIndex > 0) {                                           // If we are at the end-of-line we parsed all the data that was sent
                rmsgBluetooth = stringBuilder.substring(0, endOfLineIndex);     // The string is extracted in a string object rmsgBluetooth
                stringBuilder.delete(0, stringBuilder.length());                


                if(btSocket != null && btSocket.isConnected()){                 


                //Here we send the value of the string to a txtbox  
                txtArduino.setText("Arduino: " + rmsgBluetooth); 




                if(rmsgBluetooth.matches("-?\\d+(\\.\\d+)?")) {                
                    try{

                   sensorReading = Float.parseFloat(rmsgBluetooth);
                    }catch(NumberFormatException e){


                    }
                }   

我認為您的錯誤很可能是在解析字符串時出現的。 嘗試仔細調試線路

           if(rmsgBluetooth.matches("-?\\d+(\\.\\d+)?")) {                
                try{

               sensorReading = Float.parseFloat(rmsgBluetooth);
                }catch(NumberFormatException e){


                }
            }

暫無
暫無

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

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