我正在创建一个 iPhone 应用程序(Xcode 4.3.1,IOS 5),可以使用蓝牙设备!该应用程序的主要目标是室内导航(建筑物内的 GPS 不太准确)。
我唯一看到的解决方案(保持我的应用在 AppStore 上)是尝试扫描可用的蓝牙设备!
我尝试使用 CoreBluetooth 框架,但我没有得到可用设备列表!也许我没有正确地使用这些函数。
#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吗?在哪里可以找到它?
- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;
参数 serviceUUIDs - 应用程序感兴趣的CBUUID数组。 options - 一个字典,用于自定义扫描,参见CBCentralManagerScanOptionAllowDuplicatesKey。
在IOS上有其他使用蓝牙的方法吗?我的意思是,不使用BLE 4.0的旧框架!
任何建议都将不胜感激!
谢谢!
nil
传递给scanForPeripheralsWithServices:
方法时,在后台运行时您将不会收到任何结果。 - WrightsCS