Android bluetooth createInsecureRfcommSocketToServiceRecord to reconnect

When I try to connect for the first time and the server device is in range - everything is fine!

But, if the device is not available (service discovery failed), my BT keeps this SDP record and doesn't want to connect anymore. I really think it's pretty silly to cache this, it looks like "Oh, we didn't find this UUID on this device. It never exists in the entire world to remember this forever."

Only if I change the UUID or address, please try again. But I cannot close the old one and open the new UUID listening port anytime the device is not found on the client side.

How can I tell him "Clear this stupid entry" or something like that.

PS: I need API10, so I use "bConnected" instead of isConnected ();

Client code:

public void fire(View view) {
    final BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
    final String sUuid = "SuperHidenUuid";
    final UUID uuid = UUID.nameUUIDFromBytes(sUuid.getBytes());

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            OutputStream sOut;
            InputStream sIn;
            BluetoothSocket sConnection;
            BA.cancelDiscovery(); //shotgun debuging

            BluetoothDevice BD = BA.getRemoteDevice(sVictimBsid);
            try {
                sConnection = BD.createInsecureRfcommSocketToServiceRecord(uuid);
            } catch (IOException e) {
                return;
            }

            boolean bConnected = false;
            try {
                sConnection.connect();
                bConnected = true;
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (bConnected) {
                    sIn = sConnection.getInputStream();
                    sOut = sConnection.getOutputStream();
                    sOut.write((sVictimPass + "\n").getBytes());
                    BufferedReader reader = new BufferedReader(new InputStreamReader(sIn));

// here I send and receive some data

                    sIn.close(); //shotgun debuging
                    sOut.close(); //shotgun debuging
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(1000); //shotgun debuging
                sConnection.close();
                BA.cancelDiscovery(); //shotgun debuging
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    t.start();
}

      

+3


source to share





All Articles