2015-07-10 54 views
1

我正在为Android-Arduino蓝牙串行通信创建应用程序。我能够成功连接到arduino。我的应用程序可以无障碍地将数据发送到arduino,并且我已验证它。但是,当从Arduino接收数据时,我的应用程序只接收一部分正在发送的数据。例如,如果从Arduino发送“404”,我的应用程序仅显示“4”正在接收。Android-Arduino蓝牙通信:在Android应用程序中未正确接收数据

我检查了其他类似的应用程序,所有其他应用程序都能够接收“404”本身。所以问题在于我的代码。

这是我的代码从Arduino的读取数据:

public String read(byte[] bytes){ 
      try { 
       mInput.read(bytes); 
       strInput = new String(bytes); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return strInput; 
} 
//mInput is the input stream of bluetooth connection 

正如你可以看到数据recived到byte缓冲器,并转换为使用new String(bytes);方法的字符串。如何当我烤面包串,只有4被烤,而不是从arduino发送404

byte缓冲区的大小为256

编辑:按要求完整代码BluetoothManager.java是这样的:

public class BluetoothManager { 
    private BluetoothAdapter bluetoothAdapter; 
    private BluetoothDevice bluetoothDevice; 
    private BluetoothSocket bluetoothSocket; 
    private ConnectedThread connectedThread; 
    private byte[] buffer; 

    public BluetoothManager(){ 
     buffer=new byte[256]; 
     bluetoothSocket=null; 
     bluetoothAdapter=null; 
     bluetoothDevice=null; 
     connectedThread=null; 
     getBluetoothAdapter(); 
     if(!isBluetoothAvailable()){ 
      turnBluetoothOn(); 
     } 
     scanToConnect(); 
    } 
    public void turnBluetoothOff(){ 
     try { 
      bluetoothSocket.close(); 
      bluetoothSocket=null; 
      bluetoothAdapter.cancelDiscovery(); 
      bluetoothAdapter.disable(); 
      bluetoothAdapter=null; 
      bluetoothDevice=null; 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    private boolean isBluetoothAvailable(){ 
     return bluetoothAdapter.isEnabled(); 
    } 
    private void turnBluetoothOn(){ 
     bluetoothAdapter.enable(); 
    } 
    public String readData(Context context){ 
     String outputString=null; 
     if(isBluetoothAvailable()) { 
      outputString = connectedThread.read(buffer); 
     }else{ 
      Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show(); 
     } 
     return outputString; 
    } 
    public void writeData(String string, Context context){ 
     if(isBluetoothAvailable()) { 
      connectedThread.write(string.getBytes()); 
     }else{ 
      Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show(); 
     } 
    } 
    private void getBluetoothAdapter(){ 
     try{ 
      bluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    private void scanToConnect(){ 
     Set<BluetoothDevice> pairedDevices=bluetoothAdapter.getBondedDevices(); 
     if(pairedDevices.size()>0){ 
      try { 
       for (BluetoothDevice device : pairedDevices) { 
        if (device.getName().equals("HC-05")) { 
         bluetoothDevice = device; 
         new connectBt(bluetoothDevice); 
         break; 
        } 
       } 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
    private class connectBt extends Thread { 
     public connectBt(BluetoothDevice device) { 
      BluetoothSocket tmp = null; 
      bluetoothDevice = device; 
      UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
      try { 
       tmp = device.createRfcommSocketToServiceRecord(uuid); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      bluetoothSocket = tmp; 
      run(); 
     } 
     public void run() { 
      bluetoothAdapter.cancelDiscovery(); 
      try { 
       bluetoothSocket.connect(); 
       connectedThread = new ConnectedThread(bluetoothSocket); 
      } catch (IOException connectException) { 
       closeSocket(); 
      } 
     } 
     private void closeSocket() { 
      try { 
       bluetoothSocket.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    private class ConnectedThread extends Thread{ 
     private InputStream mInput=null; 
     private OutputStream mOutput=null; 
     private String strInput; 

     public ConnectedThread(BluetoothSocket socket){ 
      bluetoothSocket=socket; 
      InputStream tmpIn=null; 
      OutputStream tmpOut=null; 
      try{ 
       tmpIn=socket.getInputStream(); 
       tmpOut=socket.getOutputStream(); 
      }catch(IOException e){ 
       e.printStackTrace(); 
       closeSocket(); 
      } 
      mInput=tmpIn; 
      mOutput=tmpOut; 
     } 
     public void write(byte[] bytes){ 
      try{ 
       mOutput.write(bytes); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 
     public String read(byte[] bytes){ 
      try { 
       mInput.read(bytes); 
       strInput = new String(bytes); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return strInput; 
     } 
     public void closeSocket(){ 
      try{ 
       bluetoothSocket.close(); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

编辑-2:在进一步的调试,我发现,mInput.available()回报0mInput.read(bytes)回报1。为什么是这样的行为,而在我的Arduino代码我使用bluetooth.println("404");

回答

0

欧凯我通过将延迟在我read()方法解决了这个问题。

+0

你能粘贴完整的代码吗? –

0

我遇到了同样的问题。我通过在从arduino(即println())发送并检入android代码时添加分隔符(\ n)来解决此问题。尝试以下android的代码,它的工作对我来说:

试试这个代码:http://pastebin.com/sfb3kuu6

+0

对不起,延迟响应。但我在我的arduino代码中添加了分隔符,并在我的代码中添加了部分代码的读取方法。但我没有得到任何关于你的代码。在调试时,我发现'InputStream.available()'返回0,并且'InputStream.read(bytes)'在我的代码中返回1。为什么是这种行为。 – Vivek

相关问题