我最近一直在尝试很多CoreBluetooth
,现在我想使用 L2CAP 通道。
为此,我设置了两个 iOS 应用程序,一个是外围应用程序,另一个是中央应用程序。
此时,当我运行它们时,这是我在Xcode
调试控制台中看到的。
在外围方面,我得到了这个:
peripheralManager(_:didAdd:error:)
peripheralManager(_:didPublishL2CAPChannel:error:)
PSM: 192
peripheralManagerDidStartAdvertising(_:error:)
在中央,我明白了:
centralManagerDidUpdateState
centralManager(_:didDiscover:advertisementData:rssi:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-11 ....] [CoreBluetooth] WARNING: Unknown error: 431
问题是:我做错了什么来得到这个看起来很糟糕的信息?
后来:我需要在这个代码中添加什么来在中央和终端之间进行实际的数据传输。例如发送消息:“ Hello world!新年快乐。 ”
我将 Xcode 10.1 版与 Swift 4.2 一起使用。
下面是相关的源代码。
对于外围设备:
import UIKit
import CoreBluetooth
class ViewController: UIViewController,CBPeripheralManagerDelegate {
let service_UUID = CBUUID(string:"9869B57E-2005-492B-8ED9-B5716886975F"),
svcCharac_UUID = CBUUID(string:"BC7E600D-1AE4-4B09-BAC2-ECC0E85247E0")
var cbPerifMngr:CBPeripheralManager!, mutaSRVC:CBMutableService!,
svcCharac:CBMutableCharacteristic!
override func viewDidLoad() {
super.viewDidLoad()
cbPerifMngr = CBPeripheralManager(delegate: self, queue: nil)
}
// CBPeripheralManagerDelegate protocol implementation.
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheral.state == .poweredOn {
mutaSRVC = CBMutableService(type: service_UUID, primary: true)
svcCharac = CBMutableCharacteristic(type: svcCharac_UUID,
properties: [.read, .notify],
value: Data(base64Encoded: "Hello!"), permissions: .readable)
mutaSRVC.characteristics = [svcCharac]
cbPerifMngr?.add(mutaSRVC)
cbPerifMngr?.publishL2CAPChannel(withEncryption: false)
}
}
func peripheralManager(_ peripheral: CBPeripheralManager,
didPublishL2CAPChannel PSM: CBL2CAPPSM,
error: Error?) {
print(#function)
if error != nil {
print("Error in \(#function) :\n\(error!)")
return
}
print("PSM: \(PSM)")
}
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
print(#function)
if error != nil {
print("Error in \(#function) :\n\(error!)")
return
}
cbPerifMngr.startAdvertising([CBAdvertisementDataServiceUUIDsKey:[service.uuid]])
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
print(#function)
if error != nil {
print("Error in \(#function) :\n\(error!)")
return
}
}
}
对于中央:
import UIKit
import CoreBluetooth
class ViewController: UIViewController,CBCentralManagerDelegate,CBPeripheralDelegate {
let service_UUID = CBUUID(string:"9869B57E-2005-492B-8ED9-B5716886975F")
var cbCenterMngr:CBCentralManager!, cbPerifHandle:CBPeripheral!,
cbL2CAPChan:CBL2CAPChannel!
override func viewDidLoad() {
super.viewDidLoad()
cbCenterMngr = CBCentralManager(delegate: self, queue: nil)
}
// CBCentralManagerDelegate protocol implementation.
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print(#function)
if central.state == .poweredOn {
central.scanForPeripherals(withServices: [service_UUID],
options: nil)
}
}
func centralManager(_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String : Any],
rssi RSSI: NSNumber) {
print(#function)
peripheral.delegate = self
cbPerifHandle = peripheral
central.connect(peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print(#function)
peripheral.openL2CAPChannel(192)
}
func peripheral(_ peripheral: CBPeripheral,
didOpen channel: CBL2CAPChannel?,
error: Error?) {
print(#function)
if error != nil {
print("Error in \(#function) :\n\(error!)")
return
}
cbL2CAPChan = channel
}
}