バックグラウンドで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メソッドのoptionsパラメータにnilを指定しています。 – user12346
デバイス名を確認するifを削除し、何が起こるかを確認します。既知の周辺機器を認識するためには、名前ではなく識別子を使用する必要があります。バックグラウンドでスキャンするときは名前を使用できません – Paulw11