问题
I want to scan all the bluetooth devices in a ViewController, on a iOS 7 application, like this :
In ViewController.h
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : UIViewController <CBCentralManagerDelegate>
@property (strong, nonatomic) CBCentralManager *centralManager;
@end
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);
}
I have the log message "is on" but i have no results, I find no bluetooth device when there are several around me... why ?
回答1:
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"]
If there is a local name for the peripheral, it will get displayed now.
来源:https://stackoverflow.com/questions/21392987/ios-core-bluetooth-cbcentralmanager-scanforperipheralswithservices-no-results