首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何获取可用的蓝牙设备列表?

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

Stack Overflow用户
提问于 2012-04-17 00:50:53
回答 4查看 87.6K关注 0票数 51

我目前正在创建一个可以使用蓝牙设备的iPhone应用程序(Xcode4.3.1,iOS5)!这个应用程序的主要目标是室内导航(建筑物内的GPS并不是很准确)。

我在这里看到的唯一解决方案(使我的应用程序保持在AppStore上)是尝试扫描可用的蓝牙设备!

我试着使用CoreBluetooth框架,但是我没有得到可用的设备列表!也许我没有正确使用这些函数

代码语言:javascript
复制
#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); 
} 

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

代码语言:javascript
复制
[mgr scanForPeripheralsWithServices:nil options:nil];

我看了一下苹果的参考资料..我还是不明白CBUUID是什么?每个设备都有蓝牙标识吗?我在哪里可以找到它?

代码语言:javascript
复制
- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;

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

有没有其他方法可以在IOS上使用蓝牙?我的意思是,不使用BLE4.0的旧框架!

如有任何建议,我们将不胜感激!

谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-06-21 06:04:57

对于蓝牙3.0来说,This guide看起来很有前途。请记住,CoreBluetooth框架仅适用于蓝牙低能耗(4.0)。在bluetooth.com's dev-pages上,您可以看到一些全局定义的服务的示例,正如关阳提到的,您可以看到心率服务是0x180D。单位的UUID`s由厂家定义。

这里有一个代码片段,可能会对您有所帮助。

代码语言:javascript
复制
// 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]
票数 23
EN

Stack Overflow用户

发布于 2012-04-17 22:45:58

你应该看看one of the examples。下面是相关的代码行:

代码语言:javascript
复制
[manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:nil];

(我知道这是一个Mac示例,但是iOS CoreBluetooth应用编程接口非常相似。)

CBUUID标识您感兴趣的服务,而不是设备。标准服务具有16位UUID,在本例中,0x180d用于心率监视器,或者0x180a用于设备信息,而专有服务具有128位UUID (16字节)。

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

票数 10
EN

Stack Overflow用户

发布于 2013-06-28 04:29:40

在扫描设备时尝试使用此选项

代码语言:javascript
复制
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

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

您将能够通过didDiscoverPeripheral委托方法获取设备列表

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10178293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档