我目前正在创建一个可以使用蓝牙设备的 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 的旧框架!
任何意见,将不胜感激!
谢谢!