如何在Android蓝牙低功耗(BLE)中同时创建多个连接?

zvokhttg  于 2022-12-31  发布在  Android
关注(0)|答案(2)|浏览(359)

我正在开发Android BLE应用程序。
在Android中,是否有同时连接多个BLE设备(创建多个连接)的程序。在我的应用程序中,有多个BLE灯,因此第一个灯成功连接,当我点击连接到第二个灯时,第二个灯也连接。但一段时间后,第二个灯自动断开连接。我必须连接多个灯,最多8个。
这是我正在做的

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
{
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                        int newState)
    {
        String intentAction;

        if (newState == BluetoothProfile.STATE_CONNECTED)
        {

            intentAction = GattActions.ACTION_GATT_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(DSERVICE_TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(DSERVICE_TAG, "Attempting to start service discovery:"
                    + mBluetoothGatt.discoverServices());

            readRssi();

        }
        else if (newState == BluetoothProfile.STATE_DISCONNECTED)
        {

            intentAction = GattActions.ACTION_GATT_DISCONNECTED;
            Log.i(DSERVICE_TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);

        }
    }

    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status)
    {
        if (status == BluetoothGatt.GATT_SUCCESS)
        {
            broadcastUpdate(GattActions.ACTION_GATT_RSSI, rssi);
        }
        else
        {
            Log.w(DSERVICE_TAG, "onReadRemoteRssi received: " + status);
        }
    }

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

        if (status == BluetoothGatt.GATT_SUCCESS)
        {
            Log.v(DSERVICE_TAG, "Device Discovered Uuids Are==" + gatt.getDevice().getUuids());
            broadcastUpdate(GattActions.ACTION_GATT_SERVICES_DISCOVERED);
        }
        else
        {
            Log.w(DSERVICE_TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    {
        if (status == BluetoothGatt.GATT_SUCCESS)
        {

            Log.d("TestCharacter", "onCharacteristicRead character " + characteristic.getUuid());
            broadcastUpdate(GattActions.ACTION_DATA_AVAILABLE, characteristic);
            broadcastUpdate(GattActions.EXTRA_DATA, characteristic);

            filterCharacteristicOfDevices(gatt, characteristic);

        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    {
        //super.onCharacteristicWrite(gatt, characteristic, status);
        if (status != BluetoothGatt.GATT_SUCCESS)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
            }
            writeCharacteristic(characteristic, gatt);
        }

    }

阅读特性和readRss()

public void readCharacteristic(BluetoothGattCharacteristic characteristic)
{
    if (mBluetoothAdapter == null || mBluetoothGatt == null)
    {
        Log.w(DSERVICE_TAG, "BluetoothAdapter not initialized");
        return;
    }

    mBluetoothGatt.readCharacteristic(characteristic);
    try
    {
        Thread.sleep(100);
    }
    catch (InterruptedException e)
    {
    }
}

public void readRssi()
{
    if (mBluetoothAdapter == null || mBluetoothGatt == null)
    {
        Log.w(DSERVICE_TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.readRemoteRssi();

    new Handler().postDelayed(readRssi, 200);
}

private Runnable readRssi = new Runnable()
{
    @Override
    public void run()
    {
        //read remote rssi every second
        for (Map.Entry<String, BluetoothGatt> entryGatt : myApplication.deviceGattMap.entrySet())
        {

            String deviceAddress = entryGatt.getKey();
            BluetoothGatt bluetothGatt = entryGatt.getValue();
            bluetothGatt.readRemoteRssi();

            //delay for reading rssi
            try
            {
                Thread.sleep(200);
            }
            catch (InterruptedException e)
            {
            }
        }
    }
};

和连接方法,我添加GATT对象到HashMap的每盏灯:-

public boolean connect(final String address)
{
    if (mBluetoothAdapter == null || address == null)
    {
        Log.w(DSERVICE_TAG,
                "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    // Previously connected device. Try to reconnect.
    if (mBluetoothDeviceAddress != null
            && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null)
    {
        Log.d(DSERVICE_TAG,
                "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null)
    {
        Log.w(DSERVICE_TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the
    // autoConnect
    // parameter to false.

    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

    Log.d(DSERVICE_TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;

    //Arun

    //delay for reading rssi
    try
    {
        Thread.sleep(100);
    }
    catch (InterruptedException e)
    {
    }
    //map of gatt
    myApplication.deviceGattMap.put(mBluetoothDeviceAddress, mBluetoothGatt);
    try
    {
        Thread.sleep(50);
    }
    catch (InterruptedException e)
    {
    }
    Log.d(DSERVICE_TAG, "GATTMAP SIZE=="+ myApplication.deviceGattMap.size()+"---"+myApplication.deviceGattMap.get(mBluetoothDeviceAddress));
    return true;
}
gfttwv5a

gfttwv5a1#

您将通过为每个BLE设备创建每个BluetoothGattCallback来处理每个BLE设备。例如:

private final BluetoothGattCallback oneGattcallback = new BluetoothGattCallback() ...

第一个月
然后尝试连接mBluetoothGattA = deviceA.connectGatt(this, false, oneGattcallback );mBluetoothGattB = deviceB.connectGatt(this, false, twoGattcallback );就像这样。你会发现很多例子处理一个连接,只是开发更多的多个连接。此外,检查这个视频https://www.youtube.com/watch?v=qx55Sa8UZAQ显示,最大并发GATT连接是(4)Android 4.5和(7)Android 4.4+

csga3l58

csga3l582#

我之前读到蓝牙4可以支持多达七个设备。
您可能希望使用如下库:
http://arissa34.github.io/Android-Multi-Bluetooth-Library/
希望有帮助。

相关问题