在IOS中发现蓝牙设备

时间:2016-06-06 09:29:39

标签: ios objective-c core-bluetooth

对于使用蓝牙设备,我已从此网址https://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor下载了一个演示。

我正在尝试使用UUID @" 180A"来获取附近蓝牙设备的列表。但它没有发现任何设备。

如果有人实施此操作,请提供帮助。

//更改

添加以下请求后,访问其显示状态为GRANTED。但它没有要求他们的代表发现这些设备。

- (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];
 }

1 个答案:

答案 0 :(得分:1)

试试这个: 使用Core Bluetooth Framework

使用checkBluetoothAccessrequestBluetoothAccess方法

 - (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];

  }