代码之家  ›  专栏  ›  技术社区  ›  AndroidDev

BLE扫描未知服务

  •  0
  • AndroidDev  · 技术社区  · 9 年前

    我正在尝试编写一个玩具应用程序,以跟上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 回复  |  直到 9 年前
        1
  •  1
  •   jcaron    9 年前

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

    请注意:

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