0

I am just trying to open socket with RN-41 microchip, as far as I know the chip listens for incoming connections all the time, is discoverable, etc.. Why do socket gets always closed directly?

private class Connect extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public Connect(BluetoothDevice device) {

            BluetoothSocket tmp = null;
            mmDevice = device;
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("EB46DDA9-0D00-4C34-9365-D6AA6C111D1C"));
                Log.v("SOCKET SUCCESS", "HAST SOCKET"); 
            } catch (IOException e) { }
            mmSocket = tmp;
        }

        public void run() {


            try {

                mmSocket.connect();
                Log.v("SOCKET SUCCESS", "VERBUNDEN");
            } catch (IOException connectException) {

                Log.v("SOCKET SUCCESS", "KEINE VERBINDUNG");
                try {
                    mmSocket.close();
                    Log.v("SOCKET SUCCESS", "SOCKET CLOSED");
                } catch (IOException closeException) { 
                    Log.v("SOCKET SUCCESS", "SOCKET CLOSE FAIL");
                }
                return;
            }
        }

1 Answers1

0

I've been googling all day long and got things work. Unfortunately I still dont know why and how it works, but it works perfectly. I changed my Connect class constructor code like this:

public Connect(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            //try {
                // MY_UUID is the app's UUID string, also used by the server code
                //ParcelUuid[] ids = device.getUuids();
                //UUID deviceID = ids[0].getUuid();
                //tmp = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));//deviceID);//UUID.fromString("EB46DDA9-0D00-4C34-9365-D6AA6C111D1C"));

                Method m = null;
                try {
                    m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
                try {
                    tmp = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Log.v("SOCKET SUCCESS", "HAST SOCKET");
            //} catch (IOException e) { }
                mmSocket = tmp;
        }

Source:

Android Bluetooth SPP with Galaxy S3

P.s. If somebody would have a bit time to explain code above, I would appreciate it a lot. Thank you.

Community
  • 1
  • 1