BLE扫描未知服务

时间:2015-12-27 23:28:58

标签: ios bluetooth

我正在尝试编写一个玩具应用程序以加快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硬件已开启并准备就绪

否则,该应用运行正常。没有错误或警告。但它永远找不到外围设备。

我做错了什么?是否有我应该扫描的其他服务?

谢谢!

修改
使用下面的建议,我使用LightBlue应用程序扫描了外围设备。我现在知道有一个广告服务使用这个UUID:

  

84100CBF-D622-8FF7-C8B8-300FDB52B92D

我将我的数组改为这样:

_services = @[[CBUUID UUIDWithString:@"84100CBF-D622-8FF7-C8B8-300FDB52B92D"]];

但仍未在日志中输出任何内容。我也试过传递nil,但仍然没有。

1 个答案:

答案 0 :(得分:1)

正如Paulw所指出的,只需发送nil而不是服务列表,无论他们宣传什么服务,您都可以获得范围内的所有外围设备。

请注意:

  • 这只会在前台工作。后台扫描要求您提供扫描服务
  • 在您的方法中,无法列出所有服务。除了您列出的短UUID服务(实际上是完整UUID的快捷方式)之外,设备可以使用任何" full" UUID。有2 ^ 128个可能的值...