gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:05 26 4
gpt4 key购买 nike

我正在开发 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;
}

最佳答案

您将通过为每个设备创建每个 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+

关于android - 如何在 Android 低功耗蓝牙(BLE)中同时创建多个连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com