设为首页 加入收藏

TOP

iOS蓝牙APP常驻后台
2019-08-31 00:19:45 】 浏览:34
Tags:iOS 蓝牙 APP 常驻 后台

iOS蓝牙类APP常驻后台的实现方法,经过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为:

1.设置plist,蓝牙权限

2.到target-capabilities-background modes中打开use Bluetooth LE accessories选项

3.创建central manager时设置restore identifier

_bluetoothmanager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:@{CBCentralManagerOptionRestoreIdentifierKey : CardReadingServiceManagerRestoreIdentifier}];

4.appdelegate的didfinishlaunching方法中,如果检测到对应的key就重新创建Bluetooth manager

for (NSString *blue in centralManagerIdentifiers) {
     if ([blue isEqualToString:CardReadingServiceManagerRestoreIdentifier]) {
           [CardReadingService getInstance].bluetoothmanager = nil;
           [[CardReadingService getInstance] bluetoothmanager];
           break;
        }
   }

5.实现Bluetooth central delegate的willRestoreState方法,开启扫描

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *,id> *)dict {
    [self startScan];
    [self startAccleerometer];
}

 

以上方法是从开发者文档中找到的,对应的链接

 

但是到iOS12之后,发现不能长期保持后台,不知道是不是系统又对应用后台做了限制,改进方法还在研究中。

 

在应用中添加后台应用刷新,可使app在后台更加稳定。具体实现方法请自行查询。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS开发简记(3):tips提示 下一篇Object C学习笔记17-动态判断和选..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目