I am trying out Core Bluetooth framework introduced in iOS 5.0. According to many threads (one of many) on StackOverflow itself:
- Core Bluetooth framework can be used to communicate with ANY
hardware, which has Bluetooth Low Energy (4.0) hardware support.
- We can forget about Made For iPhone/iPod (MFI) program, if you are
using Core Bluetooth technology.
I have an iPhone 5, iPhone 4S, Google Android Nexus 7 with me, and I am sure at least first 2 has hardware support for BLE.
My Question is
Well, I tried below given code on my iPhone 4S/iPhone 5, but it failed to scan and find the iPhone5/iPhone 4S sitting near by. I can confirm, both devices had bluetooth turned ON. The delegate method didDiscoverPeripheral
never getting called.
What might be the reasons? Am I missing something?
This is my code (stripped down to a small test project).
ViewController.h
@interface ViewController:UIViewController<CBCentralManagerDelegate, CBPeripheralDelegate{
}
@property (strong, nonatomic) CBCentralManager *mCentralManager;
@end
ViewController.m
@implementation ViewController
@synthesize mCentralManager;
- (void)viewDidLoad{
[super viewDidLoad];
mCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
[self scanForPeripherals];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Received periferal :%@",peripheral);
}
- (int) scanForPeripherals {
if (self.mCentralManager.state != CBCentralManagerStatePoweredOn)
{
NSLog(@"self.mCentralManagerState : %d",self.mCentralManager.state);
return -1;
}
//Getting here alright.. bluetooth is powered on.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
//Documentation says passind nil as device UUID, scans and finds every peripherals
[self.mCentralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
@end
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…