0

I am trying to connect my phone to an HC06 as part of a robotics project but when I try to connect, it fails around 75% of the time and often fails to send the full message. Here is the relevant code:

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

It is mostly based on http://www.instructables.com/id/Android-Bluetooth-Control-LED-Part-2/?ALLSTEPS

The main question is am I connecting in the right way for a short-term connection down which I am sending single bytes of data?

EDIT: Here is the error it throws

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)

Anthony
  • 1
  • 3
  • Thanks for the edit cricket_007. It's my first question so I don't know how to do these things. – Anthony Mar 05 '17 at 18:38
  • A bit of explanation : Savefile is an arraylist I want to send. I think the problem is either with the doInBackground or just because my phone is too rubbish. (the phone is an Alcatel Pixi 3) – Anthony Mar 06 '17 at 07:38
  • edited : the previous link was to the wrong tutorial. now fixed that. – Anthony Mar 06 '17 at 17:41
  • Having looked at the forums, I'm still confused but I think it's related to http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3 could someone please explain this – Anthony Mar 06 '17 at 18:12
  • I have tried the method from the above post but the trace still returns "read ret: -1" rather than 1, as it should do. – Anthony Mar 06 '17 at 20:38

1 Answers1

0

Sorry for wasting your time, the problem was with the bluetooth module, not the code. I found this by checking with an existing bluetooth app.

EDIT : Having ordered another one, I am having the same problem connecting.

Anthony
  • 1
  • 3