关于蓝牙开发,必须注意的广播

本文详细介绍了蓝牙开发中必要的广播注册,包括搜索、配对、连接等关键阶段的广播动作。特别强调了PAIRING_REQUEST这一隐藏广播的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**有注释的广播,蓝牙连接时都会用到

* bind一般会停止搜索ACTION_DISCOVERY_FINISHED

* @param cxt

*/

intentFilter.addAction(BluetoothDevice.ACTION_FOUND); //搜索蓝压设备,每搜到一个设备发送一条广播

intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //配对开始时,配对成功时

intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); //配对时,发起连接

intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);

intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); //配对结束时,断开连接

intentFilter.addAction(PAIRING_REQUEST); //配对请求

(android.bluetooth.device.action.PAIRING_REQUEST)

intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始搜索

intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //搜索结束。重新搜索时,会先终止搜索

intentFilter.addAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//连接蓝牙,断开蓝牙 

intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);

intentFilter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);//更改蓝牙名称,打开蓝牙时,可能会调用多次

intentFilter.addAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

intentFilter.addAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);

intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);//搜索模式改变


蓝牙开发,如果考虑到错误的断开蓝牙,unbind,等情况,必须注册好以上的蓝牙广播,其中,PAIRING_REQUEST,是android隐藏的广播,涉及到跳过bind弹框,直接pin,配对,必须要注册。

### 关于在QT开发中接收蓝牙广播 为了处理蓝牙低功耗(BLE)广告包,在Qt环境中可以利用`QLowEnergyController`以及关联的类来完成这一功能。当创建一个用于扫描周围BLE设备的应用程序时,通常会实例化`QBluetoothDeviceDiscoveryAgent`对象并连接其信号槽以便响应发现的新设备事件。 下面展示一段简单的C++代码片段,该代码展示了怎样设置一个基本框架去监听来自附近支持BLE特性的装置所发出的服务通告: ```cpp #include <QObject> #include <QQmlApplicationEngine> #include <QBluetoothDeviceDiscoveryAgent> class BluetoothReceiver : public QObject { Q_OBJECT public: explicit BluetoothReceiver(QObject *parent = nullptr) : QObject(parent), m_discoveryAgent(new QBluetoothDeviceDiscoveryAgent(this)) { connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothReceiver::onDeviceDiscovered); // Start device discovery. m_discoveryAgent->start(); } private slots: void onDeviceDiscovered(const QBluetoothDeviceInfo &info) { qDebug() << "Found new device:" << info.name() << '(' << info.address().toString() << ')'; // Here you can add logic to filter devices based on their advertising data or UUIDs. } private: QBluetoothDeviceDiscoveryAgent *m_discoveryAgent; }; ``` 上述代码实现了启动蓝牙设备探测过程,并每当检测到新设备就会触发相应的slot函数打印出设备名称及其MAC地址[^1]。 对于更深入的操作,比如解析特定类型的广播载荷或是过滤感兴趣的GATT服务UUID,则可能需要进一步研究`QLowEnergyAdvertisingData`等相关接口文档以获取更多细节。 如果目标是在移动平台上部署此功能,需要注意不同操作系统间可能存在权限管理上的差异;因此建议查阅官方指南了解具体的平台适配事项[^4]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值