iOS难点总结iOS CorebluetoothiOS Developer

iOS ANCS 通知服务

2017-07-05  本文已影响408人  wongstar
导语

智能BLE硬件设备需要实时获取Android和iOS端通知,那他们分别是怎么实现的呢?

一,探讨Android &iOS 区别
二, 初见ANCS

ANCS(Apple Notification Center Service)意思是苹果通知中心服务,它是苹果提供给周边蓝牙设备(手环、手表等智能设备)通过BLE(低功耗蓝牙)访问iOS设备上的各类通知的一种机制.
ANCS协议中的通用属性协议(Generic Attribute Profile,GATT)协议实现的,它是GATT协议的一个子集。
在ANCS协议中,iOS设备作为server端,既数据提供者NP(Notification Provider),充当Peripheral端的角色.
周边设备如智能手表或者手环作为client端即数据消费者Notification Consumer (NC),充当Central,主要是用来连接和使用ANCS server提供的服务,如何从服务中获取对应的信息。
ANCS作为Perpheral暴露给外界设备的服务UUID是7905F431-B5CE-4E99-A40F-4B1E122D00D0,所以通过该service就能获取到对应的特征值,然后获取特征值里面的内容。
ANCS给我们提供了下面3种特征值(Characteristic)

所以智能BLE设备需要订阅Notification Source 和 Control Point 来获取DataSource数据源信息,下面对上面Characteristic特征值进行详细解释.

1)Notification Source

Notification Source.png

| EventID Values | 值 | 代表含义|
| ------------- |:-------------:|
| EventIDNotificationAdded | =0 | 消息是新来的|
| EventIDNotificationModified | =1 |消息是修改的 |
| EventIDNotificationRemoved | =2 |移除消息 |
| Reserved EventID values | = 3–255 | 其它|

EventFlags Values
EventFlagSilent = (1 << 0)
EventFlagImportant = (1 << 1)
EventFlagPreExisting = (1 << 2)
EventFlagPositiveAction = (1 << 3)
EventFlagNegativeAction = (1 << 4)
Reserved EventFlags = (= (1 << 5)–(1 << 7))
CategoryID Values 代表含义
CategoryIDOther = 0 其他类别
CategoryIDIncomingCall = 1) 来电消息
CategoryIDMissedCall = 2 miss电话消息
CategoryIDVoicemail = 3 voice mail消息
CategoryIDSocial = 4 社交类消息 比如微信facebook等
CategoryIDSchedule =5 schedule消息
CategoryIDEmail =6 Email消息
CategoryIDNews =7 News消息
CategoryIDHealthAndFitness =8 健康类消息
CategoryIDBusinessAndFinance =9 财经类消息
CategoryIDLocation =10 location消息
CategoryIDEntertainment =11 娱乐类消息
Reserved CategoryID values = 12–255 其他类消息

比如下面消息就是获取到一个

2)Control Point
NC设备需要更多信息,这个时候可以用Control Point获取通知属性命令使得NC可以得到某个特定通知的详细属性,比如短信的发送人,短信内容,时间,App name 等。

image.png

想要获取具体某条信息可以通过下面的方法

 public static byte[] getNotificationByUID(byte[] uid){
   byte appNameCapacity=100;//app name 最长容量
   byte  titleCapacity=50;//title 最长
   byte  subtitleCapactiy=100;//subtitle
   byte msgCapacity=248;//msg最大容量
   byte size=10;
   byte dateCapacity=10;
   ByteArrayOutputStream bout=new ByteArrayOutputStream();
   bout.write(0);//注意此处是commonID 一般情况是0
   bout.write(uid),//把notification uid 传过来
   bout.write(1);
   bout.write(titleCapacity);
   bout.write(0);
   bout.write(2);
   bout.write(subtitleCapactiy);
   bout.write(0);
   bout.write(3);
   bout.write(msgCapacity);
   bout.write(0);
   bout.write(4);
   bout.write(size);
   bout.write(0);
   bout.write(5);
   bout.write(dateCapacity);
   bout.write(0);
   bout.write(0);
   bout.write(appNameCapacity);
   byte[] req=bout.toByteArray();
   bout.reset();
   return req;
}

通过Control Point 传输上面的byte 流给iOS NSNotification center,iOS会根据传输过来的byte流,传回具体信息到设备订阅端,设备通过回调函数解析传过来的byte流。下面获取到byte流如下:

image.png

通过上面的byte数据结构可以写出对应的parse函数。获取具体的数据。通过1Byte AttritbuteID 知道是具体哪个NotificationAttributeID Values,2个byte AttributeLength 知道后面data有几位byte ,byte 数据 紧跟在AttributeLength后面.

NotificationAttributeID Values

NotificationAttributeIDAppIdentifier Values 代表含义
NotificationAttributeIDAppIdentifier = 0 其他类别
NotificationAttributeIDTitle =1 表示title
NotificationAttributeIDSubtitle =2 subtitle
NotificationAttributeIDMessage =3 具体Message
NotificationAttributeIDMessageSize =4 具体Message大小
NotificationAttributeIDDate =5 具体date
NotificationAttributeIDPositiveActionLabel =6
NotificationAttributeIDNegativeActionLabel =7
Reserved NotificationAttributeID values =8-255

Note NotificationAttributeIDDate 获取对应的数据格式是yyyyMMdd'T'HHmmSS,所以获取到对应的Date,要用这种格式来解析.

3)整体NP和NC交换流程

image.png

4)Notification Actions
从iOS8开始,NP发送的iOS通知起始可以间接的告诉NC可执行哪些动作。接着,NC就可以针对指定的iOS通知,请求NP执行一个动作。
通知源特征上生成的GATT通知包含一个叫做Eventflags的数据域,NC根据这个数据域就可得知对一条iOS通知可以执行哪些操作:

EventFlagPositiveAction:积极动作(Positive Action),与iOS通知相关。
EventFlagNegativeAction:消极动作(Negative Action),与iOS通知相关。

比如iPhone来电啦,接听来电可以用EventFlagPositiveAction action来
拒绝来电的时候用EventFlagNegativeAction action来.
通过下面代码就可以实现具体挂电话or接话等操作:

public static byte[] getActionCommend(byte[] uid,int actionId){
  ByteArrayOutputStream out=new ByteArrayOutputStream();
  out.write(2);
  out.write(uid);
  out.write(actionId);
  byte [] req=out.toByteArray();
  out.reset();
  return req;
}

ActionID Values :

ActionID Values
ActionIDPositive =0
ActionIDNegative =1
Reserved ActionID values =2-255
上一篇 下一篇

猜你喜欢

热点阅读