从Android手机连接到HC-06几乎总是失败

时间:2017-03-05 18:30:18

标签: android bluetooth arduino

我正在尝试将手机连接到HC06作为机器人项目的一部分但是当我尝试连接时,它在大约75%的时间内失败并且经常无法发送完整的消息。以下是相关代码:

public class send extends AsyncTask<Void, Void, Void> {
    BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
    BluetoothSocket BS;
    boolean Success = false;
    @Override
    protected void onPreExecute(){
        Toast.makeText(getApplicationContext(), "Connecting", Toast.LENGTH_SHORT).show();
    }
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            BluetoothDevice wantedDevice = BA.getRemoteDevice("20:16:07:27:71:97");
            UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
            BS = wantedDevice.createInsecureRfcommSocketToServiceRecord(myUUID);
            BA.cancelDiscovery();
            BS.connect();
            Success = true;
        } catch (IOException e) {
            e.printStackTrace();
            Success = false;
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result){
        OutputStream mmOutStream = null;
        if(Success) {
            try {
                mmOutStream = BS.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < Savefile.size(); i++) {
                String message = Savefile.get(i);
                if (!Objects.equals(message, null) && mmOutStream != null && !Objects.equals(message, " ") && !Objects.equals(message, "x")) {
                    try {
                        mmOutStream.write(message.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (mmOutStream != null) {
                try {
                    mmOutStream.write("e".getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Toast.makeText(getApplicationContext(), "Sent", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Connection Failed", Toast.LENGTH_SHORT).show();
        }
        if (mmOutStream != null) {
            try {
                mmOutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(BS != null){
            try {
                BS.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        final ImageButton sendButton = (ImageButton) findViewById(R.id.sendbutton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(BA != null) {
                    sendButton.setOnClickListener(null);
                    send a = new send();
                    a.execute();
                }
                else{
                    Toast.makeText(getApplicationContext(), "Bluetooth is off", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

主要基于http://www.instructables.com/id/Android-Bluetooth-Control-LED-Part-2/?ALLSTEPS

主要问题是我是否以正确的方式连接短期连接,我发送单字节数据?

编辑:这是它抛出的错误

W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:508) W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:519) W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:323) W/System.err: at com.croweinc.iconturtle.MainActivity$send.doInBackground(MainActivity.java:246) W/System.err: at com.croweinc.iconturtle.MainActivity$send.doInBackground(MainActivity.java:232) W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) W/System.err: at java.lang.Thread.run(Thread.java:848)

1 个答案:

答案 0 :(得分:0)

很抱歉浪费你的时间,问题在于蓝牙模块,而不是代码。我通过检查现有的蓝牙应用程序找到了这个。

编辑:订购了另一个,我遇到了同样的问题。