【问题标题】:IOS CoreBluetooth. Cannot discover correct servicesIOS 核心蓝牙。无法发现正确的服务
【发布时间】:2013-06-09 12:12:46
【问题描述】:

我在连接到所需的外围设备后尝试发现服务,但我只能根据 UUID 找到不存在的服务。我的意思是;作为一种检查手段,我还使用了一个名为“Light Blue”的应用商店应用。 Light Blue 只是一个 BLE 浏览器,它可以识别所有外围设备、服务和特性。我可以使用 Light Blue 看到所需的服务和特征,因此我知道它们存在并且 Light Blue 识别的那些是正确的。

这是我的代码以及我如何识别所有服务、包含的服务和特征:

- (void)printAllChar:(CBPeripheral*)p {
    printf("**************************************\n");
    printf("Listing all services and characteristics\n");

    CBService * s, * inc;
    CBCharacteristic * c;

    //Discover all services on peripheral
    [p discoverServices:nil];

    printf("%d services discovered\n", p.services.count);
    for(int i = 0; i < p.services.count; i++){

        //Traverse all services
        s = [p.services objectAtIndex:i];
        printf("Service %d: %s\n", i+1, s.UUID);

        [p discoverIncludedServices:nil forService:s];

        printf("    Included Services Discovered: %d\n", s.includedServices.count);

        //Traverse all included services of each service
        for(int k = 0; k < s.includedServices.count; k++){
            inc = [s.includedServices objectAtIndex:k];
            printf("    %d: %s\n", k+1, inc.UUID]);            
        }

        [p discoverCharacteristics:nil forService:s];

        printf("%d Characteristics discovered\n", s.characteristics.count);

        //Traverse all characteristic of each service
        for(int j = 0; j < s.characteristics.count; j++){
            c = [s.characteristics objectAtIndex:j];
            printf("Characteristic %d: %s\n", j+1, c.UUID);
        }
    }
}

^^^我知道打印函数实际上不会运行,因为 s.UUID 不是字符串变量,我已经删除了所有转换代码以使其更具可读性。

我认为值得一提的是,回调函数 didDiscoverServices 永远不会被调用。

这是上述代码的示例输出:

**************************************
Listing all services and characteristics
3 services discovered
Service 1: 02000000-0200-0000-60D0-E03B02000000
    Included Services Discovered: 0
    0 Characteristics discovered
Service 2: 02000000-0200-0000-18C0-F03B00005444
    Included Services Discovered: 0
    0 Characteristics discovered
Service 3: 00000000-1000-0000-B4CC-E03B82130001
    Included Services Discovered: 0
    0 Characteristics discovered

没有任何服务是我所期望的。我已经连接了正确的外围设备,但这些服务对我来说是完全未知的。

我应该找到的服务 ID 如下:

//Service UUID
#define BLE_DEVICE_SERVICE_UUID             "713D0000-503E-4C75-BA94-3148F18D941E"

//Characteristic UUID  
#define BLE_DEVICE_VENDOR_NAME_UUID         "713D0001-503E-4C75-BA94-3148F18D941E"

#define BLE_DEVICE_RX_UUID                  "713D0002-503E-4C75-BA94-3148F18D941E"

#define BLE_DEVICE_TX_UUID                  "713D0003-503E-4C75-BA94-3148F18D941E"

#define BLE_DEVICE_RESET_RX_UUID            "713D0004-503E-4C75-BA94-3148F18D941E"

#define BLE_DEVICE_LIB_VERSION_UUID         "713D0005-503E-4C75-BA94-3148F18D941E"

有人知道发生了什么吗?

如果相关,我正在使用 RedBearLabs BLEMini。

【问题讨论】:

  • 您应该找到什么服务 UUID?为什么不更多地使用 Objective-C 而不是 C,例如 NSLog?
  • 我用更多信息编辑了我的帖子。我想我只是更习惯于 C,这是我第一次接触 IOS 开发。使用 NSLog 会有更多有用的信息吗?
  • 嗯,我认为 NSLog 简化了很多事情,比如在日志中添加时间戳,最后“自动添加\n”和神奇的%@(例如NSLog(@"Service %d: %@", i+1, s.UUID); . 那么,你使用callBack方法了吗?你看到Apple的温度传感器样本了吗?
  • 是的,我有,非常感谢您的帮助。我认为我所有调用的结构都模仿了我见过的大多数示例,尽管这并不完全正确,因为我有错误
  • 您是否尝试过使用 Apple 的温度传感器样本,修改扫描?

标签: ios bluetooth core-bluetooth bluetooth-lowenergy ios6


【解决方案1】:

您确实应该尝试在回调方法中执行此操作。您说他们不会被调用——您是否将您的课程设置为 CBPeripheralDelegate 并将外围设备的委托设置为 self

(另外,顺便说一句:Objective-C 有一个foreach 循环,比手动设置带有计数器的for 循环更方便):

for(CBService *s in peripheral.services){
    NSLog(@"%@",s.uuid); //print the service uuid to console
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 2014-08-27
    相关资源
    最近更新 更多