如何获取可用蓝牙设备的列表? [英] How to get list of available Bluetooth devices?

查看:58
本文介绍了如何获取可用蓝牙设备的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个可以使用蓝牙设备的 iPhone 应用程序(Xcode 4.3.1,IOS 5)!此应用的主要目标是室内导航(建筑物内的 GPS 不是很准确).

I'm currently creating an iPhone app (Xcode 4.3.1, IOS 5) that could use Bluetooth devices! Main goal of this application is indoor navigation (GPS inside buildings isn't really accurate).

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

The only solution I see here (to keep my app on AppStore) is to try scan for available bluetooth devices!

我尝试使用 CoreBluetooth 框架,但我没有得到可用设备的列表!也许我没有正确使用这些功能

I tried to use CoreBluetooth framework, but I don't get list of available devices! Maybe I don't use these functions correctly

#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); 
} 

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

I'm not sure about this line, how to pass correct parameters?

[mgr scanForPeripheralsWithServices:nil options:nil];

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

I took a look into apple reference .. and I still don't understand what's that CBUUID ??? Every device has some bluetooth id? where can I find it?

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;

参数serviceUUIDs - 应用感兴趣的一组 CBUUID.options - 自定义扫描的字典,请参阅 CBCentralManagerScanOptionAllowDuplicatesKey.

Parameters serviceUUIDs - An array of CBUUIDs the app is interested in. options - A dictionary to customize the scan, see CBCentralManagerScanOptionAllowDuplicatesKey.

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

Is there any other way to use Bluetooth on IOS? I mean, older frameworks that don't use BLE 4.0!

任何建议将不胜感激!

谢谢!

推荐答案

本指南对于蓝牙 3.0 看起来很有希望.请记住,CoreBluetooth 框架仅适用于低功耗蓝牙 (4.0).在 bluetooth.com 的开发页面,您可以看到一些全局定义的服务示例,正如关阳提到的,你可以看到心率服务是0x180D.单位的 UUID 由制造商定义.

This guide looks promising for Bluetooth 3.0. Remember that the CoreBluetooth framework is ONLY for Bluetooth Low Energy (4.0). At bluetooth.com's dev-pages you can see some examples of globally defined services, and as Guan Yang mentionen, you can see that the heart rate service is 0x180D. UUID`s of the unit is defined by the manufacturer.

这是一个代码片段,也许可以帮助您一路走来.

Here's a code snippet to maybe help you along the way.

// 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]

这篇关于如何获取可用蓝牙设备的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆