2014-11-05 30 views
0

我正在AndroidBLE(蓝牙)上工作。我通过Google找到了信息。看起来Android可以通过使用多个BluetoothGatt连接到multiple BLE device像下面的伪代码。有更好的方式连接到Android中的多个BLE设备吗?

我有多个BLE设备。我想我需要首先定义多个BluetoothGatt参数。

private BluetoothGatt mBluetoothGattA = null, 
    mBluetoothGattB = null , mBluetoothGattC = null; 

第一个BLE设备连接。

final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address_A); 
mBluetoothGattA = device.connectGatt(this, false, mGattCallback); 

尝试连接到第二个BLE设备。

final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address_B); 
mBluetoothGattB = device.connectGatt(this, false, mGattCallback); 

对吗?如果我连接到6个BLE设备,我应该定义6个BluetoothGatt参数吗?

是否有智能的方式连接到多个BLE设备?

回答

0

您将通过为每个BLE设备创建每个BluetoothGattCallback(现在最多7个)来处理每个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);就这样。你会发现很多例子处理一个连接,并且为了多重连接而开发更多的例子。

0

不需要多个BluetoothGattCallback。偶然地,我使用相同的BluetoothGattCallback连接两个BLE设备,并且工作正常。

在BluetoothGattCallback中,您必须知道发送数据的设备的地址。

例如,你可以使用:

gatt.getDevice().getAddress(); 

希望这有助于

相关问题