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

51

我正在创建一个 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的旧框架!

任何建议都将不胜感激!

谢谢!


1
请注意,当您将 nil 传递给 scanForPeripheralsWithServices: 方法时,在后台运行时您将不会收到任何结果。 - WrightsCS
@mz87,你找到查找蓝牙设备的方法了吗?我也遇到了同样的问题。 - Gal
3
我认为苹果禁止这件事。我们只能获取具有特定CBUUID的设备列表。所以如果您想列出所有设备(与蓝牙设置本机相同),那么是不可能的。如果我说错了,请纠正我。 - Mrug
@Mrug 六年后...你错了吗?苹果是否阻止您在不指定UUID的情况下获取所有连接设备?我可以为此找到解决方案。 - Keselme
5个回答

23

这份指南 对Bluetooth 3.0看起来很有帮助。请记住,CoreBluetooth框架仅适用于低功耗蓝牙 (4.0)。在bluetooth.com的开发页面上,您可以看到一些全局定义服务的示例,正如Guan Yang所提到的,心率服务的UUID是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]

请注意,“低功耗蓝牙(Bluetooth low energy),之前被称为Wibree,是蓝牙v4.0的一个子集”http://en.wikipedia.org/wiki/Bluetooth#Bluetooth_v4.0。因此,如果设备支持BT 4.0,并不意味着它自动支持BT LE。 - m8labs
1
@MaratAl 我认为你的理解是相反的;如果设备支持LE,这并不意味着它也应该支持标准的BR/EDR。如果一个设备印有蓝牙标志,根据我的理解,它应该支持LE。将其品牌化为“蓝牙智能”可能只会使其兼容低能耗。 - chwi

15
在蓝牙中,存在“服务”(Services)。设备发布1..N个服务,每个服务都有1..M个特征(Characteristic)。每个服务都有一个唯一标识符,称为UUID。
例如,一个提供“心率”服务的心率蓝牙传感器发布带有UUID 0x180D的服务。
(更多服务在这里)
因此,在执行搜索时,必须提供UUID作为条件,告诉搜索哪个服务。

10

你应该查看其中一个示例。这是相关的代码行:

[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"]


1
扫描始终给我未知状态。 - Prince Kumar Sharma

3

当您正在扫描设备时,请尝试使用此功能

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

 [self.centralManager scanForPeripheralsWithServices:nil options:options];

您将能够在 didDiscoverPeripheral 委托方法中获取设备列表。


0

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

你需要做的只有

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


不要激动,结果是一样的。如果我进入设置/蓝牙,我可以找到一些设备,但是当我使用LGBluetooth时,我却看不到任何设备。 - iosMentalist
@Shady,如果你使用LGBluetooth连接一次设备(即配对),它将显示在“设置”屏幕中,与CoreBluetooth相同的结果。也就是说,LGBluetooth只是将CoreBluetooth封装成基于块的形式(它不添加/删除/更改BLE功能,只是封装)。 - l0gg3r
我不想配对或连接,我只想像“设置”显示的那样显示所有可用设备。我无法使用CoreBluetooth或LGBluetooth实现这一点。“centralManger:didDiscoverPeripheral:..”没有被调用! - iosMentalist
@Shady,那个功能应该支持外围硬件,CoreBluetooth和LGBluetooth无法帮助你解决这种问题。 - l0gg3r
2
保持冷静,链接已失效 :( - Paul Ruiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接