本文介绍了Android BLE多个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用程序,该应用程序可以连接并接收来自多个蓝牙低能耗设备的通知.我想知道如何实现这一目标.每个连接是否需要一个单独的线程?考虑到API的异步性质,如何确保能够按发现的顺序发现服务并设置通知.我目前正在使用此处提供的相同结构: https://developer.android.com/guide/topics/connectivity/bluetooth -le.html .此设置仅适用于单个连接.我能否保持这种结构,即在BluetoothLeService类中扩展Service类并绑定到该服务.最近,我发现Service类是一个单例,因此我将如何创建BluetootLeService类的不同实例并接收广播,并注册广播接收器/接收器以处理来自适当设备的更改.

I am trying to create an application that connects and receives notifications from multiple bluetooth low energy devices. I am wondering how this can be achieved. Do I need a separate thread for each connection? How can I make sure the services get discovered and notifications get set in an order that works given the asynchronous nature of the API. I am currently using the same structure provided here:https://developer.android.com/guide/topics/connectivity/bluetooth-le.html. This is setup for a single connection only. Would I be able to keep this structure i.e. extending the Service class in the BluetoothLeService class and binding to the service. I have recently discovered that The Service class is a singleton so how would I go about creating different instances of my BluetootLeService class and receiving broadcast and registering the Broadcast Receiver/Receivers to handle changes from the appropriate devices.

推荐答案

要实现多个BLE连接,您必须存储多个BluetoothGatt对象,并将这些对象用于不同的设备.要存储BluetoothGatt的多个连接对象,可以使用Map<>

To achieve multiple BLE connection you have to store multiple BluetoothGatt object and use those object for different device. To store multiple connection object of BluetoothGatt you can use Map<>

private Map<String, BluetoothGatt> connectedDeviceMap;

在服务中onCreate初始化Map

connectedDeviceMap = new HashMap<String, BluetoothGatt>();

然后在调用device.connectGatt(this, false, mGattCallbacks);连接到 GATT服务器之前,检查设备是否已连接.

Then before calling device.connectGatt(this, false, mGattCallbacks); to connect to GATT Server check that device is already connected.

  BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
  int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

  if(connectionState == BluetoothProfile.STATE_DISCONNECTED ){
   // connect your device
   device.connectGatt(this, false, mGattCallbacks);
  }else if( connectionState == BluetoothProfile.STATE_CONNECTED ){
   // already connected . send Broadcast if needed
  }

BluetoothGattCallback上,如果连接状态为 CONNECTED ,则将BluetoothGatt对象存储在Map上;如果连接状态为 DISCONNECTED ,则将其从Map

On BluetoothGattCallback if connection state is CONNECTED then store BluetoothGatt object on Map and if connection state is DISCONNECTED then remove it form Map

        @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {

        BluetoothDevice device = gatt.getDevice();
        String address = device.getAddress();

        if (newState == BluetoothProfile.STATE_CONNECTED) {

            Log.i(TAG, "Connected to GATT server.");

            if (!connectedDeviceMap.containsKey(address)) {
                  connectedDeviceMap.put(address, gatt);
              }
             // Broadcast if needed
            Log.i(TAG, "Attempting to start service discovery:" +
                    gatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(TAG, "Disconnected from GATT server.");
            if (connectedDeviceMap.containsKey(address)){
              BluetoothGatt bluetoothGatt = connectedDeviceMap.get(address);
              if( bluetoothGatt != null ){
                   bluetoothGatt.close();
                   bluetoothGatt = null;
              }
              connectedDeviceMap.remove(address);
            }
            // Broadcast if needed
        }
    }

类似地,onServicesDiscovered(BluetoothGatt gatt, int status)方法在参数上具有BluetoothGatt连接对象,并且可以从该BluetoothGatt获取设备.和其他类似public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)的回调方法,您将获得设备形式gatt.

Similarly onServicesDiscovered(BluetoothGatt gatt, int status) method you have BluetoothGatt connection object on parameter and you can get device from that BluetoothGatt. And other callback method like public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) you will get the device form gatt .

当您需要 writeCharacteristic writeDescriptor 时,请从Map获取BluetoothGatt对象,并使用该BluetoothGatt对象调用gatt.writeCharacteristic(characteristic) gatt.writeDescriptor(descriptor)不同的连接.

When you need to writeCharacteristic or writeDescriptor, get BluetoothGatt object from Map and use that BluetoothGatt object to call gatt.writeCharacteristic(characteristic) gatt.writeDescriptor(descriptor) for different connection.

我认为您不需要为每个连接使用单独的线程.只需在后台线程上运行Service.

I think you don't need to use separate Thread for each connection. Just run the Service on a Background Thread.

希望这对您有所帮助.

Hope this help you.

这篇关于Android BLE多个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 04:00