1

我有一个通过 SPP 与蓝牙设备通信的应用程序,我发现当我尝试使用另一个 Android 设备连接到同一个蓝牙设备时,即使我关闭,另一个 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。

4

1 回答 1

-1

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

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

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

也检查一下

于 2019-08-29T00:37:19.750 回答