安卓蓝牙4.0以上连接多台设备并接收蓝牙设备数据

前沿:
在我之前写的代码中都没有实现蓝牙连接多个设备,由于时间的原因没有进行更改。

iOS端实现 蓝牙多个连接确实比安卓的方便,本身利用官方的Demo就可以实现多台连接,只不过自己利用view加以区分就可以。

到此蓝牙4.0之前是通过scoket连接多台设备,不过感觉传输的稳定性没有 BluetoothGatt稳定所以本次我利用BluetoothGatt来连接 蓝牙多台设备

1.由于连接多台设备 我只讲解多台设备的操作 前面的基础连接我不做讲解了

首先连接多台设备需要考虑的问题就是 蓝牙每次连接的时候只能同时连接一个 BluetoothGatt 但是我们需要连接多台的时候 我们可以这样来做 把这个BluetoothGatt放入到一个 Arraylist里面这样不就可以实现多台设备的连接。

所以我们应该在Service代码中添加如下的代码,我运用的是官方的De mo只不过在原有的基础上面进行了修改

如下:

public boolean connect(final String name,final String address,Context context) {
mContext = context;

    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }else{
        mDeviceAddre.add(address);
        mDeviceName.add(name);
    }

    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);
    
    if(checkGatt(bluetoothGatt)){
        connectionQueue.add(bluetoothGatt);
    }

    Log.d(TAG, "Trying to create a new connection.");
    return true;

}

加入这段代码主要是区别是否有相同的有相同的就不让他添加进来
private boolean checkGatt(BluetoothGatt bluetoothGatt) {
if (!connectionQueue.isEmpty()) {
for(BluetoothGatt btg:connectionQueue){
if(btg.equals(bluetoothGatt)){
return false;
}
}
}
return true;
}

还有一点需要注意点就是

蓝牙读书这个 BluetoothGattCharacteristic 也需要 命名成 public List listCharacteristic = new ArrayList<>();

这段代码 目的: 扫描出来的服务 和 特征 需要 添加到 listCharacteristic中去
public void findService(BluetoothGatt gatt)
{
List gattServices = gatt.getServices();
Log.i(TAG, “Count is:” + gattServices.size());
for (BluetoothGattService gattService : gattServices)
{
Log.i(TAG, gattService.getUuid().toString());
Log.i(TAG, UUID_SERVICE.toString());
if(gattService.getUuid().toString().equalsIgnoreCase(UUID_SERVICE.toString()))
{
List gattCharacteristics =
gattService.getCharacteristics();
Log.i(TAG, “Count is:” + gattCharacteristics.size());
for (BluetoothGattCharacteristic gattCharacteristic :
gattCharacteristics)
{
if(gattCharacteristic.getUuid().toString().equalsIgnoreCase(UUID_NOTIFY.toString()))
{
Log.i(TAG, gattCharacteristic.getUuid().toString());
Log.i(TAG, UUID_NOTIFY.toString());
mNotifyCharacteristic = gattCharacteristic;

                    listCharacteristic.add(mNotifyCharacteristic);

                    setCharacteristicNotification(gattCharacteristic, true);
                    broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED, gatt.getDevice().getAddress());
                    return;
                }
            }
        }
    }
} 

//多台设备断开连接的时候 需要一个一个的去移除 这个跟连接单个设备断开的时候是有区别的

private synchronized void listClose(BluetoothGatt gatt) {
if (!connectionQueue.isEmpty()) {
if (gatt != null) {
for(final BluetoothGatt bluetoothGatt:connectionQueue){
if(bluetoothGatt.equals(gatt)){
bluetoothGatt.close();

                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                Thread.sleep(250);
                                connectionQueue.remove(bluetoothGatt);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        }else{
            for (BluetoothGatt bluetoothGatt : connectionQueue) {
                bluetoothGatt.close();
            }
            connectionQueue.clear();
        }
    }
}   

2.程序运行的效果图 我这里只有2台设备做测试 如下:
在这里插入图片描述

3.demo下载地址:

http://download.csdn.net/detail/a1989214/9837594

  1. 如果没有找到我的github可以找到

https://github.com/ArdWang/LHTempAllDevice

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

面包超人吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值