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

问题描述 投票:0回答:2

我正在开发 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;
}
android bluetooth-lowenergy android-bluetooth
2个回答
0
投票

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

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

private final BluetoothGattCallback twoGattcallback = 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+


-1
投票

我之前读过,蓝牙 4 最多可以支持七个设备。

您可能想使用类似的库:

http://arissa34.github.io/Android-Multi-Bluetooth-Library/

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.