0

I have got list of paired devices of Bluetooth.Now i want to send text to that particular paired device.I have done code for that.

Below is the code:

private void init() throws IOException {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
    if (blueAdapter.isEnabled()) {
        Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

        if(bondedDevices.size() > 0) {
            Object[] devices = (Object []) bondedDevices.toArray();
            BluetoothDevice device = (BluetoothDevice) devices[position];
            ParcelUuid[] uuids = device.getUuids();
            BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
            socket.connect();
            outputStream = socket.getOutputStream();
            inStream = socket.getInputStream();
        }

        Log.e("error", "No appropriate paired devices.");
    } else {
        Log.e("error", "Bluetooth is disabled.");
    }
}
}

public void write(String s) throws IOException {
outputStream.write(s.getBytes());
}

public void run() {
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
int b = BUFFER_SIZE;

while (true) {
    try {
        bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

But i'm getting these Error:

 java.io.IOException: read failed, socket might closed or timeout, read      ret: -1
 04-05 10:53:41.356 5580-5580/? W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
 04-05 10:53:41.356 5580-5580/? W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:912)
 04-05 10:53:41.356 5580-5580/? W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:531)

So, please help me how to solve this error.

Mansi Bhatt
  • 214
  • 3
  • 12
  • Possible duplicate of [IOException: read failed, socket might closed - Bluetooth on Android 4.3](http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3) – Pratik Tank Apr 05 '17 at 05:34
  • No i'm running on marshmallow so that's not the issue. – Mansi Bhatt Apr 05 '17 at 06:03

1 Answers1

0

What kind of device are you trying to connect to?

-> Based on discussion: If trying to connect to another phone running Android there must be application which is accepting the connection. Working example is available from Google: BluetoothChat


You can try speeding up the connection process:

// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();

Or you can try using different UUID (are there more supported)?

System.out.println(uuids);

Or you can try using insecure connection:

device.createInsecureRfcommSocketToServiceRecord(...)

BTW: Your code for reading data from inpust stream won't work very well. Take a look at example from Google Chat Application: BluetoothChatService

Petr Šabata
  • 635
  • 6
  • 7