iOS藍牙APP常駐後臺

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

相關文章
相關標籤/搜索