【问题标题】:iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers not RetrievingiOS 7 CoreBluetooth 检索PeripheralsWithIdentifiers 未检索
【发布时间】:2013-10-09 05:40:12
【问题描述】:

我在将我的 CoreBluetooth 代码从 iOS 6 更新到 iOS 7 时遇到问题。我可以扫描外围设备并建立连接,但是,我无法使用 iOS 7 中提供的新 CoreBluetooth 方法重新连接外围设备。这里是看看我是如何尝试完成重新连接的:

- (void)retrievePeripheral:(NSString *)uuidString
{

NSUUID *nsUUID = [[NSUUID UUID] initWithUUIDString:uuidString];

if(nsUUID)
{        
    NSArray *peripheralArray = [centralManager retrievePeripheralsWithIdentifiers:@[nsUUID]];

    // Check for known Peripherals
    if([peripheralArray count] > 0)
    {
        for(CBPeripheral *peripheral in peripheralArray)
        {
            NSLog(@"Connecting to Peripheral - %@", peripheral);

            [self connectPeripheral:peripheral];
        }
    }
    // There are no known Peripherals so we check for connected Peripherals if any
    else
    {
        CBUUID *cbUUID = [CBUUID UUIDWithNSUUID:nsUUID];

        NSArray *connectedPeripheralArray = [centralManager retrieveConnectedPeripheralsWithServices:@[cbUUID]];

        // If there are connected Peripherals
        if([connectedPeripheralArray count] > 0)
        {
            for(CBPeripheral *peripheral in connectedPeripheralArray)
            {
                NSLog(@"Connecting to Peripheral - %@", peripheral);

                [self connectPeripheral:peripheral];
            }
        }
        // Else there are no available Peripherals
        else
        {
            // No Dice!
            NSLog(@"There are no available Peripherals");
        }
    }
}

}

其中 uuidString 是保存的外设 UUID。

我总是遇到没有可用外设的 NSLog 语句。我想我错过了一些非常明显的东西,有人可以指出我正确的方向。

此外,我阅读了其他有关 CoreBluetooth 的 iOS 7 更新问题的帖子,并尝试重置 BLE 设备和 iOS 设备,但无济于事。

提前致谢! 听得见

【问题讨论】:

    标签: objective-c iphone ipad ios7 core-bluetooth


    【解决方案1】:

    事实证明,我的错误在于在向我的 retrievePeripheral 方法发送消息之前,我如何将 Peripheral 的标识符转换为 NSString 格式。

    这是错误的方式:

    NSString *uuidString = [NSString stringWithFormat:@"%@", CFUUIDCreateString(NULL, (__bridge CFUUIDRef)([peripheral identifier]))];
    

    这是正确的方法(在我的场景中有效):

    NSString *uuidString = [NSString stringWithFormat:@"%@", [[peripheral identifier] UUIDString]];
    

    根据 Apple 文档,NSUUID 类不是免费桥接 CoreFoundation 的 CFUUIDRef,如下所述:

    注意:NSUUID 类不是与 CoreFoundation 的CFUUIDRef 桥接的免费电话。如果需要,使用 UUID 字符串在 CFUUIDNSUUID 之间进行转换。不能保证两个 NSUUID 对象可以通过指针值进行比较(就像 CFUUIDRef 一样);使用isEqual: 比较两个NSUUID 实例。

    对于那些喜欢像我在这里所做的那样直观表示这意味着什么的人,这是一个示例:)

    在控制台中打印的外设信息:

    <CBPeripheral: 0x14699430 identifier = DD2468AB-1865-B926-7FA4-AE3755D479D8, Name = "IDYNAMO EMV-A27F", state = disconnected>
    

    将外围设备的标识符转换为NSString 的错误方法会产生:

    1865B926-7FA4-AE37-55D4-79D800000000
    

    将外围设备的标识符转换为NSString 的正确方法是:

    DD2468AB-1865-B926-7FA4-AE3755D479D8
    

    我希望这对某人的 BLE 之旅有所帮助,如果我有任何错误,请随时纠正我,因为我还在学习这些东西。

    谢谢!

    听得见

    【讨论】:

    • 我想知道您是否可以帮助我将代码转换为retrievePeripheralsWithIdentifiers - (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)aPeripheral AdvertisementData:(NSDictionary *)advertisementData RSSI: (NSNumber *)RSSI { NSMutableArray *peripherals = [self mutableArrayValueForKey:@"heartRateMonitors"]; if(![self.heartRateMonitors containsObject:aPeripheral]) [外设 addObject:aPeripheral]; [self.manager retrievePeripherals:[NSArray arrayWithObject:(id)aPeripheral.UUID]]; }
    【解决方案2】:

    这是一篇旧帖子,但供其他人寻找答案。

    retrieveConnectedPeripherals 的第二次/下一次调用实际上需要 Service UUID(CBUUID 类型),而不是 iOS 首次连接的 NSUUID。检索到的是实现了该服务 UUID 的 och 外围设备列表。例如,如果您有许多活动臂章,并且都具有您需要的心跳服务等,您将检索所有 3 个外围设备。同样在第一次调用 KNOWN 外围设备时,您必须在应用程序启动之间存储 NSUUID,因为该 ID 是由 iOS 在第一次连接时创建的。希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-19
      • 2013-11-18
      • 2019-08-20
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多