Microsoft Band 开发 (2) – 传感器

2015-10-19 14:56:36来源:作者:人点击

距离上一次发文已经过去三个月了,三个月里发生了好多事情,然后忙这个忙那个也都没时间继续把这个有关 Band 开发的文章继续下去,今天正好有些时间,就把关于读取传感器数据的一些事情说了吧。

还有就是在 10 月 6 日的发布会上,微软发布了第二代微软手环,传感器更加丰富,使用曲面屏幕,以及各种牛逼的功能。就算在这里做个广告(不过能找到这篇文章的也都应该知道这件事情了吧。

然后这是 Band 2 的宣传片(中文字幕)

http://www.bilibili.com/video/av3021636/

第二代 MSBand 的开发模式和第一代相差不大,所以之前 Band 1 开发所需的知识和 2 是通用的,包括 SDK 都是一样的。

好了扯了好多废话哈,那就赶快进入正题。

在微软手环上有如下传感器可供开发者使用:

Accelerometer

加速度计

Altimeter

高度计

Ambient Light

氛围光传感器

Barometer

大气压计

Calories

卡路里

Gyroscope

陀螺仪

Heart Rate

心率传感器

Pedometer

计步器

RR Interval

心跳周期(间隔?)

Skin Temperature

皮肤温度

UV

紫外线传感器

Distance

距离传感器

GSR

皮肤电阻传感器

这是微软文档中给出的各个传感器的属性

1.1.1 Accelerometer Type Property and description

double

AccelerationX

The x-axis acceleration of the Band in g units (9.81 m/s 2 )

double

AccelerationY

The y-axis acceleration of the Band in g units (9.81 m/s 2 )

double

AccelerationZ

The z-axis acceleration of the Band in g units (9.81 m/s 2 )

1.1.2 Altimeter Type Property and description

long

FlightsAscended

Number of floors ascended since the Band was last factory-reset

long

FlightsDescended

Number of floors ascended since the Band was last factory-reset

float

Rate

The current rate of ascend/descend in cm/s

long

SteppingGain

Total elevation gained in centimeters by taking steps since the Band was last factory-reset

long

SteppingLoss

Total elevation lost in centimeters by taking steps since the Band was last factory-reset

long

StepsAscended

Total number of steps ascended since the Band was last factory-reset

long

StepsDescended

Total number of steps descended since the Band was last factory-reset

long

TotalGain

Total elevation gained in centimeters since the Band was last factory-reset

long

TotalLoss

Total elevation loss in centimeters since the Band was last factory-reset

1.1.3 AmbientLight Type Property and description

int

Brightness

Current ambient light in lumens per square meter (lux)

1.1.4 Barometer Type Property and description

double

AirPressure

Current air pressure in hectopascals

double

Temperature

Current temperature in degrees Celsius

1.1.5 Calories Type Property and description

long

Calories

Number of kilocalories (kcals) burned since the Band was last factory-reset

1.1.6 Contact Type Property and description

BandContactState

State

Current contact state of the Band

1.1.6.1 enum BandContactState Value and description NotWorn

Value representing that the Band is currently not being worn

Unknown

Value representing that the contact state of the Band is unknown

Worn

Value representing that the Band is currently being worn

1.1.7 Distance Type Property and description

MotionType

CurrentMotion

The current MotionType of the Band

float

Pace

Current pace of the Band in ms/m

float

Speed

Current speed of the Band in cm/s

long

TotalDistance

Distance traveled in cm since the Band was last factory-reset

1.1.7.1 enum MotionType Value and description Unknown

Value representing that the mode of the Band user’s movement is currently unknown

Idle

Value representing that the user of the Band is currently idle

Walking

Value representing that the user of the Band is currently walking

Jogging

Value representing that the user of the Band is currently jogging

Running

Value representing that the user of the Band is currently running

1.1.8 Gsr Type Property and description

int

Resistance

Current skin resistance in kohms of the person wearing the Band

1.1.9 Gyroscope Type Property and description

double

AngularVelocityX

Angular velocity around the x-axis of the Band in degrees/sec

double

AngularVelocityY

Angular velocity around the y-axis of the Band in degrees/sec

double

AngularVelocityZ

Angular velocity around the z-axis of the Band in degrees/sec

1.1.10 HeartRate Type Property and description

int

HeartRate

Current heart rate as read by the Band in beats/min

HeartRateQuality

Quality

Quality of the current heart rate reading

1.1.10.1 enum HeartRateQuality Value and description Acquiring

Value representing that the Band hasn’t locked on to its user’s heart rate

Locked

Value representing that the Band has locked on to its user’s heart rate

1.1.11 Pedometer Type Property and description

long

TotalSteps

Total number of steps taken since the Band was last factory-reset

1.1.12 RRInterval Type Property and description

double

Interval

Current RR interval in seconds as read by the Band

1.1.13 SkinTemperature Type Property and description

double

Temperature

Current temperature in degrees Celsius of the person wearing the Band

1.1.14 UV Type Property and description

UVIndexLevel

IndexLevel

Current UVIndexLevel value as calculated by the Band

1.1.14.1 enum UVIndexLevel Value and description None

Value representing a very low UV index level

Low

Value representing a low UV index level

Medium

Value representing a medium UV index level

High

Value representing a high UV index level

VeryHigh

Value representing a very high UV index level

我抽空会吧这个表格整理出来方便大家使用,今天就先凑合看看,还没有翻译。

在获取传感器读数之前,要先将 Band 与手机配对并连接(关于如何和手环配对、连接和获取信息的内容,详见前一篇文章。)

using (IBandClient band = await BandClientManager.Instance.ConnectAsync(pairedBands[0])){}

检查 Band 是否支持该传感器(此处以心率传感器为例),以及用户是否授权应用获取该传感器。如果不支持该传感器,则抛出异常。

如果用户尚未同意应用获取传感器,则弹出请求用户同意的对话框。

代码如下:

if (band.SensorManager.HeartRate.IsSupported){ if (band.SensorManager.HeartRate.GetCurrentUserConsent() == UserConsent.Granted) { //获取读数 } else { await band.SensorManager.HeartRate.RequestUserConsentAsync(); }}else{ throw new Exception("Not supported.");}

控制 Band 传感器读取开始和结束的两个方法分别是: StartReadingsAsync() 和 StopReadingsAsync()

IBandSensor<T> 包含一个事件“ ReadingChanged ”。每当传感器的状态发生改变时(开始读取时、停止读取时),都会触发这个事件。所以,我们要利用这个事件,来做一个简单的能够读取用户 1 分钟内心跳的小 APP 。在这一分钟里,每隔一秒都会触发一次这个事件,然后只要在触发时将当前心率添加到一个储存心率变化的 List 中即可。但是又如何实现这个持续读取 1 分中呢 ?

众所周知, Task 类有一个 Delay 方法,利用这个方法就可以实现不阻塞 UI 线程情况下的延迟

Task.Delay(TimeSpan delay);

这里我们要让它等待一分钟,所以就是:

await Task.Delay(TimeSpan.FromSeconds(60));

这里的事件订阅,直接用 Lambda 写就 OK 了 ~

这样:

band.SensorManager.HeartRate.ReadingChanged += (sender, args) =>{}

这里的 args 的类型是 BandSensorReadingEventArgs<IBandHeartRateReading> 它的属性 SensorReading.HeartRate 便是我们需要的心率数值。其他传感器大都同理,不再赘述。

再梳理一下思路吧:

获取已配对的Band列表 --> 选择列表中第一个进行Band连接 -->判断设备是否支持心率传感器 -->如果是,向用户请求获取传感器的授权 -->订阅事件“ReadingChanged”(每秒获取一个读数并添加到 valueList 中) -->开始读取传感器 -->Delay 1分钟 -->结束传感器读取

好了,基础性的介绍就到这里,思路也差不多顺了,那就开始写代码吧。

完整的代码如下 :

IList valueList = new List<Object>();IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();try{ using (IBandClient band = await BandClientManager.Instance.ConnectAsync(pairedBands[0])) {if (band != null){if (band.SensorManager.HeartRate.IsSupported){if (band.SensorManager.HeartRate.GetCurrentUserConsent() == UserConsent.Granted){band.SensorManager.HeartRate.ReadingChanged += (sender, args) =>{ valueList.Add(args.SensorReading.HeartRate); }; await band.SensorManager.HeartRate.StartReadingsAsync(); await Task.Delay(TimeSpan.FromSeconds(60)); await band.SensorManager.HeartRate.StopReadingsAsync();}else{await band.SensorManager.HeartRate.RequestUserConsentAsync();}}else{throw new Exception("Not supported.");}} } }catch{ return null;}return valueList;

嗯差不多就是这样,如果感觉代码渣的话, 那就对了 还请包涵

_(:з」∠)_

好了就这样~

然后如果要想把获得到的数据呈现给用户或者使用什么的,调用方法,然后遍历那个返回的 valueList 就行了。我就直接把它输出出来了。最后分享我的心率♥

听说 Band 的心率传感器还挺准?不知道。

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台