且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android BLE多个连接

更新时间:2021-09-24 23:06:34

我想知道如何实现

I am wondering how this can be achieved

要实现多个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.

每个连接是否需要一个单独的线程?

Do I need a separate thread for each 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.