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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10178293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:53:44  来源:igfitidea点击:

How to get list of available Bluetooth devices?

iosbluetoothcore-bluetooth

提问by mz87

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).

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

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

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

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

我尝试使用 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); 
} 

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

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

[mgr scanForPeripheralsWithServices:nil options:nil];

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?

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

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

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

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

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

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

any advice would be appreciated!

任何意见,将不胜感激!

thanks!

谢谢!

采纳答案by chwi

This guidelooks promising for Bluetooth 3.0. Remember that the CoreBluetooth framework is ONLY for Bluetooth Low Energy (4.0). At bluetooth.com's dev-pagesyou 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.

本指南对蓝牙 3.0 很有前景。请记住,CoreBluetooth 框架仅适用于低功耗蓝牙 (4.0)。在bluetooth.com的dev-pages你可以看到一些全局定义服务的例子,正如关阳提到的,你可以看到心率服务是0x180D。单位的 UUID 由制造商定义。

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]

回答by viktorvogh

in Bluetooth, there are "Services". A device publishes 1..Nservices, and each service has 1..Mcharacteristics. Each service has an unique identifier, called UUID.

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

For example, a heart rate Bluetooth Sensor that offers the service "heart rate", publishes a service with UUID 0x180D.

例如,心脏速率传感器蓝牙的优惠,服务“心脏率”,出版与服务UUID 0x180D

(more services here)

(更多服务在这里

So when you perform a search, you must provide a UUIDas the criteria, telling which service to search.

因此,当您执行搜索时,您必须提供一个UUID作为条件,告诉您要搜索哪个服务。

回答by Guan Yang

You should take a look at one of the examples. Here's the relevant line:

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

[manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:nil];

(I know this is a Mac OS X example, but the iOS CoreBluetooth API is very similar.)

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

CBUUID identifies services you are interested in, not devices. Standard services have a 16-bit UUID, in this case 0x180d for the heart rate monitor or perhaps 0x180a for device information, while proprietary services have a 128-bit UUID (16 bytes).

CBUUID 标识您感兴趣的服务,而不是设备。标准服务有一个 16 位 UUID,在这种情况下,0x180d 用于心率监测器,或者 0x180a 用于设备信息,而专有服务有一个 128 位 UUID(16 字节)。

Most devices implement the device information service, so if you are just looking for any device, you could try [CBUUID UUIDWithString:@"180A"].

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

回答by Jes

Try giving this while you are scanning for devices

在扫描设备时尝试提供此信息

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

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

you will be able to get the list of devices at didDiscoverPeripheraldelegate method

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

回答by l0gg3r

Keep calm and use https://github.com/l0gg3r/LGBluetooth

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

All you need to do

所有你需要做的

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