Android手机与多个BLE设备通信

2 篇文章 0 订阅
1 篇文章 0 订阅

       BLE是蓝牙4.0的核心Profile,主打功能是快速搜索,快速连接,超低功耗保持连接和传输数据,弱点是数据传输速率低,由于BLE的低功耗特点,因此普遍用于穿戴设备。Android 4.3才开始支持BLE API。


       本文改自Android Sample: BluetoothLeGatt(可参见http://developer.android.com/samples/BluetoothLeGatt/index.html,如果不能翻墙,sdk本地帮助文档中也有)。


       以上的例子是一对一的,也就是一次性只能连接一个设备,但很多情况下需要同时连接多个设备,收取多个设备的数据,那就很麻烦了,因为网上的相关资料很少,stackoverflow上有相关的帖子,全英文,而且也不全。


       原本我的想法是,既然sample中一个service连接一个设备,那我开多个service不就可以连接多个设备了?但事实并非这么简单,因为一个Android系统只能有一个BluetoothAdapter,那怎么实现开多个service呢?我一时也没能实现。


       后来我翻看了Sample的源码,在BluetoothLeService类中发现:

    public boolean connect(final String address) { ......
        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
......}
虽然 BluetoothAdapter只能有一个,但BluetoothGatt可以有多个,将其放置与Arraylist容器中,逐一进行连接即可,下面是我的代码:

private ArrayList<BluetoothGatt> connectionQueue = new ArrayList<BluetoothGatt>();
public boolean connect(final String address) {
        ......
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        BluetoothGatt bluetoothGatt;
        bluetoothGatt = device.connectGatt(this, false, mGattCallback);
        connectionQueue.add(bluetoothGatt);
        .......
    }

       接下来就是在disconnect(),close()等方法中增加对connectionQueue的操作即可。


       我的Demo源码在http://download.csdn.net/detail/mark_sssss/8598191


       不过由于我的项目需要,这个Demo只能接收字符数据,而不能发送数据。如果添加发数据的功能,你可以参考这篇文章http://blog.csdn.net/hellogv/article/details/24267685,按以上方法,将对BluetoothGatt的操作以connectionQueue的方式实现即可。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Android平台上,使用BLE(低功耗蓝牙)进行设备通信需要编写源代码。下面是一个简单的示例,展示了如何启用BLE功能、搜索和连接设备、发送和接收数据。 首先,需要确保应用程序在AndroidManifest.xml文件中申请必要的权限和特性。 ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> ``` 接下来,在应用程序的MainActivity中创建一个BluetoothAdapter对象,并启用BLE功能。 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { // 设备不支持蓝牙蓝牙未启用 // 进行相关处理 } ``` 然后,需要创建一个BluetoothLeScanner对象来搜索BLE设备。可以通过扫描结果回调来获取设备信息。 ```java BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); bluetoothLeScanner.startScan(new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { // 获取设备信息 BluetoothDevice device = result.getDevice(); // 进行相关处理,比如连接设备 } }); ``` 接下来,可以通过设备名称或MAC地址来连接设备,并与之进行数据通信。 ```java BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(this, false, new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { // 设备已连接 // 进行相关处理,比如发现服务 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 设备已断开连接 // 进行相关处理 } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { // 服务已发现 // 进行相关处理,比如获取特征值 } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { // 特征值已读取 // 进行相关处理 } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { // 特征值已写入 // 进行相关处理 } }); ``` 最后,可以使用BluetoothGatt对象向设备发送数据或者接收设备发送的数据。 ```java BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid); characteristic.setValue(data); gatt.writeCharacteristic(characteristic); ``` 这只是一个简单的示例,实际的BLE设备通信可能会涉及更多的操作和数据处理。使用上述代码作为起点,可以根据实际需求进行扩展和改进。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值