2015-07-10 84 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

相關問題