用于 iOS 和 macOS 的蓝牙 LE 传感器管理器。
use_frameworks!
pod 'SwiftySensors'
将 Sources
目录中的所有 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
}
所有服务和特征都是具体的类,使蓝牙 LE 传感器的使用更加容易。
心率传感器层级示例
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
}
添加特定于您需求的自定义功能非常简单。
// Customize the Sensor class that the manager instantiates for each sensor
SensorManager.instance.SensorType = < Custom Sensor Class : Extends Sensor >
查看 HeartRateService 以获取编写您自己的 Service 类的简单示例。
要将新的特征类型添加到不是官方规范一部分的现有服务,请查看 Wahoo 训练器特征扩展。 这并非 BLE 传感器制造商采用的正常解决方案,但偶尔他们会违反规则。
特征数据的序列化/反序列化独立于特征类之外,可以单独使用。 如果您已经拥有传感器管理堆栈,并且只需要正确的逻辑来反序列化各种 BLE 消息,这将非常有用。
use_frameworks!
pod 'SwiftySensors/Serializers'
无。
有许多官方 BLE 规范需要实现。
如果您想在此处列出您的应用程序,请告诉我们!