Chandan Kumar Jha
Chandan Kumar Jha

Reputation: 325

Discover Bluetooth Devices in IOS

For working with bluetooth devices I have downloaded one demo from this url https://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor.

I am trying to get the list of nearby bluetooth devices using UUID @"180A" but it is not discovering any devices.

Please help if anyone have implemented this.

// Changes

After adding the below request access its show the status is GRANTED. But its not calling their delegates for discover the devices.

- (void)requestBluetoothAccess {

if(!self.mgr) {
    self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
 requests to start scanning for bluetooth devices.
 */
CBUUID *heartRate = [CBUUID UUIDWithString:@"180A"];

// Create a dictionary for passing down to the scan with service method
NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[self.mgr scanForPeripheralsWithServices:[NSArray arrayWithObject:heartRate] options:scanOptions];
 }

Upvotes: 1

Views: 2335

Answers (1)

TheHungryCub
TheHungryCub

Reputation: 1970

Try this: Use Core Bluetooth Framework

using checkBluetoothAccess and requestBluetoothAccess method

 - (void)checkBluetoothAccess {

      if(!self.cbManager) {
           self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
      }

   /*
     Ask to bluetooth manager ahead of time what the authorization status, for our bundle and take the action.
  */

     CBCentralManagerState state = [self.cbManager state];

    if(state == CBCentralManagerStateUnknown) {
          [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"UNKNOWN", @"")];
    }
   else if(state == CBCentralManagerStateUnauthorized) {
          [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"DENIED", @"")];
    }
else {
          [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"GRANTED", @"")];
  }
}

.

- (void)requestBluetoothAccess {

  if(!self.cbManager) {
          self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    }

/*
  requests to start scanning for bluetooth devices.
*/

[self.cbManager scanForPeripheralsWithServices:nil options:nil];

  }

Upvotes: 1

Related Questions