【问题标题】:IOS Bluetooth not discovering peripheral services. (sometimes!)IOS 蓝牙未发现外围服务。 (有时!)
【发布时间】:2015-03-14 13:04:50
【问题描述】:

有时,我的应用会连接到外围设备(即调用“didConnectPeripheral”),但它不会发现可用服务(未调用“didDiscoverServices”)。此外,当这种情况发生时,外围设备(基于 nRF8001 的 Adafruit Bluefruit)会说它未连接!这只发生在应用启动的大约 1/5 次。

下面的代码块总是被执行:

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
     //Connect to the peripheral if its a UART
    if(peripheral.name == "UART") {
        currentPeripheral = peripheral
        currentPeripheral.delegate = self
        central.connectPeripheral(currentPeripheral, options: nil) 
    }
}


 func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
    println()
    println("Connected to: \(peripheral.name)")
    peripheral.discoverServices(nil)
    connectionStatus = .Connected
    central.stopScan()
    println("***Stopped scanning***")
}

知道为什么不调用“didDiscoverServices”吗?

(我在第 5 代 iPod touch 上运行该应用程序)

【问题讨论】:

  • 你保留peripheral的引用吗?
  • 如果您使用currentPeripheral.discoverServices(nil) 而不是peripheral.discoverServices(nil)
  • 是的,一旦发现外围设备,我就会保留对它的引用。 (我编辑了我的帖子)
  • 我做了currentPeripheral.discoverServices(nil) 结果是一样的。
  • 您是否以某种方式解决了这个问题?我目前面临类似的问题。 iOS 表示它已连接,但无法发现服务,而外围设备声称未收到连接尝试。在我们的应用程序和使用 nRF Connect 中进行了测试。两者都表现出相同的行为。

标签: ios swift bluetooth


【解决方案1】:

根据CoreBluetooth programming guide,你应该这样做:

在开始与外围设备交互之前,您应该设置 外围设备的委托,以确保它收到适当的 回调,像这样:

peripheral.delegate = self;

因此,您的 didConnectPeripheral 函数应如下所示:

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
        NSLog("peripheral connected")
        peripheral.delegate = self
        peripheral.discoverServices(nil)

    }

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 2014-08-27
    • 1970-01-01
    • 2018-03-04
    • 2017-03-17
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多