簡單記錄一下:
前提條件是已經(jīng)可以在后臺時收發(fā)藍(lán)牙數(shù)據(jù)毁枯。
在后臺斷開連接時,程序確實(shí)調(diào)用了:
central.scanForPeripheralsWithServices(nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey:true])
但是一直無法掃描到我需要連接的設(shè)備叮称,在網(wǎng)上找原因种玛,發(fā)現(xiàn)一個網(wǎng)站是這樣描述的:
When backgrounding, you can still scan and connect to a LE peripheral. You can scan for peripherals using scanForPeripheralsWithSerives by stating what service you are looking for in a Peripheral. You cannot do a general scan without stating at least one service in the passed dictionary. If you have a CFUUIDRef from a previous connection, you can call retrievePeripherals with the CFUUIDRef. Your delegate will receive a call to didRetrievePeripherals with a CBPeripheral. With the CBPeripheral you can then connect using connectPeripheral.
大意如下:
程序在后臺時,依然可以掃描并且連接藍(lán)牙設(shè)備瓤檐。我們可以用『scanForPeripheralsWithSerives』去掃描一個指定的設(shè)備(傳入特定的serviceUUIDs參數(shù))赂韵,不可以傳nil掃描周圍所有的設(shè)備,如果傳nil挠蛉,我們無法掃描到周圍的任何設(shè)備祭示。
當(dāng)我把代碼改成如下:
let uuid = CBUUID.init(string:ServicesUUID)
central.scanForPeripheralsWithServices([uuid], options: [CBCentralManagerScanOptionAllowDuplicatesKey:true])
程序就可以在后臺重連藍(lán)牙設(shè)備了。
關(guān)于我
我是可樂谴古,在職iOS開發(fā)质涛,業(yè)余時間獨(dú)立開發(fā)App,現(xiàn)有上架作品:Mini記賬
公眾號:沙拉可樂 分享獨(dú)立開發(fā)的干貨和背后的故事