Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
蓝牙SPP(串行)故障(Android)_Android_Sockets_Bluetooth_Spp - Fatal编程技术网

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

蓝牙SPP(串行)故障(Android),android,sockets,bluetooth,spp,Android,Sockets,Bluetooth,Spp,我遇到了一个奇怪的问题。我已经编写了一个应用程序,可以与arduino建立蓝牙SPP链接。Arduino上的蓝牙设备配置为9600波特。我可以从arduino接收数据,但似乎我收到了一些值为0或高峰值的小故障。这很烦人,因为我确实需要图形部分的精确值,而且我知道arduino发送的数据很好,因为我将它发送的数据记录在一个文件中 我希望修复或理解为什么会发生这种情况,而不是创建一个平均值或类似的值来生成一个“补丁” 谢谢你的帮助 这张图片可以解释我的问题,arduino数据范围约为101到103:

我遇到了一个奇怪的问题。我已经编写了一个应用程序,可以与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){


                }
            }

谢谢你的回复!但即使是rmsgBluetooth字符串也有这个问题。。。所以基本上问题出在别处。当我试图解析所有字节以生成一个字符串时,它可能位于字符串生成器区域,但我找不到错误。