gpt4 book ai didi

iphone - 为什么我的特性没有出现在 BLE 广告 Swift 中

转载 作者:行者123 更新时间:2023-11-30 13:51:00 25 4
gpt4 key购买 nike

我想宣传 iPhone 作为外围设备的一些特征,但是尽管该服务似乎包含使用 BLE 扫描仪查看时不会显示的特征,但该服务工作正常并显示 localNameKey .

谢谢

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBPeripheralManagerDelegate{

var peripheralManager: CBPeripheralManager!
let myCustomServiceUUID: CBUUID = CBUUID(string: "B5893BC9-63AB-42A5-BB33-EEAE686BED1D")
let myCustomCharacteristic: CBUUID = CBUUID(string: "9BA41369-C5B7-456B-B4E3-BB0A8DFF3A95")
let myCustomCharacteristic2: CBUUID = CBUUID(string: "9BA41369-C5B7-456B-B4E3-BB0A8DFF3A85")
var myService: CBMutableService!
var myCharacteristics: CBMutableCharacteristic!
var myCharacteristics2: CBMutableCharacteristic!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
myService = CBMutableService(type: myCustomServiceUUID, primary: true)

myCharacteristics = CBMutableCharacteristic(type: myCustomCharacteristic, properties: CBCharacteristicProperties.Broadcast, value: nil, permissions: CBAttributePermissions.Readable)
myCharacteristics2 = CBMutableCharacteristic(type: myCustomCharacteristic2, properties: CBCharacteristicProperties.Broadcast, value: nil, permissions: CBAttributePermissions.Readable)

myService.characteristics = [myCharacteristics, myCharacteristics2]
peripheralManager.addService(myService)



}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
if peripheral.state == CBPeripheralManagerState.PoweredOn {
let dataTobeAdvetised :[String: AnyObject!] = [CBAdvertisementDataServiceUUIDsKey: [myService.UUID], CBAdvertisementDataLocalNameKey: "MY Device"]
self.peripheralManager.startAdvertising(dataTobeAdvetised)
print(myService)
print("It should be working!........")


} else if peripheral.state == CBPeripheralManagerState.PoweredOff {
self.peripheralManager.stopAdvertising()
}
}



}

最佳答案

您无权使用 CBCharacteristic.broadcast 属性值。

来自documentation -

  • CBCharacteristicPropertyBroadcast

The characteristic’s value can be broadcast using a characteristic configuration descriptor.

This property is not allowed for local characteristics published via the addService: method of the CBPeripheralManager class. This means that you cannot use this property when you initialize a new CBMutableCharacteristic object via the initWithType:properties:value:permissions: method of the CBMutableCharacteristic class.

您应该根据您的要求使用 CBCharacteristicPropertyReadCBCharacteristicPropertyWriteWithoutResponseCBCharacteristicPropertyWrite 的某种组合。

关于iphone - 为什么我的特性没有出现在 BLE 广告 Swift 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34265588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com