I'm currently creating an iPhone app (Xcode 6.2, IOS 8.2) that could use Bluetooth devices! Main goal of this application is only search the available bluetooth devices and whenever you go to out of range from bluetooth devices one alert message pop up.and when ever you come in range automatically connected.
The only solution I see here (to keep my app on AppStore) is to try scan for available bluetooth devices!
I tried to use CoreBluetooth framework, but I don't get list of available devices! I wants to hit my head on my monitor i am very frustrated with this problem.
My code is here:
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CBCentralManagerDelegate,CBPeripheralDelegate,CBPeripheralManagerDelegate,UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate>
{
CBCentralManager *mgr;
CBPeripheralManager *manager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
manager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];
NSLog(@"%@",messtoshow);
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];
[mgr scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey :@YES}];
NSLog(@"%@",messtoshow);
break;
}
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"%@",[NSString stringWithFormat:@"%@",[advertisementData description]]);
NSLog(@"%@",[NSString stringWithFormat:@"Discover:%@,RSSI:%@\n",[advertisementData objectForKey:@"kCBAdvDataLocalName"],RSSI]);
NSLog(@"Discovered %@", peripheral.name);
[mgr connectPeripheral:peripheral options:nil];
}
didDiscoverPeripheral method successfully called But shows only one device,i show you Log:
2015-03-27 17:10:48.137 StanBluetooth[853:109794] Discover:(null),RSSI:-68
2015-03-27 17:10:48.138 StanBluetooth[853:109794] Discovered peripheral E876A182-9DC1-26B4-3C89-DD20F5EAE88B
2015-03-27 17:10:48.139 StanBluetooth[853:109794] Discovered Amit’s MacBook Air
2015-03-27 17:10:48.140 StanBluetooth[853:109794] {
kCBAdvDataIsConnectable = 1;
}
i am trying to find another device last 2 days.
any advice would be appreciated!
thanks!