我最近一直在尝试使用
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
问题是:
我有什么不好的地方去得到这个难看的信息?
稍后:为了在中央和终端之间进行实际的数据传输,我需要在这个代码中添加什么。例如,发送消息:
你好,世界!新年快乐。
"
我使用的是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
}
}