首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS核心蓝牙CBCentralManager scanForPeripheralsWithServices无结果

iOS核心蓝牙CBCentralManager scanForPeripheralsWithServices无结果
EN

Stack Overflow用户
提问于 2014-01-28 06:24:54
回答 1查看 3.6K关注 0票数 1

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

在ViewController.h中

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController : UIViewController <CBCentralManagerDelegate>

@property (strong, nonatomic) CBCentralManager *centralManager;

@end

在ViewController.m中

代码语言:javascript
运行
复制
#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);

}

我有日志信息"is on“,但我没有结果,当我周围有几个蓝牙设备时,我发现没有蓝牙设备…为什么?

EN

回答 1

Stack Overflow用户

发布于 2014-01-28 13:48:23

首先,你必须确认你周围的所有蓝牙设备是否都是蓝牙低能耗设备。事实上,并非所有的蓝牙4.0设备都是蓝牙低能耗设备。据我所知,当我扫描外围设备时,我的mac还没有被检测到。其次,除非外围设备至少配对一次,否则无法从peripheral.name获取外围设备的名称。您可以尝试此方法来获取名称(如果有)但不进行配对,在didDiscoverPeripheral方法下调用此方法

advertisementData的值:@“kCBAdvDataLocalName”

如果存在外设的本地名称,则现在将显示该名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21392987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档