iOS核心蓝牙CBCentralManager scanForPeripheralsWithServices没有结果 [英] iOS core bluetooth CBCentralManager scanForPeripheralsWithServices no results

查看:328
本文介绍了iOS核心蓝牙CBCentralManager scanForPeripheralsWithServices没有结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS 7应用程序的ViewController中扫描所有蓝牙设备,如下所示:

I want to scan all the bluetooth devices in a ViewController, on a iOS 7 application, like this :

在ViewController.h

In ViewController.h

#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController : UIViewController <CBCentralManagerDelegate>

@property (strong, nonatomic) CBCentralManager *centralManager;

@end

在ViewController.m

In ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - CBCentralManager Delegate

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (self.centralManager.state == CBCentralManagerStatePoweredOn)
    {
        NSLog(@"is on");
        [self.centralManager scanForPeripheralsWithServices:nil options:nil];

    }
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{   
    NSLog(@"Discovered %@", peripheral.name);

}

我有一条日志消息"on on",但我没有任何结果,当我周围有几个蓝牙设备时,我找不到蓝牙设备...为什么?

I have the log message "is on" but i have no results, I find no bluetooth device when there are several around me... why ?

推荐答案

首先,您必须确认您周围的所有蓝牙设备是否确实是蓝牙低能耗设备,所有蓝牙4.0设备都不都是蓝牙低能耗设备.设备.据我所知,当我扫描外围设备时,尚未检测到我的mac.其次,除非外围设备至少配对了一次,否则您不会从外周设备名获得外围设备的名称.您可以尝试使用此方法获取名称(如果有的话)而不配对,请在didDiscoverPeripheral方法下调用此名称

First of all, you will have to confirm if all the bluetooth devices around you are bluetooth low energy devices indeed, all bluetooth 4.0 devices aren't bluetooth low energy devices. And as far as I have seen my mac is not yet detected when I scan for peripherals. Secondly, you won't get the name of a peripheral device from peripheral.name unless and until the peripheral device has been paired atleast once. You may try this method to get the name if there is any without pairing, call this under didDiscoverPeripheral method

[advertisementData valueForKey:@"kCBAdvDataLocalName"]

[advertisementData valueForKey:@"kCBAdvDataLocalName"]

如果外围设备有本地名称,它将立即显示.

If there is a local name for the peripheral, it will get displayed now.

这篇关于iOS核心蓝牙CBCentralManager scanForPeripheralsWithServices没有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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