我想在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);
}
我有日志信息"is on“,但我没有结果,当我周围有几个蓝牙设备时,我发现没有蓝牙设备…为什么?
发布于 2014-01-28 13:48:23
首先,你必须确认你周围的所有蓝牙设备是否都是蓝牙低能耗设备。事实上,并非所有的蓝牙4.0设备都是蓝牙低能耗设备。据我所知,当我扫描外围设备时,我的mac还没有被检测到。其次,除非外围设备至少配对一次,否则无法从peripheral.name获取外围设备的名称。您可以尝试此方法来获取名称(如果有)但不进行配对,在didDiscoverPeripheral方法下调用此方法
advertisementData的值:@“kCBAdvDataLocalName”
如果存在外设的本地名称,则现在将显示该名称。
https://stackoverflow.com/questions/21392987
复制相似问题