我正在使用iOS 13和swift 5上使用Core Blue连接到外围设备。我在连接或发现服务和特征时没有问题。但是,我似乎无法保存上述服务和特性以供以后使用。
这是我的主要 CentralManager 类,它连接到外围设备并访问其服务:
class CentralManager : NSObject{
private var centralManager : CBCentralManager!
var peripheralZero : CBPeripheral!
override init(){
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func startScanning(){
if centralManager.state == .poweredOn{
centralManager.scanForPeripherals(withServices: nil, options: nil)
}
}
}
// MARK: - CBCentralManager Delegate Methods
extension CentralManager: CBCentralManagerDelegate{
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn{
print("central powered on")
}else{
print("Central does not support bluetooth")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
if(peripheral.identifier == ParticlePeripheral.DeviceUUID! as UUID){
centralManager.stopScan()
peripheralZero = peripheral //Here I save the found peripheral
peripheral.delegate = self
centralManager.connect(self.peripheralZero, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("connection successful")
peripheralZero.discoverServices(nil)
}
}
//MARK: - CBPeripheral Delegate Methods
extension CentralManager: CBPeripheralDelegate{
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let services = peripheralZero.services { // this executes to nil but if I instead use:
// if let services = peripheral.services
// it works
for service in services{
print(service)
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
}
来自Apple关于外围设备的服务属性的文档:
如果尚未调用 discoverServices(_:( 方法来发现外围设备的服务,或者这样做时出错,则此属性的值为 nil。
既然我已经调用了 discoverServices,我不应该使用我的外围零变量来检索它们吗?无论我尝试在代码中的何处访问它,该值均为 nil。我猜我保存外围设备的方式是导致问题的地方,但我真的不知道。我是 swift 的新手,所以任何帮助将不胜感激!
extension XXTrain: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
let cState = central.state
switch cState {
case .poweredOn:
print(">>central.state is .poweredOn")
centralManager.scanForPeripherals(withServices: services)
@unknown default: break
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
discoveredPeripherals[peripheral.identifier] = peripheral
myPeripheral = peripheral
centralManager.connect(peripheral)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print(" -- Connected: ((peripheral.state == .connected) ? "YES" : "NO"), (String(describing: peripheral.name)), UUID: (peripheral.identifier)")
peripheral.delegate = self
peripheral.discoverServices(services)
}
}
extension XXTrain: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else { return }
for service in services {
print(" >> Discover Services Found : (service.uuid) service")
peripheral.discoverCharacteristics(nil, for: service)
}
}
我发现的是订单/下达
peripheral.delegate = self
重要的是放在didConnect
而不是disDiscover
和
myPeripheral = peripheral
其中"持有强有力的参考"放在didDiscover