微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我正在尝试使用CoreBluetooth模块,但是我的部分代码无法运行

如何解决我正在尝试使用CoreBluetooth模块,但是我的部分代码无法运行

我正在尝试使用CoreBluetooth模块将Mac用作蓝牙外围设备。我看过文档及其各种示例,但是我的代码仍然无法正常工作。我将打印语句放在整个语句中,由于某种原因,输出显示的唯一打印语句是“ 1”和“ 1.5”。如果有帮助,它位于Mac OS Catalina 10.15.6上运行的XCode 11.6游乐场中。

import AppKit
import PlaygroundSupport
import CoreBluetooth

print("1")
let myCBUUID = CBUUID(string:"2FC62EDD-EFED-457A-A88E-6E9BC1B8D7AF")
let properties: CBCharacteristicProperties = [.notify,.read,.write]
let permissions: CBAttributePermissions = [.readable,.writeable]
let characteristic = CBMutableCharacteristic(type:myCBUUID,properties: properties,value:nil,permissions: permissions)
let myCBService = CBMutableService(type:myCBUUID,primary: true)
myCBService.characteristics = [characteristic]
print("1.5")
class Peripheral: CBPeripheralManager,CBPeripheralManagerDelegate
{
    var peripheralManager : CBPeripheralManager!
    convenience init(delegate: CBPeripheralManagerDelegate?,queue: dispatchQueue?){
        print("1.75")
        self.init()
        peripheralManager = CBPeripheralManager(delegate: self,queue: nil,options: [CBPeripheralManagerOptionShowPowerAlertKey: true])
        peripheralManager.add(myCBService)
    }
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
    {
        print("2")
        print("state: \(peripheral.state)")
    }
    func Advertise(){
        print("hello its me")
        peripheralManager.startAdvertising([CBAdvertisementDataLocalNameKey : "My Peripheral",CBAdvertisementDataLocalNameKey : myCBUUID])
    }
    func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager,error: NSError?)
    {
        if let error = error
        {
            print("Failed due to error: \(error)")
            return
        }
        print("Success")
    }
}

let myPeripheral = Peripheral.init()
myPeripheral.Advertise()

(这是我第一次发帖,如果我搞砸了,抱歉,如果我遗漏了一些东西,可以随时在评论中提出建议)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。