swift - BLE Gatt 自定义服务 - 如何仅通知已注册的中心(基于应用级别)
问题描述
语境
应用程序通过 BLE 5.0 连接到设备以进行简单配置/获取设备的一些信息。(用作服务工具)
未经某种注册,不允许与设备通信。因此,可以扫描设备的二维码并询问应用程序 ID 和密码(在设备启动期间或按下按钮时)
可以在蓝牙堆栈级别连接并查看所有服务和特征,只要服务实现在中央“未知”时没有做任何有用的事情,我在如何在应用程序级别锁定它时遇到问题。
这是我的第一个蓝牙/Ble/Gatt-Server/Dbus 和应用程序开发项目。
我的设置
外围设备
我已经实现了一个 Gatt-Server,在Raspberry Pi4上使用Python3/DBus和Bluez 5.50(ControllerMode = le only)
我从以下示例开始:
- 如何读出 Pi 的 cpu 温度
- 如何写入 Gatt-Server(在摄氏和华氏之间切换)
- 温度变化时如何通知
https://github.com/Douglas6/cputemp/blob/master/cputemp.py
我还从以下 Apple 文档中获得了一些想法:
特征权限
- 只写(有响应)给 exec。一些命令
- 只通知接收数据
对于配对:
- 我使用了一个带有选项的简单 Bluez 代理示例
NoInputNoOutput
。作为中心,我只有在第一次与设备配对时才会遇到问题。没有我必须接受的代码(这是我想要的)。
中环
我有一个 iOS 应用,使用 Swift5 和 CoreBluetooth
从中心的角度来看,有点像 Apple Notifications Center Service 的文档中所示:
- 我有一个特征可以写入命令(读取属性 x 的数据)
- 我有被通知的特征(如果数据更改或我要求)
我的基本沟通过程(目前对我来说很有效)
- 应用程序连接到 Pi(无需 PIN 码的简单配对)
- 应用程序读取所有服务、特征并启用带有通知标志的特征的通知
- 应用程序提供一个授权码(从扫描的二维码中提取)并接收一个应用程序 id 和一个密码并存储它以供下次使用
- 请求属性 x 并接收属性 x 的当前值的通知
我的问题、想法和疑问
当第二个中心激活该特定特征的通知时,它也会收到属性 x 的信息。Central B 使用一个免费的应用程序来扫描所有可用的服务/特性。(我使用 LightBlue for Mac 或 Beacon Grabber for iPhone)。Central B 刚刚与 Raspberry Pi 配对,不需要提供其他东西。
- 到目前为止,在我的服务和特性实现中,我没有关于连接设备的上下文。我知道,我可以通过读出来读取哪个设备写入特征
dbus.ObjectPath('/org/bluez/hci0....
我可以记住该信息并询问设备 X 是否有有效的 id 和 pin,否则我不会做任何事情。
我也只能在已知设备要求时触发通知。但这并不能完全解决问题。
我只能(也许..)允许一个中央连接来避免我的问题(但不能解决它)
有没有办法只通知特定的设备?
有没有更好的方法来处理这个问题?
我可以强制断开与未注册的中心的连接吗?
是否有更好的方法来实现此级别的某种安全性?
我对一些想法和意见感到高兴。
谢谢!
解决方案
推荐阅读
- angular - 如何测试是否从组件中以角度调用服务
- python - 查找数据框中每一列的平均值,按列分组,排除一个值
- excel - 如何使用 VBA 代码从 HTTP 响应获取值到 Excel
- java - 如何在 Hibernate 中包含串联?
- c - 端口扫描时随机端口连接
- wordpress-gutenberg - 如何使用 WordPress Gutenberg MediaUpload 上传多张图片?
- r - 将数据框转换为语料库
- twitter-bootstrap - Bootstrap:单行有 4 个控件的表单
- angular - (Angular)使全局变量始终根据服务更改其值
- swift - 为什么我的 NSTableView 显示我的数据错误?