Android- arduino蓝牙通信:Android应用无法正常接收数据



我正在创建一个Android-Arduino蓝牙串行通信应用程序。我能够成功地连接到arduino。我的应用程序可以发送数据到arduino没有麻烦,我已经验证了它。但是,当从arduino接收数据时,我的应用程序只接收发送的数据的一部分。例如,如果从arduino发送"404",我的应用程序只显示"4"作为接收。

我检查了其他这样的应用程序和所有其他应用程序都能够收到"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

可以看到,数据被接收到byte缓冲区,并使用new String(bytes);方法转换为字符串。无论如何,当我烤串,只有4被烤,而不是404从arduino发送。

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();
            }
        }
    }
}

Edit-2:在进一步的调试中,我发现mInput.available()返回0,而mInput.read(bytes)返回1 .为什么在我的arduino代码中,我使用bluetooth.println("404");

http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/

试试这个应该可以。也请提供完整的代码。可能是错误应该在您将函数连接到串行事件处理程序的部分。

我也有同样的问题。我通过添加分隔符(n)来解决,同时从arduino即println()发送并检查android代码。试试下面的代码在android,它的工作为我:

试试下面的代码:http://pastebin.com/sfb3kuu6

我通过在read()方法中添加延迟来解决这个问题。

最新更新