如何获得可用的蓝牙设备列表?

我目前正在创build一个可以使用蓝牙设备的iPhone应用程序(Xcode 4.3.1,IOS 5)! 这个应用程序的主要目标是室内导航(build筑物内的GPS不是很准确)。

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

我试图使用CoreBluetooth框架,但我没有得到可用设备的列表! 也许我不正确地使用这些function

#import <UIKit/UIKit.h> #import <CoreBluetooth/CoreBluetooth.h> @interface AboutBhyperView : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate> { CBCentralManager *mgr; } @property (readwrite, nonatomic) CBCentralManager *mgr; @end - (void)viewDidLoad { [super viewDidLoad]; mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { NSLog([NSString stringWithFormat:@"%@",[advertisementData description]]); } -(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{ NSLog(@"This is it!"); } - (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."]; break; } case CBCentralManagerStatePoweredOn: { messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."]; [mgr scanForPeripheralsWithServices:nil options:nil]; //[mgr retrieveConnectedPeripherals]; //--- it works, I Do get in this area! break; } } NSLog(messtoshow); } 

我不确定这一行,如何传递正确的参数?

 [mgr scanForPeripheralsWithServices:nil options:nil]; 

我看了看苹果的参考..我仍然不明白什么是CBUUID? 每个设备都有一些蓝牙ID? 我在哪里可以find它?

 - (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options; 

参数serviceUUIDs – 应用程序感兴趣的CBUUID数组。options – 用于自定义扫描的字典,请参阅CBCentralManagerScanOptionAllowDuplicatesKey。

有没有其他的方式来使用IOS上的蓝牙? 我的意思是,旧的框架,不使用BLE 4.0!

任何意见,将不胜感激!

谢谢!

本指南对蓝牙3.0看起来很有希望。 请记住,CoreBluetooth框架仅适用于蓝牙低功耗(4.0)。 在bluetooth.org的开发页面中,您可以看到全球定义的服务的一些示例,正如pipe洋提到的,您可以看到心率服务是0x180D。 单位的UUID是由制造商定义的。

这里有一段代码可能会帮助你。

 // Initialize a private variable with the heart rate service UUID CBUUID *heartRate = [CBUUID UUIDWithString:@"180D"]; // Create a dictionary for passing down to the scan with service method NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // Tell the central manager (cm) to scan for the heart rate service [cm scanForPeripheralsWithServices:[NSArray arrayWithObject:heartRate] options:scanOptions] 

在蓝牙中,有“服务”。 一个设备发布1..N服务,每个服务有1..M特征。 每个服务都有一个唯一的标识符,称为UUID

例如,提供服务“心率”的心率蓝牙传感器,以UUID 0x180D发布服务。

(更多的服务在这里 )

因此,当您执行search时,您必须提供一个UUID作为标准,告诉要search哪个服务。

你应该看看其中的一个例子 。 这是相关的行:

 [manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:nil]; 

(我知道这是一个Mac OS X的例子,但iOS CoreBluetooth API非常相似。)

CBUUID标识您感兴趣的服务,而不是设备。 标准服务有一个16位UUID,在这种情况下,心率监视器为0x180d,设备信息为0x180a,专有服务则有128位UUID(16字节)。

大多数设备实现设备信息服务,所以如果您只是在寻找任何设备,您可以尝试[CBUUID UUIDWithString:@"180A"]

尝试在扫描设备时进行此操作

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil]; [self.centralManager scanForPeripheralsWithServices:nil options:options]; 

您将能够通过didDiscoverPeripheral委托方法获取设备列表

保持冷静,使用https://github.com/l0gg3r/LGBluetooth

所有你需要做的

 [[LGCentralManager sharedInstance] scanForPeripheralsByInterval:4 completion:^(NSArray *peripherals) { }];