Swifty Sensors
iOS 和 macOS 的 Bluetooth LE 传感器管理器。
安装
CocoaPods
use_frameworks!
pod 'SwiftySensors'
手动方式
将 Sources
目录中的所有 swift 文件复制到您项目中。
Swift 包管理器
将此仓库 URL 添加到依赖项列表
dependencies: [
.Package(url: "https://github.com/kinetic-fit/sensors-swift", Version(X, X, X))
]
注意:如果您正在使用Swifty Sensors Kinetic 插件,由于没有支持 Objective-C 库,因此当前无法使用 Swift 包管理器。
使用方法
查看示例 iOS 应用,以了解如何:
- 侦测传感器
- 连接到传感器
- 发现服务
- 发现特征
- 读取值
- 特征通知
创建 SensorManager 的初始化方法是简单的。
- 设置需要扫描的 服务
- 添加您要在 传感器 上发现但不在广告数据中扫描的额外 服务
- 设置管理器的 扫描模式
// Customize what services you want to scan for
SensorManager.instance.setServicesToScanFor([
CyclingPowerService.self,
CyclingSpeedCadenceService.self,
HeartRateService.self
])
// Add additional services we want to have access to (but don't want to specifically scan for)
SensorManager.instance.addServiceTypes([DeviceInformationService.self])
// Set the scan mode (see documentation)
SensorManager.instance.state = .aggressiveScan
// Capture SwiftySensors log messages and print them to the console. You can inject your own logging system here if desired.
SensorManager.logSensorMessage = { message in
print(message)
}
SwiftySensors 使用 Signals 使观察各种事件变得简单。
// Subscribe to Sensor Discovery Events
SensorManager.instance.onSensorDiscovered.subscribe(on: self) { sensor in
// sensor has been discovered (but not connected to yet)
}
// Subscribe to value changes on a Characteristic
characteristic.onValueUpdated.subscribe(on: self) { characteristic in
// characteristic.value was just updated
}
所有服务和特征都是具体的类,这使得与蓝牙低功耗传感器一起工作更加容易。
示例心率传感器层次结构
Sensor
- HeartRateService
- Measurement
- BodySensorLocation
- DeviceInformationService
- SoftwareRevision
- ModelNumber
- SerialNumber
- ...
连接到传感器
SensorManager.instance.connectToSensor(sensor)
订阅值更新并获取心率传感器的反序列化值
// The sensor could be selected by a user, selected by a matching algorithm on the sensor's advertised services, etc.
let sensor = < Heart Rate Sensor >
// The function service() on a sensor will try to find the appropriate return type requested
guard let hrService: HeartRateService = sensor.service() else { return }
// The function characteristic() on a service will try to find the appropriate return type requested
guard let hrMeasurement: HeartRateService.Measurement = hrService.characteristic() else { return }
// ... the HeartRateService class also defines the `measurement` property, which is equivalent to the above
hrMeasurement.onValueUpdated.subscribe(on: self) { characteristic in
// The Measurement characteristic has a deserialized value of the sensor data
let heartRate = hrMeasurement.currentMeasurement.heartRate
}
当前具体服务和特征
扩展和第三方服务
- Wahoo 训练器特性扩展 适用于骑行功率服务
- Kinetic 传感器
类型注入;编写服务、特性、扩展
根据您的需要添加自定义功能相对简单。
// Customize the Sensor class that the manager instantiates for each sensor
SensorManager.instance.SensorType = < Custom Sensor Class : Extends Sensor >
查看HeartRateService,了解编写自己的服务类的一个简单示例。
要将新特性类型添加到不属于官方规范的现有服务中,请参阅Wahoo Trainer Characteristic Extension 这不是 BLE 感应器制造商采用的标准解决方案,但他们有时会打破规则。
序列器
特性的序列化和反序列化被隔离在特性类之外,并且可以单独使用。这在进行传感器管理时非常有用,只需正确反序列化各种 BLE 消息的逻辑。
use_frameworks!
pod 'SwiftySensors/Serializers'
已知错误
无。
待办事项
有许多官方 BLE 规范需要实现。
使用 SwiftySensors 的项目
如果您希望您的应用程序出现在这里,请联系我们!