【问题标题】:Detecting Bluetooth Enabled iPhone devices in IOS7 in IOS在IOS中检测IOS7中启用蓝牙的iPhone设备
【发布时间】:2014-03-08 20:48:02
【问题描述】:

我在我的应用程序中使用核心蓝牙框架。

我知道如何扫描外围设备并从中获取值。(如心率监视器)

但我想要的是检索周围支持 BLE 4.0 和蓝牙的 iPhone 设备列表。

我参考了下面的链接..

Uses IOBluetooth Framework

Uses CoreBluetooth For Getting Peripherals not the Devices List

 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    // I'm not sure how to make this work

    NSLog (@"Discovered peripheral: %@", [peripheral name]);
    [self.list addObject:peripheral.name]; // add peripheral name to foundarray
    NSLog (@"UUID peripheral: %@", [peripheral UUID]);

    NSLog (@"peripheral services before connected: %@", [peripheral services]);

    NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]);

    NSLog(@"foundArray is %@", self.list);
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    NSLog(@"Central manager's state is updated to: %@", central);
    if(central.state == CBCentralManagerStatePoweredOn)
    {
        //okay your good to go and can now scan
    }
    else
    {
        //Unable to use CentralManager methods so print out the central.state and find out why
    }
}

我不知道苹果是否提供这个..

任何建议或想法将不胜感激..

提前致谢..

【问题讨论】:

  • BLE 从 iPhone 4S 及更高版本、iPad 3 及更高版本开始支持。您不会通过代码获得列表。无论如何,看到 CBCentralManager 状态应该会告诉你设备是否支持 BLE (CBCentralManagerStateUnsupported)。

标签: ios objective-c core-bluetooth


【解决方案1】:

您只能检索周围支持蓝牙 4.0 的 iOS 设备,前提是它们还宣传特定服务以识别它们。您不能只看到所有已开机和附近的 iOS 设备。如果他们在做广告,你可以只扫描 nil ,这将返回被看到的广告包。

注意:如果您关心从其他应用中检索当前连接的 BLE 设备,您可以使用retrieveConnectedPeripheralsWithServices: 方法。

【讨论】:

  • 如果我去设置/蓝牙我可以找到设备,但是当我使用扫描时:scanForPeripharlsWithServices:nil 我没有找到任何设备。为什么?
【解决方案2】:

试试下面的代码checkBluetoothAccessrequestBluetoothAccess方法

 - (void)checkBluetoothAccess {

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

       /*
            We can ask the bluetooth manager ahead of time what the authorization status is for our bundle and take the appropriate 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];
        }

    /*
            When the application requests to start scanning for bluetooth devices that is when the user is presented with a consent dialog.
    */

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

  }

【讨论】:

  • @bhavya..Getting error at this line.. [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"GRANTED", @"")]; } 这里的蓝牙是什么..?
  • @Vidhyanand - 只需删除该类型的行 - 您可以放置​​一个简单的 UIAlertView 或 NSLog
猜你喜欢
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多