如何使用核心蓝牙sdk扫描蓝牙设备?

我目前正在创建一个可以使用蓝牙设备的iPhone应用程序(Xcode 6.2,IOS 8.2)! 此应用程序的主要目标是仅搜索可用的蓝牙设备,并且当您从蓝牙设备超出范围时,会弹出一条警报消息。当您进入范围时自动连接。

我在这里看到的唯一解决方案(将我的应用程序保存在AppStore上)是尝试扫描可用的蓝牙设备!

我尝试使用CoreBluetooth框架,但我没有获得可用设备的列表! 我想在我的显示器上碰到头,我对这个问题非常沮丧。

我的代码在这里:

#import "ViewController.h" #import  #import  @interface ViewController ()  { 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方法成功调用但是只显示一个设备,我给你看日志:

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

我想在最近2天找到另一台设备。

任何意见,将不胜感激!

谢谢!

您的代码看起来正确。

有关第二个设备可能不显示原因的一些提示:

  • 如果您发现第二台设备使用另一个第三方iOS蓝牙扫描应用程序,如LightBlue ,它可能已连接到该设备(如果您点击它以获取更多详细信息)。 当外围设备连接时,通常会停止广告(连接建立通常是广告的唯一原因)。 如果设备不再广告,则无法通过扫描找到它。 如果是这种情况并且设备已连接到iOS设备,则可以使用CBCentralManager的方法-retrieveConnectedPeripheralsWithServices:-retrieveConnectedPeripherals (自iOS 7起不推荐使用)来获取已由另一个应用程序连接到iOS的外围设备。

  • 外围设备可能会以如此缓慢的速度进行广告宣传,iOS设备只需要很长时间(而且有点运气)。 如果是这种情况,请尝试在外围设备上选择较短的广告时间间隔(假设您控制外围设备的固件)。

请注意,如果您只需要扫描其他外围设备,则不需要CBPeripheralManager