백그라운드에서 BLE와 연결을 시도하고 있지만 백그라운드에서 연결하지 않았습니다. 내 앱이 포 그라운드에있을 때 작동합니다. 주변기기의 UUID로 스캔하려고합니다. 첨부 된 코드는 다음과 같습니다.신속한 백그라운드 스캔 BLE
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var msg = ""
switch central.state {
case .poweredOff:
msg = "Bluetooth is Off"
case .poweredOn:
msg = "Bluetooth is On"
let arrayOfServices: [CBUUID] = [CBUUID(string: "CCAExxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
manager?.scanForPeripherals(withServices:arrayOfServices, options: nil)
case .unsupported:
msg = "Not Supported"
default:
msg = "Not Connected"
}
print("STATE: " + msg)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Name: \(peripheral.name)") //print the names of all peripherals connected.
//you are going to use the name here down here ⇩
if peripheral.name == "Name of device" { //if is it my peripheral, then connect
self.myBluetoothPeripheral = peripheral //save peripheral
self.myBluetoothPeripheral.delegate = self
manager.stopScan() //stop scanning for peripherals
manager.connect(myBluetoothPeripheral, options: nil) //connect to my peripheral
}
}
어떻게 해결할 수 있습니까?
왜 백그라운드에서 장치에 연결 하시겠습니까? 당신이 백그라운드에서 연결할 수 있다면 당신이 원하는 것을 정확히 말해 줄 수 있습니까? – EmirC
그것은 일부 데이터, 즉 하트 비트를 전송하기위한 것이거나 모든 데이터 일 수 있습니다 .i scanForPeripherals method.is에서 options 매개 변수에 nil을 지정 했습니까? – user12346
장치 이름을 확인하고 무엇이 발생하는지 보려면 if를 제거하십시오. 알려진 주변 장치를 인식하려면 이름이 아닌 식별자를 사용해야합니다. 배경에서 스캔 할 때 이름을 사용할 수 없습니다. – Paulw11