android多个蓝牙广播接收器,Java-Android BLE多个连接

I am wondering how this can be achieved

要实现多个BLE连接,您必须存储多个BluetoothGatt对象,并将这些对象用于不同的设备.要存储BluetoothGatt的多个连接对象,可以使用Map

private Map connectedDeviceMap;

On Service onCreate初始化地图

connectedDeviceMap = new HashMap();

然后在调用device.connectGatt(this,false,mGattCallbacks);之前要连接到GATT Server,请检查设备是否已连接.

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);

int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

if(connectionState == BluetoothProfile.STATE_DISCONNECTED ){

// connect your device

device.connectGatt(this, false, mGattCallbacks);

}else if( connectionState == BluetoothProfile.STATE_CONNECTED ){

// already connected . send Broadcast if needed

}

在BluetoothGattCallback上,如果连接状态为CONNECTED,则将BluetoothGatt对象存储在Map上;如果连接状态为DISCONNECTED,则将其从Map中删除

@Override

public void onConnectionStateChange(BluetoothGatt gatt, int status,

int newState) {

BluetoothDevice device = gatt.getDevice();

String address = device.getAddress();

if (newState == BluetoothProfile.STATE_CONNECTED) {

Log.i(TAG, "Connected to GATT server.");

if (!connectedDeviceMap.containsKey(address)) {

connectedDeviceMap.put(address, gatt);

}

// Broadcast if needed

Log.i(TAG, "Attempting to start service discovery:" +

gatt.discoverServices());

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

Log.i(TAG, "Disconnected from GATT server.");

if (connectedDeviceMap.containsKey(address)){

BluetoothGatt bluetoothGatt = connectedDeviceMap.get(address);

if( bluetoothGatt != null ){

bluetoothGatt.close();

bluetoothGatt = null;

}

connectedDeviceMap.remove(address);

}

// Broadcast if needed

}

}

同样,在onServicesDiscovered(BluetoothGatt gatt,int status)方法中,您也可以在参数上设置BluetoothGatt连接对象,然后可以从该BluetoothGatt获取设备.以及其他回调方法,例如public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic特性),您将获得gatt形式的设备.

当您需要writeCharacteristic或writeDescriptor时,请从Map中获取BluetoothGatt对象,并使用该BluetoothGatt对象为不同的连接调用gatt.writeCharacteristic(characteristic)gatt.writeDescriptor(descriptor).

Do I need a separate thread for each connection?

我认为您不需要为每个连接使用单独的线程.只需在后台线程上运行服务即可.

希望这对您有所帮助.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款用于开发Android应用程序的集成开发环境(IDE)。BLEBluetooth Low Energy)是一种低功耗蓝牙技术,用于在设备之间进行无线通信。在Android Studio中,可以通过接收BLE广播来实现与BLE设备的通信。 要接收BLE广播,首先需要创建一个BLE广播接收器(BroadcastReceiver)。BLE广播接收器是一个用于接收系统发送的广播消息的组件。可以通过以下步骤来创建和注册BLE广播接收器: 1. 创建一个类,继承自BroadcastReceiver,并重写onReceive()方法。在onReceive()方法中处理接收到的广播消息。 2. 在AndroidManifest.xml文件中注册BLE广播接收器。在<application>标签内添加一个<receiver>标签,并设置接收器的名称和要接收的广播消息的过滤条件。 例如,以下是一个简单的BLE广播接收器的示例代码: ```java public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); switch (state) { case BluetoothAdapter.STATE_OFF: // 蓝牙已关闭 break; case BluetoothAdapter.STATE_ON: // 蓝牙已打开 break; } } } } ``` 在AndroidManifest.xml文件中注册BLE广播接收器: ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application> <receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> </intent-filter> </receiver> </application> </manifest> ``` 以上代码示例中,BLE广播接收器接收的是蓝牙状态改变的广播消息(ACTION_STATE_CHANGED)。根据接收到的广播消息,可以执行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值