Microsoft Band 开发 (2) – 传感器

距离上一次发文已经过去三个月了,三个月里发生了好多事情,然后忙这个忙那个也都没时间继续把这个有关 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/s2)


double


AccelerationY

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


double


AccelerationZ

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

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 的心率传感器还挺准?不知道。

时间: 2024-07-30 05:34:58

Microsoft Band 开发 (2) – 传感器的相关文章

Microsoft Band 开发 (1) – 连接设备并进行简单操作

一.准备工作 去年10月,微软发布了微软手环,并在几个月后发布了相应平台的SDK并在持续更新中.今天,我们就来说说在Windows Phone当中进行MS Band应用的开发. 所谓Band开发,你写的App其实在根本上是运行在你手机上的,Band只用于呈现信息.采集信息和显示通知.当然,Band上强大的传感器也可以帮助你构建更牛逼的健康类应用.我就抛砖引玉地来给大家简单说说Band开发的所需的基础知识. 首先,创建一个WP8.1(RT)的空白项目,下载并引用Band SDK(可以在Nuget上

Microsoft Azure开发体验 – 网络报名系统

去年底RP好抢到了中国版Azure的使用机会,最近社团里讨论到9月份招新的问题,就用Azure Website和Azure Table Storage打造了这个报名系统. 网站放在 http://joinzjazure.chinacloudsites.cn/ ,一个很简单的页面,用的是bootstrap的CSS,针对不同分辨率的屏幕做了优化,在手机上也可以有不错的体验. 基础知识 阅读本文,我假定你具有以下知识: C# 基础知识 ASP.NET MVC 基础知识 知道 Azure 的概念 ASP

IOS开发——手势 &amp; 传感器 &amp; 物理引擎

这次思维导图比较杂,demo已经全部上传到github上,小编的github地址是:狂戳 先看下效果图: 手势画板: 物理引擎: 传感器: IOS开发--手势 & 传感器 & 物理引擎

iOS开发之──传感器使用 (转载)

在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API作一简单介绍. AD:WOT2015 互联网运维与开发者大会 热销抢票 在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API作一简单介绍 一. iPhone 4的传感器技术 进入正题,iPhone 4到底用上了哪些传感器呢? 1) 影像传感器 简单说就是相机镜头,由于只牵涉到微光学与微电子,没有机械成份在里头,即便加入马达.机械驱动的镜头,这

iOS开发之──传感器使用

本文转载至 http://mobile.51cto.com/iphone-423219.htm 在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API作一简单介绍. AD:2014WOT全球软件技术峰会北京站 课程视频发布 在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API作一简单介绍 一. iPhone 4的传感器技术 进入正题,iPhone 4到底用上了哪些传感器呢? 1) 影像传感器 简

安卓开发之传感器

和前面的位置服务基本一致的配置方法: SensorManager senserManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); Sensor sensor = senserManager.getDefaultSensor(Sensor.TYPE_LIGHT); //这里可以定义不同的传感器类型,光照,加速度,地磁.... sensorManager.registerListener(listener, senso

iOS开发之传感器

传感器 什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都是由传感器完成的 在地图应用中, 能判断出手机头面向的方向 一关灯, iPhone会自动降低亮度让屏幕显得不是那么刺眼 打电话时, 人脸贴近iPhone屏幕时, 屏幕会自动锁屏, 达到省电的目的 ... ... 传感器的类型 iPhone5中内置的传感器有 运动传感器\加速度传感器\加速计(Motion/A

Arduino和C51开发光敏传感器

技术:51单片机.Arduino.光敏传感器.PCF8591.AD/DA转换 概述 本文介绍了如何接收传感器的模拟信号和如何使用PCF8591 AD/DA转换模块对光敏传感器的模拟信号进行转换.讲述了51单片机和Arduino如何读取模拟信号,并通过串口实时显示出来. 详细 代码下载:http://www.demodashi.com/demo/14737.html 一.光敏传感器 光敏传感器是利用光敏元件将光信号转换为电信号的传感器,它的敏感波长在可见光波长附近,包括红外线波长和紫外线波长.光传

Android笔记----Android传感器开发

Android的传感器开发 Android的常用传感器 传感器应用案例         Android的传感器开发 1.1 开发传感器应用 开发传感器的步骤如下: 调用Context的getSystemService(Context.SENSOR_SERVICE)方法获取SensorManager对象. 调用SensorManager的getDefaultSensor(int type)方法来获取指定类型的传感器. 一般在Activity的onResume()方法中调用SensorManager