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在后台更加穩定。具體實現方法請自行查詢。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM