iOS蓝牙类APP常驻后台的实现方法,通过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为:html
1.设置plist,蓝牙权限app
2.到target-capabilities-background modes中打开use Bluetooth LE accessories选项ide
3.建立central manager时设置restore identifierspa
_bluetoothmanager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:@{CBCentralManagerOptionRestoreIdentifierKey : CardReadingServiceManagerRestoreIdentifier}];
4.appdelegate的didfinishlaunching方法中,若是检测到对应的key就从新建立Bluetooth managerrest
for (NSString *blue in centralManagerIdentifiers) { if ([blue isEqualToString:CardReadingServiceManagerRestoreIdentifier]) { [CardReadingService getInstance].bluetoothmanager = nil; [[CardReadingService getInstance] bluetoothmanager]; break; } }
5.实现Bluetooth central delegate的willRestoreState方法,开启扫描code
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *,id> *)dict { [self startScan]; [self startAccleerometer]; }
以上方法是从开发者文档中找到的,对应的连接orm
可是到iOS12以后,发现不能长期保持后台,不知道是否是系统又对应用后台作了限制,改进方法还在研究中。htm
在应用中添加后台应用刷新,可以使app在后台更加稳定。具体实现方法请自行查询。blog