苹果蓝牙后台的限制,原本广播会有两个段分别是localName和serviceUUID这两块,但现在后台广播时,是不发送在这两段的

手机app可以作为一个蓝牙外设端来模拟外设硬件,但广播包里的数据只能包含localName和serviceUUID,相对于外设硬件来说还是有一些不足之处。

一个128位的蓝牙UUID来标示

32个 x 是 0-9 或 a-f 范围内的一个十六进制的数字(0x00),X是字符串格式

把数据按uuid的格式加进去

self.peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey:serviceUUIDs,CBAdvertisementDataNameKey:localName}];

对应的值是数组

key: kCBAdvDataIsConnectable, value: 1

key: kCBAdvDataLocalName, value: SimpleBLEPeripheral

key: kCBAdvDataServiceUUIDs    //数据就在这里

       uuid(0): FF F0

key: kCBAdvDataTxPowerLevel, value: 0

Manufacturer Specific Data

NSArray *keys = [advertisementData allKeys];

NSData *dataAmb, *dataObj;

//获取所有的key

for (int i = 0; i < [keys count]; ++i) {

id key = [keys objectAtIndex: i];

NSString *keyName = (NSString *) key;

NSObject *value = [advertisementData objectForKey: key];

//处理所有的value

if ([value isKindOfClass: [NSArray class]]) {

printf("   key: %s\n", [keyName cStringUsingEncoding: NSUTF8StringEncoding]);

NSArray *values = (NSArray *) value;

//处理每个value里面的值

for (int j = 0; j < [values count]; ++j) {

if ([[values objectAtIndex: j] isKindOfClass: [CBUUID class]]) {

CBUUID *uuid = [values objectAtIndex: j];

NSData *data = uuid.data;  //获取到  uuid.data

if (j == 0) {

dataObj = uuid.data;

} else {

dataAmb = uuid.data;

}

printf("      uuid(%d):", j);

for (int j = 0; j < data.length; ++j)

printf(" %02X", ((UInt8 *) data.bytes)[j]);

printf("\n");

} else {

const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];

printf("      value(%d): %s\n", j, valueString);

}

}

} else {

const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];

printf("   key: %s, value: %s\n", [keyName cStringUsingEncoding: NSUTF8StringEncoding], valueString);

}

}

蓝牙周边后台执行模式

想要作为一个周边角色在后台工作,你需要在Info.plist文件中添加bluetooth-periphral到UIBackgroundModes关键字下。当你这么做了,系统会在你的app需要读,写,订阅事件的时候唤醒它。

除了可以在后台唤醒app处理连接的中心的读写订阅。蓝牙中心库还可以允许你的app在后台的时候广播。但是你需要了解app在后台的广播和在前台的广播状态不太一样。特别的,当你的app在后台广播时。

CBAdvertisementDataLocalNameKey 广告键是被忽略的,而且local name也不会被广播的

所以 CBAdvertisementDataServiceUUIDsKey中的服务UUID被放在一个“溢出”区,它们只能被明确搜索的iOS设备搜索到。

如果所有app都在后台广播,你的app的包广播频率会变少。

When you start advertising peripheral data, the peripheral manager calls the peripheralManagerDidStartAdvertising(_:error:) method of its delegate object.

Data advertising is done on a “best effort” basis, because space is limited and there may be multiple apps advertising simultaneously. While your app is in the foreground, it can use up to 28 bytes of space in the initial advertisement data for any combination of the supported advertising data keys. If this space is used up, there are an additional 10 bytes of space in the scan response that can be used only for the local name (represented by the value of the CBAdvertisementDataLocalNameKey key). Note that these sizes do not include the 2 bytes of header information that are required for each new data type. Any service universally unique identifiers (UUIDs) contained in the value of the CBAdvertisementDataServiceUUIDsKey key that do not fit in the allotted space are added to a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them. While your app is in the background, the local name is not advertised and all service UUIDs are placed in the overflow area. The exact format of advertising and response data is defined in the Bluetooth 4.0 specification, Volume 3, Part C, Section 11.

当开始广告外围设备数据时,外围设备管理器调用其委托对象的外围信息管理器didstartadvertising (_:error:)方法。

数据广告是在“尽最大努力”的基础上进行的,因为空间有限,同时可能有多个应用程序在做广告。当你的应用程序在前台时,它可以在初始广告数据中使用最多28字节的空间来组合支持的广告数据键。如果耗尽了这个空间,那么扫描响应中还有额外的10字节空间,只能用于本地名称(由CBAdvertisementDataLocalNameKey键值表示)。注意,这些大小不包括每个新数据类型所需的2个字节的头信息。在特定的“溢出”区域中添加不适合于分配的空间的CBAdvertisementDataServiceUUIDsKey键值中包含的任何服务通用惟一标识符(uuid);只有当iOS设备显式地扫描它们时,才能发现它们。当您的应用程序在后台时,本地名称没有广告,所有服务uuid都放置在溢出区域。广告和响应数据的确切格式定义在蓝牙4.0规范第3卷C部分第11节中。

在初始化中心或者周边管理者的时候选择是否需要支持状态的保存和恢复

在初始化时指定CBCentralManagerOptionRestoreIdentifierKey选项,并为中心管理者提供一个字符串作为“恢复标识”就可以了:

myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey: @"myCentralManagerIdentifier" }];

CBPeripheralManagerOptionRestoreIdentifierKey

恢复你的中心和周边管理者

当你的app在后台被系统重启时,你的第一件事就是根据“恢复标识”恢复适当的中心和周边管理者就像他们第一次创建时一样

当你的app被系统重启时,你可以检索系统为你的应用程序保留的中央管理器对象的所有恢复标识符,像这样:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];

在拿到恢复标示符之后,只需要遍历并恢复适当的中央管理者。

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)state { NSArray *peripherals = state[CBCentralManagerRestoredStatePeripheralsKey]; ...

iOS 作为蓝牙外设广播信息相关推荐

  1. linux获得蓝牙外设mac,iOS获取蓝牙外设Mac地址

    #pragma mark 发现周边蓝牙服务里的特征 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsFo ...

  2. iOS swift 蓝牙详解(蓝牙中心demo,蓝牙外设demo(可替代mac蓝牙串口调试工具),蓝牙中心框架,gif演示)

    持续更新中... 文章目录 1.gif演示 1.1 蓝牙中心app 1.2 蓝牙外设app(外设被一个设备连接后,还可以被另一个设备连接,但两个同时连会导致连接不稳定,容易断开) 1.3 写write ...

  3. linux获得蓝牙外设mac,iOS 与蓝牙操作如何获取MAC地址

    因为小弟搞了很长时间关于蓝牙设备得APP 所以对这边比较了解 因为iOS自从7以后就无法或取设备的MAC地址 网上有人说把它加到蓝牙的广播包里但是这样的话对硬件来说负担很大 而且芯片会不稳定.我这边是 ...

  4. IOS用coreBluetooth库连接蓝牙外设(Andriod设备作为外设设备)出现The connection has timed out unexpectedly.

    情形:公司IOS项目用coreBluetooth库连接蓝牙外设(Andriod设备作为外设设备)出现The connection has timed out unexpectedly,但是androi ...

  5. 蓝牙BLE---DA14585修改常用的广播信息

    广播名修改: 广播间隔修改: MAC地址修改: 这是最常用的广播信息修改,还有其他的修改可参考官网的Trainning文档.

  6. iOS简易蓝牙对战五子棋游戏设计思路之一——核心蓝牙通讯类的设计

    iOS简易蓝牙对战五子棋游戏设计思路之一--核心蓝牙通讯类的设计 一.引言 本系列博客将系统的介绍一款蓝牙对战五子棋的开发思路与过程,其中的核心部分有两个,一部分是蓝牙通讯中对战双方信息交互框架的设计 ...

  7. iOS进阶--蓝牙技术

    声明:这篇文章关于蓝牙的相关知识的笔记,文章中会其他网上作者的资料.由于有些文章只做参考或统计不足,如涉及版权请在评论区留言~,我会及时更改 当下蓝牙开发可谓是越来越火,不论是智能穿戴的兴起还是物联网 ...

  8. iOS:蓝牙通讯开发快速上手

    1. 思维导图 蓝牙知识的结构图 蓝牙数据通讯流程图 2. 苹果对蓝牙设备的要求 BLE:bluetouch low energy,蓝牙4.0设备因为低功耗,所有也叫作 BLE.苹果在 iPhone ...

  9. iOS App 连接外设的几种方式

    原创作者: Max_Marry 文章地址: http://www.jianshu.com/p/852bf92c5c92 随着近年来车联网和物联网的兴起,智能家居和智能硬件的逐步火热,越来越多的 App ...

最新文章

  1. 分布式锁的使用与注意事项
  2. 北京关于领取2021年上半年合格证书的通知
  3. No slave process to process jobs, aborting 报错!!!
  4. Android之BaseAdapter—convertView回收机制与动态控件响应
  5. 外设驱动库开发笔记20:BME280压力湿度温度传感器驱动
  6. sql 一列中平均应发工资_劳动者的工资标准,应如何认定?
  7. 从马云看“穷男人”如何创业——看后信心倍增!
  8. Python | threading02 - 互斥锁解决多个线程之间随机调度,造成“线程不安全”的问题。
  9. 两化融合:唐山探路重工业城市智慧转型
  10. mongodb的分布式集群(4、分片和副本集的结合)
  11. android控制wifi,基于 Android 手机操作和控制的 Wifi 小车程序设计
  12. HCIA--路由交换
  13. HSPICE求导语句
  14. 中兴新支点操作系统_中兴新支点操作系统的设计和功能怎样?
  15. HEVC中CU、TU、PU划分和扫描方式简析
  16. 爱普生Epson Stylus Photo T60 打印机驱动
  17. 网络协议梳理(三)(网关和路由器、动态路由算法、Bellman-Ford算法、Dijkstra算法、动态路由协议、TCP和UDP)
  18. 2020互联网大厂职级对应薪资一览表
  19. 你为什么不敢重构代码?
  20. Engineering Dynamics 3 --- 转动惯量

热门文章

  1. EXCEl2013 创建下拉菜单
  2. 多语言 - 国际化处理 上
  3. MySQL:HINT
  4. 如何找到google主题的壁纸
  5. jq onclick 定义_jquery onclick函数未定义(jquery onclick function not defined)
  6. 中英文切换遇到的坑-总结
  7. Django mako 的使用(七)
  8. DIV背景半透明 样式
  9. 恢复IDEA中误删的文件
  10. Atcoder abc A~E