简体   繁体   English

如何使用 SPP 连接多个蓝牙设备?

[英]How to connect Multiple Bluetooth Device by using SPP?

I am creating an Android APP which could connect the two BT device and do the communication over SPP.我正在创建一个 Android APP,它可以连接两个 BT 设备并通过 SPP 进行通信。 To create such application I am following the simple logic.为了创建这样的应用程序,我遵循简单的逻辑。

I got the two mac addressed of the BT Device, so in a for loop I am connecting the BT Devices as below:我得到了 BT 设备的两个 mac 地址,因此在 for 循环中我按如下方式连接 BT 设备:

private void connect() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String[] strDevice = deviceAddress.split(",");
    BluetoothDevice device = null;
    String currentConnection;
    String deviceName = "";
    try {
        disconnect();
        Thread.sleep(500);

        for (String item: strDevice) {
            Log.d(TAG,"item is: " + item + " size is: " + strDevice.length);
            currentConnection = item;
            device = bluetoothAdapter.getRemoteDevice(item);
            deviceName = device.getName() != null ? device.getName() : device.getAddress();
            status("connecting..." + item);
            connected = Connected.Pending;
            socket = new SerialSocket();
            service.connect(this, "Connected to " + deviceName);
            socket.connect(getContext(), service, device);

            Thread.sleep(500);
        }


    } catch (Exception e) {
        onSerialConnectError(e);
    }
}

Using above code, I am able to connect the two BT devices.使用上面的代码,我可以连接两个 BT 设备。 But the problem is when I close the activity I disconnect them but at that time only one device disconnects.但问题是当我关闭活动时,我断开了它们的连接,但当时只有一台设备断开连接。 I am calling the disconnect on "OnDestroy" of the fragment:我在片段的“OnDestroy”上调用断开连接:

@Override
public void onDestroy() {
    if (connected != Connected.False) {
        disconnect();
    }
    getActivity().stopService(new Intent(getActivity(), SerialService.class));
    super.onDestroy();
}

void disconnect() {
    listener = null; // ignore remaining data and errors
    connected = false; // run loop will reset connected
    if(socket != null) {
        try {
            socket.close();
        } catch (Exception ignored) {
        }
        socket = null;
    }
    try {
        context.unregisterReceiver(disconnectBroadcastReceiver);
    } catch (Exception ignored) {
    }
}

I need a help to find out why on disconnect, only one device is disconnecting?我需要帮助找出断开连接的原因,只有一个设备断开连接? Do I need to close two sockets because during connection it opened 2 sockets for two devices?我是否需要关闭两个套接字,因为在连接期间它为两个设备打开了 2 个套接字? If yes, how can I close two sockets?如果是,如何关闭两个套接字?

Thanks in Advance提前致谢

Here's the solution:这是解决方案:

The issue was described above that on socket disconnect only one of the BT device was disconnecting.上面描述的问题是,在套接字断开时,只有一个 BT 设备断开连接。 To resolve the situation, what I did is, I saved the socket in a concurrent list whenever it's created and connected.为了解决这种情况,我所做的是,无论何时创建和连接套接字,我都将它保存在并发列表中。

So when the disconnect happens, I disconnected all the sockets from the list which are connected.因此,当断开连接发生时,我从连接的列表中断开了所有套接字。 This way it disconnects all the BT device.这样它就会断开所有 BT 设备的连接。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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