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

mz8*_*z87 49 bluetooth ios core-bluetooth

我目前正在创建一个可以使用蓝牙设备的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); 
} 
Run Code Online (Sandbox Code Playgroud)

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

[mgr scanForPeripheralsWithServices:nil options:nil];
Run Code Online (Sandbox Code Playgroud)

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

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;
Run Code Online (Sandbox Code Playgroud)

参数serviceUUIDs - 应用程序感兴趣的CBUUID数组.选项 - 用于自定义扫描的字典,请参阅CBCentralManagerScanOptionAllowDuplicatesKey.

在IOS上还有其他方法可以使用蓝牙吗?我的意思是,旧的框架不使用BLE 4.0!

任何意见,将不胜感激!

谢谢!

chw*_*hwi 23

本指南看起来很有希望用于蓝牙3.0.请记住,CoreBluetooth框架仅适用于低功耗蓝牙(4.0).在bluetooth.org的开发页面中,您可以看到全局定义服务的一些示例,正如关阳所提到的,您可以看到心率服务是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]
Run Code Online (Sandbox Code Playgroud)


小智 15

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

例如,提供服务"心率"的心率蓝牙传感器发布具有UUID 0x180D的服务.

(这里有更多服务)

因此,当您执行搜索时,必须提供UUID作为条件,告知要搜索的服务.


Gua*_*ang 10

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

[manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

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