iOS藍牙類APP常駐後臺的實現方法,經過在蘋果開發者論壇詢問,以及查看蘋果開發者文檔,最後得出正確的方法為: 1.設置plist,藍牙許可權 2.到target-capabilities-background modes中打開use Bluetooth LE accessories選項 3.創建ce ...
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之後,發現不能長期保持後臺,不知道是不是系統又對應用後臺做了限制,改進方法還在研究中。