Android 4.3是否支持多个BLE设备连接?
我目前正在使用Android 4.3蓝牙低能耗,我能够连接到设备,获得服务,读/写服务。现在,当我尝试连接到第二个设备时,第二个设备的服务被接收,第一个设备的服务丢失。现在,当我尝试连接到第一个设备的写/读特性时,什么都不起作用Android 4.3是否支持多个BLE设备连接?,android,bluetooth,connection,bluetooth-lowenergy,Android,Bluetooth,Connection,Bluetooth Lowenergy,我目前正在使用Android 4.3蓝牙低能耗,我能够连接到设备,获得服务,读/写服务。现在,当我尝试连接到第二个设备时,第二个设备的服务被接收,第一个设备的服务丢失。现在,当我尝试连接到第一个设备的写/读特性时,什么都不起作用 有人尝试连接到多个设备。如何为两个设备初始化Gatt?我认为三星BLE API和谷歌的API有Broadcom BLE堆栈,而Broadcom堆栈目前不支持多个BLE设备。我在三星BLE堆栈(galaxy S4)上有完全相同的行为。我破解了三星代码并修复了它: Blue
有人尝试连接到多个设备。如何为两个设备初始化Gatt?我认为三星BLE API和谷歌的API有Broadcom BLE堆栈,而Broadcom堆栈目前不支持多个BLE设备。我在三星BLE堆栈(galaxy S4)上有完全相同的行为。我破解了三星代码并修复了它: BluetoothGatt类保存“所有”已发现服务的列表。调用BluetoothGatt->DiscoversServices将清除此列表。我驱动自己的类并修改函数的行为,只删除相关Bluetooth设备的服务。这是我的代码:
public class MyBluetoothGatt extends BluetoothGatt {
MyBluetoothGatt(Context paramContext, BluetoothProfile.ServiceListener paramServiceListener)
{
super(paramContext, paramServiceListener);
}
public final boolean discoverServices(BluetoothDevice paramBluetoothDevice)
{
if (paramBluetoothDevice == null)
{
Log.e("BtGatt.BluetoothGatt", "discoverServices() - device is null ");
return false;
}
Log.d("BtGatt.BluetoothGatt", "discoverServices() - device: " + paramBluetoothDevice.getAddress());
if ((d == null) || (this.f == 0))
return false;
// old code this.h.clear();
//--new code
@SuppressWarnings("rawtypes")
Iterator localIterator = this.h.iterator();
while (localIterator.hasNext())
{
BluetoothGattService localBluetoothGattService;
localBluetoothGattService = (BluetoothGattService)localIterator.next();
if (localBluetoothGattService.a().equals(paramBluetoothDevice))
{
this.h.remove(paramBluetoothDevice);
}
}
//end new code
this.d.discoverServices(this.f, paramBluetoothDevice.getAddress());
return true;
}
}
我还必须使用类编辑器来删除一些“final”修饰符,以便能够对我自己的类进行修改。这是一个大的黑客,但现在它的工作漂亮。正式发布后,我会将S4升级到android 4.3,并移植我的代码,所以祈求好运,这对安卓4.3也适用。如果您为每个要连接的设备实现不同的
BluetoothGatt
实例和不同的GattCallback
,那么它应该适用于所有设备。我刚刚验证了这是可能的(Nexus 4和4.3):我已使用API 18附带的BLE样本连接到HR传感器,同时使用经过大量修改的样本连接到速度计设备。但是,即使我使用两个独立的服务(从一个服务超类派生两个服务不起作用-只存在一个实例),我也无法从自己的应用程序连接到这两个服务。所以我想两个同时连接是可能的,不确定如何从同一个活动中实现它,但一旦我发现它,就会发布一个解决方案