gpt4 book ai didi

ios - 没有在 Swift 中调用 CoreBluetooth didDiscoverPeripheral

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:39 25 4
gpt4 key购买 nike

我 99% 确定我按照说明正确设置了 CoreBluetooth。无论我做什么,当我在我的 iPad mini 上运行这个应用程序时,蓝牙都会显示它已打开。它说它正在扫描设备,但绝对找不到任何设备。如果我转到设备上的蓝牙菜单,我会看到其他设备被发现。我初始化 CBCentralManager。我设置了 centralManagerDidUpdateState。当确定蓝牙已准备就绪时,它会调用 centralManager.scanForPeripheralsWithServices。所有这一切都在正确发生。但是我的委托(delegate)函数 centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) 从来没有被调用过。我的代码非常简单。也许我遗漏了一些东西,但我能够确认我的 Macbook 是一个 BTLE 设备,我的 ipad mini 也是一个 BTLE 设备。这是我的代码。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

var centralManager:CBCentralManager!
var blueToothReady = false

override func viewDidLoad() {
super.viewDidLoad()
startUpCentralManager()
}

func startUpCentralManager() {
println("Initializing central manager")
centralManager = CBCentralManager(delegate: self, queue: nil)
}

func discoverDevices() {
println("discovering devices")
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println("Discovered \(peripheral.name)")
}

func centralManagerDidUpdateState(central: CBCentralManager!) {
println("checking state")
switch (central.state) {
case .PoweredOff:
println("CoreBluetooth BLE hardware is powered off")

case .PoweredOn:
println("CoreBluetooth BLE hardware is powered on and ready")
blueToothReady = true;

case .Resetting:
println("CoreBluetooth BLE hardware is resetting")

case .Unauthorized:
println("CoreBluetooth BLE state is unauthorized")

case .Unknown:
println("CoreBluetooth BLE state is unknown");

case .Unsupported:
println("CoreBluetooth BLE hardware is unsupported on this platform");

}
if blueToothReady {
discoverDevices()
}
}

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

}

最佳答案

我不得不为我的 macbook 做广告。一旦我使用 https://github.com/mttrb/BeaconOSX 做到了这一点它完全按照我写的那样工作。

关于ios - 没有在 Swift 中调用 CoreBluetooth didDiscoverPeripheral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25039103/

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