Polar 传感器 SDK
这是 Polar 软件开发套件的官方仓库。使用本 SDK,您可以开发针对 Polar 生产的传感器制造的 Android 和 iOS 应用程序。
SDK API 使用 ReactiveX。您可以在其网站 reactivex 上了解更多关于 ReactiveX 的信息
特性
H10 心率传感器
市场上最准确的心率传感器。H10 用于本页面的入门部分。商店页面
H10 SDK支持的功能
- 心率(每分钟跳动次数)和RR间隔(毫秒)。
- 心率广播。
- 心电图(ECG)数据,单位为 µV,采样率为 130Hz。
- 加速度计数据,采样率为 25Hz、50Hz、100Hz 和 200Hz,范围为 2G、4G 和 8G。轴特定的加速度数据,单位为 mG。
- 开始和停止内部记录以及请求内部记录状态。记录支持 RR 和 HR(1秒采样时间)或 HR(5秒采样时间)。
- 存储、读取和删除存储的内部记录(传感器一次只能支持一个记录)。
H9心率传感器
可靠的高质量心率胸带。 商品页面
H9 SDK支持的功能
- 心率(每分钟跳动次数)和RR间隔(毫秒)。
- 心率广播。
Polar Verity Sense 光学心率传感器
光学心率传感器是一种可充电设备,使用LED技术测量用户的心率。 商品页面
通过SDK可用的Polar Verity Sense功能
- 每分钟心跳次数。
- 心率广播。
- 光电容积描记图(PPG)值。
- PP间隔(毫秒),代表从PPG信号中提取的心搏间期。
- 加速度计数据,采样率为52Hz,范围为8G。轴特定加速度数据以mG为单位。
- 陀螺仪数据,采样率为52Hz,范围为250dps、500dps、1000dps和2000dps。轴特定陀螺仪数据以dps为单位。
- 磁力计数据,采样率为10Hz、20Hz、50Hz和100Hz,范围为±50高斯。轴特定磁力计数据以高斯为单位。
- SDK模式(从版本1.1.5开始)
- 离线记录(从版本2.1.0开始)
OH1 光学心率传感器
光学心率传感器是一种可充电设备,利用LED技术测量用户心率。商店页面
SDK可用的OH1功能
- 每分钟心跳次数。
- 心率广播。
- 光电容积描记图(PPG)值。
- PP间隔(毫秒),代表从PPG信号中提取的心搏间期。
- 加速度计数据,采样率为50Hz,范围为8G。轴特定加速度数据以mG为单位。
项目结构
- polar-sdk-ios 包含了 iOS SDK 源码的文档
- polar-sdk-android 包含了 Android SDK 源码的文档
- demos 包含 Android 心电图演示应用
- examples 包含了 Android 和 iOS 演示应用,利用了 SDK 的大部分功能
- documentation 包含与 SDK 相关的文档
Android 入门
详细文档: 文档
安装
- 在
build.gradle
中确保将 minSdk 设置为 24 或更高版本。
android {
...
defaultConfig {
...
minSdk 24
}
}
- 将 JitPack 仓库添加到您的仓库设置
...
repositories {
...
maven { url 'https://jitpack.io' }
...
}
}
- 添加 Polar BLE SDK 库的依赖项。您还需要添加 RxJava 依赖项以使用 Polar BLE SDK 库
dependencies {
implementation 'com.github.polarofficial:polar-ble-sdk:${sdk_version}'
implementation 'io.reactivex.rxjava3:rxjava:3.1.6'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.2'
}
- 最后,为了让 SDK 使用蓝牙,需要 蓝牙相关权限。在您的应用
AndroidManifest.xml
中,需要列出以下权限
<!-- Polar SDK needs Bluetooth scan permission to search for BLE devices. Polar BLE SDK doesn't use the scan
to decide the location so "neverForLocation" permission flag can be used.-->
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<!-- Polar SDK needs Bluetooth connect permission to connect for found BLE devices.-->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<!-- Allows Polar SDK to connect to paired bluetooth devices. Legacy Bluetooth permission,
which is needed on devices with API 30 (Android Q) or older. -->
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<!-- Allows Polar SDK to discover and pair bluetooth devices. Legacy Bluetooth permission,
which is needed on devices with API 30 (Android Q) or older. -->
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<!-- Polar SDK needs the fine location permission to get results for Bluetooth scan. Request
fine location permission on devices with API 30 (Android Q). Note, if your application
needs location for other purposes than bluetooth then remove android:maxSdkVersion="30"-->
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
<!-- The coarse location permission is needed, if fine location permission is requested. Request
coarse location permission on devices with API 30 (Android Q). Note, if your application
needs location for other purposes than bluetooth then remove android:maxSdkVersion="30" -->
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="30" />
在您的应用中,您必须请求 权限。以下是如何请求 SDK 需要的权限的示例
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requestPermissions(arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_CODE)
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_CODE)
}
代码示例:心率
请查看 示例 文件夹以查看完整项目。
关键事项
- 加载默认API实现并添加回调。
// NOTICE in this code snippet all the features are enabled.
// You may enable only the features you are interested
val api: PolarBleApi = PolarBleApiDefaultImpl.defaultImplementation(applicationContext,
setOf(PolarBleApi.PolarBleSdkFeature.FEATURE_HR,
PolarBleApi.PolarBleSdkFeature.FEATURE_POLAR_SDK_MODE,
PolarBleApi.PolarBleSdkFeature.FEATURE_BATTERY_INFO,
PolarBleApi.PolarBleSdkFeature.FEATURE_POLAR_H10_EXERCISE_RECORDING,
PolarBleApi.PolarBleSdkFeature.FEATURE_POLAR_OFFLINE_RECORDING,
PolarBleApi.PolarBleSdkFeature.FEATURE_POLAR_ONLINE_STREAMING,
PolarBleApi.PolarBleSdkFeature.FEATURE_POLAR_DEVICE_TIME_SETUP,
PolarBleApi.PolarBleSdkFeature.FEATURE_DEVICE_INFO)
)
)
api.setApiCallback(object : PolarBleApiCallback() {
override fun blePowerStateChanged(powered: Boolean) {
Log.d("MyApp", "BLE power: $powered")
}
override fun deviceConnected(polarDeviceInfo: PolarDeviceInfo) {
Log.d("MyApp", "CONNECTED: ${polarDeviceInfo.deviceId}")
}
override fun deviceConnecting(polarDeviceInfo: PolarDeviceInfo) {
Log.d("MyApp", "CONNECTING: ${polarDeviceInfo.deviceId}")
}
override fun deviceDisconnected(polarDeviceInfo: PolarDeviceInfo) {
Log.d("MyApp", "DISCONNECTED: ${polarDeviceInfo.deviceId}")
}
override fun bleSdkFeatureReady(identifier: String, feature: PolarBleApi.PolarBleSdkFeature) {
Log.d(TAG, "Polar BLE SDK feature $feature is ready")
}
override fun disInformationReceived(identifier: String, uuid: UUID, value: String) {
Log.d("MyApp", "DIS INFO uuid: $uuid value: $value")
}
override fun batteryLevelReceived(identifier: String, level: Int) {
Log.d("MyApp", "BATTERY LEVEL: $level")
}
})
- 请求权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requestPermissions(arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_CODE)
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_CODE)
}
// callback is invoked after granted or denied permissions
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
}
- 当需要时进行清理功能,例如
public override fun onDestroy() {
super.onDestroy()
api.shutDown()
}
- 使用
api.connectToDevice(<DEVICE_ID>)
连接到Polar设备,其中 <DEVICE_ID> 是打印到您的传感器的设备ID,使用api.autoConnectToDevice(-50, null, null).subscribe()
连接附近的设备,或使用api.searchForDevice()
扫描并选择设备
开始iOS使用
详细文档:文档。最小iOS版本为14。
要求
- Xcode 12.x
- Swift 5.x
依赖项
- RxSwift 6.0 或更高版本
- Swift Protobuf 1.18.0 或更高版本
安装
CocoaPods
如果您使用 CocoaPods 来管理您的依赖项,则在您的 Podfile
中添加 PolarBleSdk
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'PolarBleSdk', '~> 5.0'
end
Swift Package Manager
将 PolarBleSdk 添加为依赖项到您的 Package.swift
清单文件中
dependencies: [
.package(name: "PolarBleSdk", url: "https://github.com/polarofficial/polar-ble-sdk.git", .upToNextMajor(from: "5.0.0"))
]
或者使用 XCode 包管理器 将 Swift 包添加到您的项目中。
Carthage
如果您使用 Carthage 来管理依赖项,则在您的 Cartfile
中添加 PolarBleSdk
github "polarofficial/polar-ble-sdk" ~> 5.0
$ carthage update --use-xcframeworks
设置您的应用程序
- 在您的项目目标设置中启用 后台模式,添加 使用蓝牙 LE 外设
- 在您的项目目标属性列表中添加键 NSBluetoothAlwaysUsageDescription
代码示例:心率
查看示例文件夹以获取完整项目。
关键事项
deviceId
是您的Polar设备的ID。如果您使用自动连接,则不需要此ID。
- 导入所需的包。
import PolarBleSdk
import RxSwift
- 加载默认的API实现并实现所需的协议。
class MyController: UIViewController,
PolarBleApiObserver,
PolarBleApiPowerStateObserver,
PolarBleApiDeviceInfoObserver,
PolarBleApiDeviceFeaturesObserver,
PolarBleApiDeviceHrObserver {
// NOTICE only FEATURE_HR is enabled, to enable more features like battery info
// e.g. PolarBleApiDefaultImpl.polarImplementation(DispatchQueue.main, features: Features.hr.rawValue |
// Features.batteryStatus.rawValue)
// batteryLevelReceived callback is invoked after connection
var api = PolarBleApiDefaultImpl.polarImplementation(DispatchQueue.main, features: Features.hr.rawValue)
var deviceId = "0A3BA92B" // TODO replace this with your device id
override func viewDidLoad() {
super.viewDidLoad()
api.observer = self
api.deviceHrObserver = self
api.powerStateObserver = self
api.deviceFeaturesObserver = self
api.deviceInfoObserver = self
}
func deviceConnecting(_ polarDeviceInfo: PolarDeviceInfo) {
print("DEVICE CONNECTING: \(polarDeviceInfo)")
}
func deviceConnected(_ polarDeviceInfo: PolarDeviceInfo) {
print("DEVICE CONNECTED: \(polarDeviceInfo)")
}
func deviceDisconnected(_ polarDeviceInfo: PolarDeviceInfo) {
print("DISCONNECTED: \(polarDeviceInfo)")
}
func batteryLevelReceived(_ identifier: String, batteryLevel: UInt) {
print("battery level updated: \(batteryLevel)")
}
func disInformationReceived(_ identifier: String, uuid: CBUUID, value: String) {
print("dis info: \(uuid.uuidString) value: \(value)")
}
func hrValueReceived(_ identifier: String, data: PolarHrData) {
print("HR notification: \(data.hr) rrs: \(data.rrs)")
}
func hrFeatureReady(_ identifier: String) {
print("HR READY")
}
func ftpFeatureReady(_ identifier: String) {
print("FTP ready")
}
func streamingFeaturesReady(_ identifier: String, streamingFeatures: Set<DeviceStreamingFeature>) {
for feature in streamingFeatures {
print("Feature \(feature) is ready.")
}
}
func blePowerOn() {
print("BLE ON")
}
func blePowerOff() {
print("BLE OFF")
}
}
- 使用
api.connectToDevice(id)
连接到Polar设备,或使用api.startAutoConnectToDevice(_ rssi: Int, service: CBUUID?, polarDeviceType: String?)
连接附近的设备,或使用api.searchForDevice()
进行扫描并选择设备。
迁移指南
合作
如果您希望与Polar进行商业合作,请点击此处。
许可
快速许可摘要 / SDK 的使用权利
您可以使用、复制和修改 SDK,只要在任何软件/源代码的副本中包含原始的版权和许可声明,并遵守许可条款。只要您遵守许可条款使用 SDK,您就可以用于开发个人以及商业用途的软件。
通过利用 SDK,您表示接受 许可。
第三方代码和许可
Polar BLE SDK 中使用的第三方代码和许可请参阅 第三方软件清单。