简体   繁体   中英

How to get two Android devices to connect to the same Bluetooth device without unpairing?

I have an app that I communicate with a Bluetooth device via SPP, and I made a discovery that when I try to connect to the same Bluetooth device using another Android device, the other Android device cannot connect to the Bluetooth device even when I close out of my app or remove power from the Bluetooth device. The only fix is unpairing the Bluetooth device. I am sure that I have closed all my sockets and sent the proper disconnect command to my Bluetooth device; I wonder why my second Android device won't connect to my Bluetooth device unless I unpair it.

Here is the code to connect:

public class ConnectTask extends AsyncTask {

private final WeakReference<Context> weakContext;
BluetoothDevice mdevice;
BluetoothSocket mSocket;
ProgressDialog pd;

public ConnectTask(Context context) {
    weakContext = new WeakReference<Context>(context);
}

@Override
protected void onPreExecute() {
    final Context context = weakContext.get();
    if (context != null) {
        super.onPreExecute();
        if (pd != null) {
            pd = null;
        }
        pd = new ProgressDialog(context);
        pd.setTitle("Connecting...");
        pd.setCancelable(false);
        if (!pd.isShowing()) {
            pd.show();
        }
    }
    BluetoothConnectionService.btAdapter.cancelDiscovery();
}

@Override
protected Void doInBackground(Void... params) {
    try {
        try {
            Thread.sleep(1000);
            mdevice = BluetoothConnectionService.getDevice();
            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

            mSocket = mdevice.createInsecureRfcommSocketToServiceRecord(uuid);

            mSocket.connect();
            BluetoothConnectionService.setSocket(mSocket);
            BluetoothConnectionService.sendMessage(mSocket, "S");

            Thread.sleep(1000);
            Log.i("BT", "Connected");
        } catch (InterruptedException e) {
            throw new IOException();
        }
    } catch (IOException e) {
        e.printStackTrace();
        try {
            Log.i("BT", "trying fallback...");

            mSocket = (BluetoothSocket) mSocket.getClass().getMethod("createInsecureRfcommSocket", new Class[]{int.class}).invoke(mdevice, 2);
            mSocket.connect();
            BluetoothConnectionService.setSocket(mSocket);
            BluetoothConnectionService.sendMessage(mSocket, "S");

            Thread.sleep(1000);
            Log.i("BT", "Connected");
        } catch (Exception e2) {
            Log.e("Error", "Couldn't establish Bluetooth connection!");
            try {
                if (mSocket != null) {
                    mSocket.close();
                }  else {
                    Log.e("Error", "Could not close socket!");
                }
            } catch (IOException e1) {
                Log.e("Error", "Could not close socket!");
            }
        }
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    final Context context = weakContext.get();
    if (context != null) {
        super.onPostExecute(result);
        try {
            if (pd != null) {
                if (pd.isShowing()) {
                    if (context instanceof Configuration) {
                        onCompleteConfiguration((Configuration) context);
                    } else if (context instanceof CollectingDetail) {
                        onCompleteCollectingDetail((CollectingDetail) context);
                    }
                    pd.dismiss();
                }
            }
        } catch (final IllegalArgumentException are) {
            Log.e("Error", "Illegal Argument Exception!");
        } finally {
            pd = null;
        }
    }
}

UPDATE: Turns out this problem is specific to some Android devices. The device that I am having this problem specifically is when using two Dragon Touch V10 tablets. Other devices I haven't had this problem. The Bluetooth device is based on an RN4677.

You are using non-exposed or deprecated methods.

While the newer Android have improved the Bluetooth communications, some bugs may linger, specially when using hidden methods. Ensure the communication is really closing, check the device, and that its actually disconnected, and set to go back to broadcasting mode, or that it accepts moving to a new Bluetooth Master

As far as Android certification goes, only the public declared methods must be implemented, the hidden might be, but may be buggy as hell.

Check this as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM