iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers没有检索 [英] iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers not Retrieving

查看:788
本文介绍了iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers没有检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having an issue updating my CoreBluetooth code from iOS 6 to iOS 7. I can scan for Peripherals and make connections, however, I am not able to reconnect a Peripheral using the new CoreBluetooth methods offered within iOS 7. Here is a look at how I am trying to accomplish the reconnection:

- (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是保存的Peripheral UUID。

Where uuidString is the saved Peripheral UUID.

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

I am always getting to the NSLog statement where there are no available Peripherals. I suppose I am missing something very obvious and someone can point me in the right direction.

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

In addition, I have read other posts about the iOS 7 update issues with CoreBluetooth and have tried reseting the BLE device and the iOS device but to no avail.

提前致谢!
声音

Thanks in Advance! audible

推荐答案

事实证明,我的错误是如何将外围设备的标识符转换为<$ c在将消息发送到我的retrievePeripheral方法之前,$ c> NSString 格式。

It turns out that my mistake dealt with how I was converting the Peripheral's identifier into an NSString format before sending a message to my retrievePeripheral method.

这是不正确的方式:

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

这是正确的方式(在我的方案中有效):

This was the correct way (that worked in my scenario):

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

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

According to the Apple Documentation the NSUUID Class is not toll-free bridged with CoreFoundation's CFUUIDRef as described below:


注意: NSUUID 类与CoreFoundation的 CFUUIDRef 没有免费桥接。如果需要,使用UUID字符串在 CFUUID NSUUID 之间进行转换。两个 NSUUID 对象不能保证与指针值相当(因为 CFUUIDRef 是);使用 isEqual:比较两个 NSUUID 个实例。

Note: The NSUUID class is not toll-free bridged with CoreFoundation’s CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.

对于那些喜欢直观表达我这意味着什么的人来说,这是一个例子:)

For those of you who like a visual representation of what this means like I do here is an example:)

打印的外围信息控制台:

The Peripheral information printed within the Console:

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

将Peripheral的标识符转换为 NSString的错误方法收益率:

The incorrect way of converting the Peripheral's identifier into an NSString yields:

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

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

The correct way of converting the Peripheral's identifier into an NSString yields:

DD2468AB-1865-B926-7FA4-AE3755D479D8

我希望这可以帮助某人完成他们的BLE旅程,如果我错了,我可以随时纠正我,因为我还在学习这些东西。

I hope this helps someone on their BLE journey and feel free to correct me if I am wrong in any way as I am still learning this stuff as well.

谢谢!

听得见

这篇关于iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers没有检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆