我正在尝试编写一个玩具应用程序来加快 BLE 的速度。我有一个外围设备(它是一个 Tile ),我想扫描它的服务。但是,我不知道它提供什么服务(而且我在产品的技术规范中也没有看到这一点)。所以,我得到了一个 list of services来自蓝牙规范,我正在尝试扫描所有这些规范。我只是假设此列表是详尽无遗的,因为它没有说明任何一种方式。
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Scan for all available CoreBluetooth LE devices
_services = @[[CBUUID UUIDWithString:@"1811"],
[CBUUID UUIDWithString:@"1815"],
[CBUUID UUIDWithString:@"180F"],
[CBUUID UUIDWithString:@"1810"],
[CBUUID UUIDWithString:@"181B"],
[CBUUID UUIDWithString:@"181E"],
[CBUUID UUIDWithString:@"181F"],
[CBUUID UUIDWithString:@"1805"],
[CBUUID UUIDWithString:@"1818"],
[CBUUID UUIDWithString:@"1816"],
[CBUUID UUIDWithString:@"180A"],
[CBUUID UUIDWithString:@"181A"],
[CBUUID UUIDWithString:@"1800"],
[CBUUID UUIDWithString:@"1801"],
[CBUUID UUIDWithString:@"1808"],
[CBUUID UUIDWithString:@"1809"],
[CBUUID UUIDWithString:@"180D"],
[CBUUID UUIDWithString:@"1823"],
[CBUUID UUIDWithString:@"1812"],
[CBUUID UUIDWithString:@"1802"],
[CBUUID UUIDWithString:@"1821"],
[CBUUID UUIDWithString:@"1820"],
[CBUUID UUIDWithString:@"1803"],
[CBUUID UUIDWithString:@"1819"],
[CBUUID UUIDWithString:@"1807"],
[CBUUID UUIDWithString:@"1825"],
[CBUUID UUIDWithString:@"180E"],
[CBUUID UUIDWithString:@"1822"],
[CBUUID UUIDWithString:@"1806"],
[CBUUID UUIDWithString:@"1814"],
[CBUUID UUIDWithString:@"1813"],
[CBUUID UUIDWithString:@"1824"],
[CBUUID UUIDWithString:@"1804"],
[CBUUID UUIDWithString:@"181C"],
[CBUUID UUIDWithString:@"181D"]];
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[centralManager scanForPeripheralsWithServices:_services options:nil];
self.centralManager = centralManager;
}
- (IBAction)scanForDevices:(UIButton *)sender {
[_centralManager scanForPeripheralsWithServices:_services options:nil];
}
// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter.
// This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if ([localName length] > 0) {
NSLog(@"Found the heart rate monitor: %@", localName);
[self.centralManager stopScan];
_hRMPeripheral = peripheral;
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
// Determine the state of the peripheral
if ([central state] == CBCentralManagerStatePoweredOff) {
NSLog(@"CoreBluetooth BLE hardware is powered off");
}
else if ([central state] == CBCentralManagerStatePoweredOn) {
NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([central state] == CBCentralManagerStateUnauthorized) {
NSLog(@"CoreBluetooth BLE state is unauthorized");
}
else if ([central state] == CBCentralManagerStateUnknown) {
NSLog(@"CoreBluetooth BLE state is unknown");
}
else if ([central state] == CBCentralManagerStateUnsupported) {
NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
}
}
我得到的唯一日志输出是这样的:
CoreBluetooth BLE hardware is powered on and ready
否则,应用程序运行良好。没有错误或警告。但它永远找不到外围设备。
我做错了什么?我应该扫描其他服务吗?
谢谢!
编辑
根据下面的建议,我使用 LightBlue 应用程序扫描了外围设备。我现在知道有一个使用此 UUID 的广告服务:
84100CBF-D622-8FF7-C8B8-300FDB52B92D
我将数组更改为如下所示:
_services = @[[CBUUID UUIDWithString:@"84100CBF-D622-8FF7-C8B8-300FDB52B92D"]];
但日志中仍然没有任何输出。我也尝试传递 nil
但仍然没有。
最佳答案
正如 Paulw 所指出的,只需发送 nil
而不是服务列表,您将获得范围内的所有外围设备,无论它们宣传什么服务。
请注意:
- 这只会在前台工作。后台扫描需要您提供扫描服务
- 按照您的方法,不可能列出所有服务。除了您列出的具有短 UUID 的服务(实际上是完整 UUID 的快捷方式)之外,设备还可以宣传具有任何“完整”UUID 的服务。有 2^128 个可能的值...
关于ios - BLE 扫描未知服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485986/