1

I need to send a string to a bluetooth device. But while sending,I am getting an Exeption java.io.IOException: Transport endpoint is not connected on java.io.OutputStream.write(byte[]) method.

The Code Is as shown below. The code just search a specific device from the paired device list and send string.

public class MainActivity extends Activity {

    TextView out;
    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothAdapter btAdapter;
    private ArrayList<BluetoothDevice> btDeviceList = new ArrayList<BluetoothDevice>();
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
    private ArrayAdapter<String> mNewDevicesArrayAdapter;
    BluetoothDevice device1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        out = (TextView) findViewById(R.id.out);
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        ListpairedDevices();
    }

    /* This routine is called when an activity completes. */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ENABLE_BT) {
            CheckBTState();
        }
    }

    public void ListpairedDevices() {
        Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

        // If there are paired devices, add each one to the ArrayAdapter
        if (pairedDevices.size() > 0) {
            out.append("\nPaired Devices \n");
            for (BluetoothDevice device : pairedDevices) {
                out.append("\n  Device123: " + device.getName() + "," + device);
                //mPairedDevicesArrayAdapter.add("\n  Device123: " + device.getName() + "," + device);
                String dv=device.toString();
                if(dv.contains("00:1B:EE:82:31:1E"))
                {
                device1=device;
                }
            }
        } else {
            out.append("\nNo Pared Device");
        }
        out.append("\nDiscovered Devices");
    }

    public void sendtext(View v) {//button click

    Set<BluetoothDevice> bd=btAdapter.getBondedDevices();
    sendDataToPairedDevice("message1");
    }
    private void sendDataToPairedDevice(String message ){       
        byte[] toSend = message.getBytes();
        try {
         UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
         BluetoothSocket socket = device1.createInsecureRfcommSocketToServiceRecord(applicationUUID);
         OutputStream mmOutStream = socket.getOutputStream();
         mmOutStream.write(toSend);
     } catch (IOException e) {
         Log.e( "Exception during write", e.toString());
     }
 }
}
KIRAN K J
  • 632
  • 5
  • 28
  • 57

1 Answers1

2

Edit:

You need to connect to the socket, before that you need to cancel the discovery

 btAdapter.cancelDiscovery();
 socket.connect();

UUID worked for Kiran K J is

00001105-0000-1000-8000-00805F9B34FB


You Haven't get the Bluetooth Adapter, you have just declared it.

Get the bluetooth adapter like this using static factory method

Add this in onCreate

btAdapter=BluetoothAdapter.getDefaultAdapter();
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • this is the Set pairedDevices = btAdapter.getBondedDevices(); first line in your listpaireddevice() method – Pragnani Mar 08 '13 at 08:47
  • You've used adatper but you haven't get the adapter at that time – Pragnani Mar 08 '13 at 08:48
  • 1
    You've already declared your adatper but it is null because you haven't get object of it.. place the code I have posted in OnCreate method before calling listpaireddevices() method – Pragnani Mar 08 '13 at 08:59
  • it is not the problem.while copy paste,i forgot to add the line in stackoverflow.the error still same.is there any problem with the uuid.i just copy paste a junk uuid.but the device address is correct. – KIRAN K J Mar 08 '13 at 09:04
  • call socket.connect() after getting the socket – Pragnani Mar 08 '13 at 09:09
  • java.io.IOException: Service discovery failed on socket.connet(); – KIRAN K J Mar 08 '13 at 09:13
  • call this btAdapter.cancelDiscovery(); before connecting to socket – Pragnani Mar 08 '13 at 09:16
  • the same problem on socket.connect() – KIRAN K J Mar 08 '13 at 09:20
  • it seems there is a problem with UUID http://stackoverflow.com/questions/12091913/android-bluetooth-connection-service-discovery-failed – Pragnani Mar 08 '13 at 09:22
  • http://stackoverflow.com/questions/8515572/service-discovery-failed-from-android-bluetooth-insecure-rfcomm – Pragnani Mar 08 '13 at 09:23
  • yes i can send it send the string now.but i cannot receive on a simple bluetooth handset.just show an accept request on the device.....Thanks for your help and support.please update your answer with points for acceptance.the problems with my code are uuid and socket.connet().please update after edit.Thanks a lote.... – KIRAN K J Mar 08 '13 at 09:37
  • the working uuid is 00001105-0000-1000-8000-00805F9B34FB.update it too.`THANKS` – KIRAN K J Mar 08 '13 at 09:53