【发布时间】:2012-04-28 00:41:42
【问题描述】:
我目前正在创建一个可以使用蓝牙设备的 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:时,您在后台时不会收到任何结果。 -
@mz87 你找到找到BT设备的方法了吗?我也遇到了同样的问题。
-
我认为 Apple 禁止这件事。我们只能获取具有特定 CBUUID 的设备列表。因此,如果您想列出所有设备(与本机的蓝牙设置相同),那么这是不可能的。如果我错了,请纠正我。
-
@Mrug 6 年后......你错了吗? Apple 是否会阻止您在未指定 UUID 的情况下获取所有连接的设备?我可以找到解决方案。
标签: ios bluetooth core-bluetooth