【问题标题】:How to get two Android devices to connect to the same Bluetooth device without unpairing?如何在不取消配对的情况下让两个 Android 设备连接到同一个蓝牙设备?
【发布时间】:2019-12-31 01:59:10
【问题描述】:

我有一个通过 SPP 与蓝牙设备通信的应用程序,我发现当我尝试使用另一个 Android 设备连接到同一蓝牙设备时,即使在我关闭了我的应用程序或断开了蓝牙设备的电源。唯一的解决方法是取消配对蓝牙设备。我确信我已经关闭了所有的套接字并向我的蓝牙设备发送了正确的断开命令;我想知道为什么我的第二台 Android 设备无法连接到我的蓝牙设备,除非我取消配对。

这里是连接代码:

公共类 ConnectTask 扩展 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;
        }
    }
}

更新:原来这个问题是特定于某些 Android 设备的。我遇到此问题的设备具体是在使用两台 Dragon Touch V10 平板电脑时。其他设备我没有这个问题。蓝牙设备基于 RN4677。

【问题讨论】:

  • 你连接的设备是什么?
  • 蓝牙设备基于 RN4677。

标签: android bluetooth rfcomm spp bluetooth-socket


【解决方案1】:

您正在使用未公开或已弃用的方法。

虽然较新的 Android 改进了蓝牙通信,但一些错误可能会持续存在,尤其是在使用隐藏方法时。确保通信真正关闭,检查设备,并且它实际上已断开连接,并设置为返回广播模式,或者它接受移动到新的蓝牙主机

就 Android 认证而言,只有公开声明的方法必须实现,隐藏的可能是,但也可能是错误的。

Check this as well

【讨论】:

  • 请注意,如果公共方法失败,我只使用反射作为后备。但是,这两种方法都失败了,这似乎是特定于设备的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多