xcode iOS - CoreBluetooth didDiscoverPeripheral 未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26452691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - CoreBluetooth didDiscoverPeripheral not being called
提问by Brabbeldas
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.
我在 Xcode 6 中制作了一个非常简单的 iOS 应用程序来试用 CoreBluetooth 并与我的 Polar H6 心率监测器进行通信。出于某种原因,没有调用 didDiscoverPeripheral 方法。
I have found the following similar questions on StackOverflow but thy are either a bit different or do not really answer it for me:
我在 StackOverflow 上发现了以下类似的问题,但你的问题要么有点不同,要么没有真正为我回答:
- corebluetooth-diddiscoverperipheral-not-being-called-in-swift
- not-working-call-to-centralmanager-diddiscoverperipheral-advertisementdata
- diddiscoverperipheral-delegate-method-is-not-called
- corebluetooth-diddiscoverperipheral-not-being-call-in-swift
- 不工作呼叫中央经理确实发现外设广告数据
- diddiscoverperipheral-delegate-method-is-not-调用
My code in viewDidLoad:
我在 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;
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.
centralManagerDidUpdateState
正在调用方法,它告诉我 state 等于CBCentralManagerStatePoweredOn
。该didDiscoverPeripheral
方法从未被调用,即使在我传入nil
该scanForPeripheralsWithServices
方法时也不被调用。
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..
在应用程序商店中,我下载了一个名为 LightBlue 的应用程序。它是一个非常简单的应用程序,可以测试任何使用蓝牙 4.0 LowEnergy 的设备。它扫描外围设备并显示有关设备及其服务的详细信息。LightBlue 应用程序确实可以看到我的心率监视器,而我自己的小应用程序却看不到……
Does anybody have any tips or clues how to go forward with this?
有没有人有任何提示或线索如何继续这个?
(I am using Xcode 6 and and iPhone 6 with iOS 8)
(我在 iOS 8 上使用 Xcode 6 和 iPhone 6)
回答by Maria
I am using Swift, and it looks like the Bluetooth queue needs to be using the main queue
我正在使用 Swift,看起来蓝牙队列需要使用主队列
myCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())
Here it is in objective C
这是在目标 C
myCentralManager = [[CBCentralManager alloc]
initWithDelegate:self queue:dispatch_get_main_queue()];