本文介绍了iOS 蓝牙 LE:无法使用存储的配对数据进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做什么

我正在尝试将我的应用连接到需要配对的低功耗蓝牙设备.

I am trying to connect my app to a Bluetooth LE device which needs to be paired.

当前行为

没有配对设备和我的 iPhone 应用程序没有问题.我可以毫无问题地连接、重新连接和读/写特性.

There is no problem without pairing the device and my iPhone application. I am able to connect, reconnect and read/write characteristics without any problem.

但是,如果设备需要配对,我只能在第一次读取/写入特征,就在配对弹出确认之后.下一次,我发现应用程序并将其连接到我的设备,但我无权读取/写入特征数据,因为(我猜)我没有使用配对信息.

But, if the device need to be paired, I am only able to read/write characteristics the first time, right after the pairing popup confirmation. The next time, I discover and connect the app to my device, but I don't have the rights to read/write characteristics data because (I guess) I am not using the pairing information.

终于……

在网上搜索了几个小时但没有运气之后,我的问题是:

After spending few hours searching around the web with no luck here are my questions :

  • 如何使用存储在手机中的配对数据将我的应用程序从 iPhone 应用程序连接到蓝牙 LE 设备?我错过了什么吗?

  • How can I connect my app to a Bluetooth LE device from my iPhone app using the pairing data stored in my phone? Am I missing something?

是否可能不是 IOS 问题,因为如果手机中存在连接设备的配对数据,它会自动使用?

Is it possible that it is not an IOS problem because if pairing data are present in the phone for the connecting device, it is automatically used?

有没有蓝牙 LE 和 IOS 经验的人可以帮助我?

Is there someone with experience with Bluetooth LE and IOS to help me?

更新 2013-10-27

我发现在发现特征之后如果存在配对(没有确认弹出窗口),您无法通过配对身份验证读取受保护的特征.非保护特性没问题!我不知道为什么会发生这种情况,但行为是 IOS 应用程序永远不会从设备收到答案.

I have discovered that you can't read a protected characteristic by pairing authentication right after that the characteristic has been discovered if a pairing exists (no confirmation popup). No problem with non-protected characteristic! I don't know exactly why is happening, but the behavior is that the IOS app never receive answers from the device.

所以如果第一次阅读是在之后完成的,它不会引起问题.这是我用来发现注释中读取数据的特征的代码.

So if the first reading is done after, it doesn't cause problem. Here is the code I am using to discover characteristics with the data reading in comment.

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;
{
    NSArray     *characteristics    = [service characteristics];
    CBCharacteristic *characteristic;

    if (peripheral != servicePeripheral) {
        NSLog(@"Wrong Peripheral.
");
        return ;
    }

    if (service != batteryService) {
        NSLog(@"Wrong Service.
");
        return ;
    }

    if (error != nil) {
        NSLog(@"Error %@
", error);
        return ;
    }

    for (characteristic in characteristics) {
        NSLog(@"discovered characteristic %@", [characteristic UUID]);

        if ([[characteristic UUID] isEqual:[CBUUID UUIDWithString:kBatteryCharacteristicUUIDString]]) { // Bat
            NSLog(@"Discovered Bat Characteristic");
            batteryCharacteristic = [characteristic retain];
            //--> generate problem when pairing exists between IOS app and device
            //[peripheral readValueForCharacteristic:batteryCharacteristic];
        }
    }
}

推荐答案

您无需在应用中进行任何配对管理操作.

You don't have to do anything in your app for pairing management.

如果您的应用在 LE Central 模式下运行,并且外围设备发送 Insufficient Authentication 错误代码以响应读/写请求,iOS 将自动与您的设备配对并重试请求.

If your app runs in LE Central mode, and the peripheral sends an Insufficient Authentication error code in response to a read / write request, iOS will automatically pair with your device and will retry the request.

如果与设备断开连接,稍后重新连接,则外设需要再次发送 Insufficient Authentication 错误代码,以便 iPhone 重新启动加密.同样,您无需在此处对您的应用执行任何特殊操作.

If you disconnect from the device, and later reconnect again, the peripheral needs to send the Insufficient Authentication error code again for the iPhone to restart encryption. Again, you don't have to do anything special in your app here.

如果您的应用在 LE Peripheral 模式下运行,则情况会有所不同.设置 GATT 数据库时,请确保为 CBAttributePermissionsCBCharacteristicProperties 设置正确的标志.这将告诉 iOS 如果它没有配对,它应该自己发送 Insufficient Authentication 错误代码.然后由中央设备负责启动加密过程.

If your app runs in LE Peripheral mode, things are a bit different. When you set up your GATT database, make sure to set correct flags for both the CBAttributePermissions and CBCharacteristicProperties. This will tell iOS that it should send the Insufficient Authentication error code itself, if it is not paired. It is then the responsibility of the central device to start the encryption process.

Apple 产品的蓝牙配件设计指南中,描述了进一步的限制.

In the Bluetooth Accessory Design Guidelines for Apple Products, further restrictions are described.

  • 您的配件需要能够解析私有蓝牙地址.iPhone 会不时更改其公共蓝牙地址,只有配对的设备才能拥有正确的密钥来解析该公共地址并识别 iPhone.

  • Your accessory needs the capability to resolve private Bluetooth addresses. The iPhone will change its public Bluetooth address every now and then, and only paired devices will have the correct key to resolve that public address and recognize the iPhone.

第 3.9 节配对"也很有趣.

"Section 3.9 Pairing" is also interesting.

请注意,如果您在没有中间人 (MITM) 保护的情况下配对,您的外围设备可以使用生成的密钥来解析 iPhone 的私有蓝牙地址.但是,您将无法加密通道.

Note that if you pair without man-in-the-middle (MITM) protection, your peripheral can use the resulting key to resolve the private Bluetooth address of the iPhone. However, you won't be able to encrypt the channel.

在 iOS 上与 MITM 保护配对涉及输入远程设备显示的 PIN 码.据我所知,iOS 不支持通过外部通道发送配对数据的带外 (OOB) 配对(至少没有公共 API 来设置 OOB 数据).

Pairing with MITM protection on iOS involves entering a PIN code that is displayed by the remote device. Out-of-band (OOB) pairing where you send pairing data over an external channel is not supported by iOS as far as I know (at least there's no public APIs to set OOB data).

长话短说:如果您只有配对"/取消"配对,则无法加密 LE 通道,而只能在以后的连接中识别 iPhone.好消息是,即使您在 iPhone 端取消配对,甚至在恢复 iPhone 固件后,您仍然可以识别 iPhone ;-).

Long story short: if you have only a "Pair" / "Cancel" pairing, you cannot encrypt the LE channel but only recognize the iPhone in future connections. The nice thing is that you can still recognize the iPhone even if you unpair it on the iPhone side, and even after restoring the iPhone firmware ;-).

关于一般的 LE 加密:无论如何它都不安全(请参阅 http://eprint.iacr.org/2013/309a>).

Regarding LE encryption in general: it's not secure anyways (see http://eprint.iacr.org/2013/309).

这篇关于iOS 蓝牙 LE:无法使用存储的配对数据进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 04:03
查看更多