IOS蓝牙没有发现周边服务。 (有时!) [英] IOS Bluetooth not discovering peripheral services. (sometimes!)

查看:917
本文介绍了IOS蓝牙没有发现周边服务。 (有时!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我的应用程序会连接到外围设备(即didConnectPeripheral之称),但它不会发现可用的服务(didDiscoverServices不叫)。此外,当这种情况发生的外围设备(一个Adafruit的Bluefruit基础上,nRF8001)会说其没有连接!这只是发生约1/5的应用启动次数的。

低于code的块总是执行:

  FUNC centralManager(中央:CBCentralManager!didDiscoverPeripheral外设:CBPeripheral!advertisementData:[NSObject的:AnyObject]!RSSI:NSNumber的){
     //连接到外设如果一个UART
    如果(peripheral.name ==UART){
        currentPeripheral =外设
        currentPeripheral.delegate =自
        central.connectPeripheral(currentPeripheral,选项:无)
    }
}
 FUNC centralManager(中央:CBCentralManager!didConnectPeripheral外设:CBPeripheral){
    的println()
    的println(连接到:\\(peripheral.name))
    peripheral.discoverServices(无)
    connectionStatus = .Connected
    central.stopScan()
    的println(***停止扫描***)
}

任何想法,为什么didDiscoverServices是不是叫什么名字?

(我运行在一个iPod上的应用触摸第五代)


解决方案

按照<一个href=\"https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonCentralRoleTasks/PerformingCommonCentralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH3-SW1\"相对=nofollow> CoreBluetooth编程指南,你应该这样做:


  

您开始与周边的互动之前,您应该设置
  外围的委托,以确保它得到适当的
  回调,像这样的:


  peripheral.delegate =自我;

因此​​,你的didConnectPeripheral函数应该是这个样子:

  FUNC centralManager(中央:CBCentralManager!didConnectPeripheral外设:CBPeripheral){
        的NSLog(周边连接)
        peripheral.delegate =自
        peripheral.discoverServices(无)    }

Sometimes, my app will connect to the peripheral device (i.e. "didConnectPeripheral" is called) but it will not discover available services ("didDiscoverServices" is not called). Also when this happens the peripheral (an Adafruit Bluefruit based on the nRF8001) will say its not connected! This only happens about 1/5 of the times the app is launched.

The blocks of code below are always executed:

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***")
}

Any idea why "didDiscoverServices" is not called?

(I am running the app on an IPod touch 5th gen)

解决方案

According to the CoreBluetooth programming guide, you should do this:

Before you begin interacting with the peripheral, you should set the peripheral’s delegate to ensure that it receives the appropriate callbacks, like this:

peripheral.delegate = self;

Therefore, your didConnectPeripheral function should look something like this:

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

    }

这篇关于IOS蓝牙没有发现周边服务。 (有时!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆