iOS ANCS 通知服务

#####导语

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

#####一,探讨Android &iOS 区别

  • Android端手机是通过NotificationListenerService服务来获取通知栏上的信息,当然这个服务需要获取到对应的权限.通过服务获取到的具体消息,然后通过BLE或者传统蓝牙传输到智能硬件端.然后智能硬件显示该消息
  • iOS 端手机是通过ANCS服务来获取对应消息,该服务在开机后就启动啦,只等订阅设备来订阅。智能硬件端订阅ANCS服务成功后,一旦有新消息,比如来电或者其它App推送消息,苹果通知中心NotificationCenter就推送一份到订阅设备上,同时往通知中心上推送一份。 总结如下结论: 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)

  • Notification Source:    UUID 9FBF120D-6301-42D9-8C58-25E699A21DBD(notifiable)    基本通知源,通知一些计数的信息,可以通过notification 获取uuid,
  • Control Point:    UUID 69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9(writeablewithresponse)    控制器,用于向ios设备写入控制信息,例如读取详情 ,接听来电,拒绝来电等
  • Data Source: UUID 22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB(notifiable)    数据源,用于提供详细数据,在控制信息写入后通过此characteristic返回

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

1)Notification Source

  • EventID Values 如下Table

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

  • EventFlags如下Table
EventFlags Values
EventFlagSilent= (1 << 0)
EventFlagImportant= (1 << 1)
EventFlagPreExisting= (1 << 2)
EventFlagPositiveAction= (1 << 3)
EventFlagNegativeAction= (1 << 4)
Reserved EventFlags= (= (1 << 5)–(1 << 7))
  • CategoryID 代表该消息的类型
CategoryID Values代表含义
CategoryIDOther= 0其他类别
CategoryIDIncomingCall= 1)来电消息
CategoryIDMissedCall= 2miss电话消息
CategoryIDVoicemail= 3voice mail消息
CategoryIDSocial= 4社交类消息 比如微信facebook等
CategoryIDSchedule=5schedule消息
CategoryIDEmail=6Email消息
CategoryIDNews=7News消息
CategoryIDHealthAndFitness=8健康类消息
CategoryIDBusinessAndFinance=9财经类消息
CategoryIDLocation=10location消息
CategoryIDEntertainment=11娱乐类消息
Reserved CategoryID values= 12–255其他类消息
  • CategoryCount:给定类型中活跃的通知的数量。例如,邮箱中有两封未读的邮件,这个时候又来了一封新的邮件,那么通知的邮件的数量将是3

  • NotificationUID: 32byte 唯一标示该消息.后续Control Point需要用这个NotificationUID,可以获取到具体的信息datasource.

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

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

  • CommandID: 设为零 (CommandIDGetNotificationAttributes),0x00
  • NotificationUID: 想要获得的通知的uid,32位数字是通知的唯一标示。
  • AttributeIDs:NC想要获得的属性列表。有些属性可能需要后面接一个16位的的参数,0xff 0xff。

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

 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流如下:

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

NotificationAttributeID Values

NotificationAttributeIDAppIdentifier Values代表含义
NotificationAttributeIDAppIdentifier= 0其他类别
NotificationAttributeIDTitle=1表示title
NotificationAttributeIDSubtitle=2subtitle
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交换流程

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值