Android-Arduino蓝牙通信:Android应用中未正确接收数据

时间:2015-07-10 11:09:07

标签: android bluetooth arduino

我正在为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被烘烤,而不是从{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");

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我遇到了同样的问题。我通过添加分隔符(\ n)解决了从arduino发送,即println()和检查Android代码。尝试下面的android代码,它为我工作:

试试此代码:http://pastebin.com/sfb3kuu6

答案 2 :(得分:0)

Okey我通过延迟read()方法解决了这个问题。