iOS - CoreBluetooth didDiscoverPeripheral没有被调用 [英] iOS - CoreBluetooth didDiscoverPeripheral not being called
问题描述
我在Xcode 6中制作了一个非常简单的iOS应用程序来试用CoreBluetooth并与我的Polar H6心率监测器进行通信。由于某种原因,没有调用didDiscoverPeripheral方法。
I have made a very simple iOS app in Xcode 6 to try out CoreBluetooth and communicate to my Polar H6 heart rate monitor. For some reason the didDiscoverPeripheral method is not being called.
我在StackOverflow上发现了以下类似的问题,但你要么有点不同,要么我没有真正回答它:
I have found the following similar questions on StackOverflow but thy are either a bit different or do not really answer it for me:
- corebluetooth-diddiscoverperipheral-not-being-called-in-swift
- not-working-call-to-centralmanager-diddiscoverperipheral-advertisementdata
- diddiscoverperipheral-delegate-method-is-没有叫
- corebluetooth-diddiscoverperipheral-not-being-called-in-swift
- not-working-call-to-centralmanager-diddiscoverperipheral-advertisementdata
- diddiscoverperipheral-delegate-method-is-not-called
我在viewDidLoad中的代码:
My code in viewDidLoad:
NSArray *services = @[[CBUUID UUIDWithString:HRM_HEART_RATE_SERVICE_UUID]];
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[centralManager scanForPeripheralsWithServices:services options:nil];
self.centralManager = centralManager;
正在调用方法 centralManagerDidUpdateState
并告诉它我状态等于 CBCentralManagerStatePoweredOn
。
永远不会调用 didDiscoverPeripheral
方法,即使我将 nil
传递到 scanForPeripheralsWithServices
方法。
Method centralManagerDidUpdateState
is being called and it tells me state is equal to CBCentralManagerStatePoweredOn
.
The didDiscoverPeripheral
method is never called, not even when I pass nil
into the scanForPeripheralsWithServices
method.
在应用程序商店中,我下载了一个名为LightBlue的应用程序。它是一个非常简单的应用程序,可以测试使用蓝牙4.0 LowEnergy的任何设备。它扫描外围设备并显示有关设备及其服务的详细信息。
LightBlue应用确实看到我的HeartRate显示器,而我自己的小应用程序没有...
Within the app store I have downloaded d an app called LightBlue. Its a very simple app to test any device which uses Bluetooth 4.0 LowEnergy. It scans for peripherals and shows detailed info about the devices and its services. The LightBlue app does see my HeartRate monitor, while my own little app does not..
有没有人有任何提示或线索如何继续这个?
Does anybody have any tips or clues how to go forward with this?
(我使用Xcode 6和iPhone 6和iOS 8)
(I am using Xcode 6 and and iPhone 6 with iOS 8)
推荐答案
我正在使用Swift,看起来蓝牙队列需要使用主队列
I am using Swift, and it looks like the Bluetooth queue needs to be using the main queue
myCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())
这里是客观C
myCentralManager = [[CBCentralManager alloc]
initWithDelegate:self queue:dispatch_get_main_queue()];
这篇关于iOS - CoreBluetooth didDiscoverPeripheral没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!