Programatically send text using bluetooth socket to another bluetooth device in android

705 views Asked by At

For sending data to another Bluetooth device programmatically, I am using Bluetooth socket code.Whenever I called bluetoothSocket.connect(); It gives connection refused or connection to peer Exception. I have one more confusion that which UUId I have to use in it or from where i can get it.Below is the snippet of code.

public void openBT() {
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard
                                                                                // SerialPortService

        try {

            mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid);
            Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            mmSocket = (BluetoothSocket) m.invoke(mmDevice, 3);
            mBluetoothAdapter.cancelDiscovery();
            mmSocket.connect();
            /*mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

            mmSocket.connect();*/
            mmOutputStream = mmSocket.getOutputStream();
            mmInputStream = mmSocket.getInputStream();

            beginListenForData();

            myLabel.setText("Bluetooth Opened");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Help Appreciated..!!

0

There are 0 answers