2

I am trying to scan all the available peripherals using CoreBluetooth framework.

@implementation ViewController



@synthesize myCentralManager;

-(id)init{

    self=[super init];
    if(self){
        myCentralManager=[[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
        [myCentralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    myCentralManager=[[CBCentralManager alloc]initWithDelegate:self queue:nil];

    //[self scanForPeripherals];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (NSString*)UUIDString:(CFUUIDRef)uuid {
    CFStringRef string = CFUUIDCreateString(NULL, uuid);
    return (__bridge_transfer NSString*)string;
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    //NSLog(@"DidUpdateState");

    if(central.state==CBCentralManagerStateUnknown)
        NSLog(@"Unknown State");
    else
    if(central.state==CBCentralManagerStateUnsupported)
        NSLog(@"State Unsupported");
    else
    if (central.state == CBCentralManagerStatePoweredOn) {
        [self scanForPeripherals];
    } else
            NSLog(@"Turn on Bluetooth");

}

- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI {
    NSLog(@"Received peripheral : \n%@", peripheral);
    NSLog(@"Adv data : %@", advertisementData);

    [peripheral setDelegate:self];
    [central connectPeripheral:peripheral options:nil];
    [peripheral readRSSI];
}



- (int)scanForPeripherals {
    NSLog(@"Scanning");

    if(self.myCentralManager.state!=CBCentralManagerStatePoweredOn)
        NSLog(@"Turn on Bluetooth");
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
                             nil];
    NSLog(@"Scanning");
    [myCentralManager scanForPeripheralsWithServices:nil options:options];
    return 0;
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"didConnectPeripheral");
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"didDisconnectPeripheral");
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    NSLog(@"failed to connect");
}

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
    NSLog(@"didReadRSSI");
}

@end

Even though I checked my system supports Bluetooth 4.0 LMP 0x6, my code always returns State Unsupported. Any help would be highly appreciated! Thanks.

user2979190
  • 239
  • 2
  • 13
  • Please consider pasting your code instead of a code screen shot as the font in your screenshot is small and have code you help the people to answer by testing your code and modification of it – dvhh Sep 01 '15 at 02:47
  • @dvhh Done! Any lead! – user2979190 Sep 01 '15 at 02:56
  • 1
    Are you running on the simulator or on a real device? The simulator cannot access Bluetooth hardware. You must test on an iPhone 4S or later (or BLE capable iPad or iPod Touch) – Paulw11 Sep 01 '15 at 03:22
  • @Paulw11 Yes I was trying to run it on the emulator not on a real device (may be thats the reason). Is it anyway possible to run bluetooth based app on emulator? – user2979190 Sep 01 '15 at 03:57
  • No, you need to use a real device since iOS 7 – Paulw11 Sep 01 '15 at 03:58

0 Answers0