iOS CoreBluetooth:startAdvertising() 广告静态数据时出错

2024-03-20

我想宣传静态数据。我在 iOS 上使用 Swift 2.2.1 和 CoreBluetooth。我的应用程序构建蓝牙Services以及他们对应的特征,然后调用开始广告(),以及PeripheralManagerDidStartAdvertising90回调返回此错误:

peripheralManagerDidStartAdvertising encountered an error. // Mine
One or more parameters were invalid. // ...from Apple CoreBluetooth
nil  // ... return value from Apple CoreBluetooth callback

我对 Swift 和 iOS 开发还比较陌生,所以我猜我正在做一些愚蠢的事情,但到目前为止我还不知道是什么。

我将尝试为更有经验的人提炼一些东西。

- - - - - - pointutility.swift - - - - - - -
// This is a code excerpt and probably won't compile.

// UUID for the one peripheral service, declared outside the class:
var peripheralServiceUUID = CBUUID(string: "9BC1F0DC-F4CB-4159-BD38-7375CD0DD545")

// UUID for one characteristic of the service above, declared outside the class:
var nameCharacteristicUUID = CBUUID(string: "9BC1F0DC-F4CB-4159-BD38-7B74CD0CD546")

class PointUtility: NSObject, CBPeripheralManagerDelegate {

var peripheralManager:CBPeripheralManager?
var bluetoothServices:CBMutableService?
var nameCharacteristic:CBMutableCharacteristic?

override init() {
    super.init()
    peripheralManager = CBPeripheralManager(delegate:self, queue:nil)
    bluetoothServices = CBMutableService(type: peripheralServiceUUID, primary: true)
}

func configureUtilityForIdentity(identity:Point!) {
    var characteristicsArray:[CBCharacteristic] = []
    myIdentity = identity

      if (identity.name != nil) {
        nameCharacteristic =
            CBMutableCharacteristic(type: nameCharacteristicUUID,
                properties: (CBCharacteristicProperties.Read),
                value: myIdentity?.name?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
                permissions: CBAttributePermissions.Readable)

        characteristicsArray.append(nameCharacteristic!)
      }

    // more characteristics built here and added to the characteristicsArray[]...
    // ... then all are added to the CBMutableService at the bottom...
    bluetoothServices?.characteristics = characteristicsArray as [CBCharacteristic]
}


func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
    switch (peripheral.state) {
    case .PoweredOn:
        print("Current Bluetooth State:  PoweredOn")
        publishServices(bluetoothServices)
        break;
    case .PoweredOff:
        print("Current Bluetooth State:  PoweredOff")
        break;
    case .Resetting:
        print("Current Bluetooth State:  Resetting")
        break;
    case .Unauthorized:
        print("Current Bluetooth State:  Unauthorized")
    case .Unknown:
        print("Current Bluetooth State:  Unknown")
        break;
    case .Unsupported:
        /
        print("Current Bluetooth State:  Unsupported")
        break;
    }
}

func publishServices(newService:CBMutableService!) {
    peripheralManager?.addService(newService)
}

func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) {

    if (error != nil) {
        print("PerformerUtility.publishServices() returned error: \(error!.localizedDescription)")
        print("Providing the reason for failure: \(error!.localizedFailureReason)")
    }
    else {
        peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey : service.UUID])
    }
}

func peripheralManagerDidStartAdvertising(peripheral: CBPeripheralManager,
    error: NSError?) {
        if (error != nil) {
            print("peripheralManagerDidStartAdvertising encountered an error.")
            print(error!.localizedDescription)  // Error: One or more parameters were invalid
            print(error!.localizedFailureReason)  // Error: nil
        }
        print ("Debug: peripheralManagerDidStartAdvertising()")
}
}
- - - - - - pointutility.swift - - - - - - -

我真的很感谢所提供的任何帮助。

此致,

Michael


的值CBAdvertisementDataServiceUUIDsKey在字典中传递给startAdvertising是一个数组CBUUID对象,但你只传递一个CBUUID。一旦我将其更改为数组,您的代码就可以工作了。

func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) {

    if (error != nil) {
        print("PerformerUtility.publishServices() returned error: \(error!.localizedDescription)")
        print("Providing the reason for failure: \(error!.localizedFailureReason)")
    }
    else {
        peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey : [service.UUID]])
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS CoreBluetooth:startAdvertising() 广告静态数据时出错 的相关文章

随机推荐