【问题标题】:iOS core bluetooth CBCentralManager scanForPeripheralsWithServices no resultsiOS 核心蓝牙 CBCentralManager scanForPeripheralsWithServices 无结果
【发布时间】:2014-01-27 22:24:54
【问题描述】:

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

在 ViewController.h 中

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

@interface ViewController : UIViewController <CBCentralManagerDelegate>

@property (strong, nonatomic) CBCentralManager *centralManager;

@end

在 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);

}

我有日志消息“开启”但我没有结果,当我周围有几个时我发现没有蓝牙设备......为什么?

【问题讨论】:

  • 它们是低功耗蓝牙吗?如果没有,您将不得不使用 ExternalAccessory.framework,因为 CoreBluetooth 仅适用于低功耗蓝牙。
  • 是的,我认为.. 例如,我的 MacBook 是蓝牙 4.0

标签: ios bluetooth core-bluetooth


【解决方案1】:

首先,您必须确认您周围的所有蓝牙设备是否确实是蓝牙低功耗设备,所有蓝牙4.0设备都不是蓝牙低功耗设备。据我所知,当我扫描外围设备时,还没有检测到我的 mac。其次,除非并且直到外围设备至少配对一次,否则您不会从 peripheral.name 获得外围设备的名称。如果有不配对的可以试试这个方法取名字,在didDiscoverPeripheral方法下调用

[advertisementData valueForKey:@"kCBAdvDataLocalName"]

如果有外设的本地名称,它将立即显示。

【讨论】:

  • 谢谢,我认为蓝牙 4.0 设备耗能低……否则,我如何扫描所有蓝牙 4.0 设备?我查看了 ExternalAccessory 框架,但它仅适用于 Apple 设备,不是吗?但我也想扫描其他设备。我该怎么做?
猜你喜欢
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
相关资源
最近更新 更多