Xcode 9.2 및 iOS 11.1.2에 문제가 있습니다. "didDiscover"가 정상적으로 작동하고 연결을 호출하기 전에 주변 장치가 배열에 저장되지만 코드에서,Bluetooth didConnect 또는 didFailToConnect가 iOS 11.1.2와 함께 Xcode 9.2로 호출되지 않았습니다.
var myPeripheral:CBPeripheral?
보다 : 이
var manager: CBCentralManager!
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
//manager = CBCentralManager (delegate: self, queue: DispatchQueue.main)
//manager = CBCentralManager.init(delegate: self, queue: nil, options:[:])
//manager.delegate = self
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
guard let name = peripheral.name else { return }
print("BR peripheral.name = \(name), rssi = \(RSSI), adertisementData = \(advertisementData)")
if (peripheral.name?.hasPrefix("testBT"))! {
peripheralArray.append(peripheral)
manager.connect(peripheralArray.first!, options: [:])
print("Connecting to peripheral \(peripheral)")
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("BT connected!")
manager.stopScan()
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("BT disconnected!")
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print("BT failed!")
}
아마도 문제가 아니지만 주변 장치의 주변 장치 인 peripheralArea.append (peripheral); central.connect (peripheralArray.last !, options : [:])'당신은'central.connect (peripheralArray.last !, 옵션 : [:])'을 의미 했습니까? – Larme
@Larme 지적 해 주셔서 고맙습니다. 나는 "중앙"에서 "관리자"로 바꿔서 다시 시도했지만 운은 없습니다. –