iOS 蓝牙 - 立即配对弹窗

9
我正在创建一款连接到蓝牙耳机(BLE)的iOS应用程序。
  • 我搜索并连接我的iPhone与耳机
  • 我将设备与iPhone配对
  • 我打开我的应用程序,它会搜索蓝牙设备
  • 当应用程序找到我的设备时,它会请求连接
  • iOS弹出一个消息,要求用户按下“立即配对”按钮以连接设备
由于我已经在使用应用程序之前配对了设备,是否有办法在应用程序中不弹出“立即配对”弹窗的情况下连接设备?
--------------- 编辑1 --------- 我稍微改了一下代码。我在第一次连接时保存了设备的UUID,并在重新连接设备时尝试查找“已知外设”并重新连接它。代码实际上找到了“已知外设”,但在我尝试重新连接它之后,它又要求再次配对。是否有办法避免设备重新连接时弹出“立即配对”弹窗?
片段:
-(void) connectToPeripheral : (CBPeripheral*) peripheral {
    [self.centralManager stopScan];
    self.peripheral = peripheral;
    peripheral.delegate = self;
    [self.centralManager connectPeripheral:peripheral options:nil];
    self.peripheral = peripheral;
}

-(void) searchForDevices {
    // Scan for all available CoreBluetooth LE devices
    if (self.centralManager == nil ) {
        CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
        self.centralManager = centralManager;
    }

    //check if previous peripheral exists
    NSArray *knownPeripherals = nil;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* knownPeripheralID = [defaults stringForKey:@"knownPeripheralID"];
    if ( knownPeripheralID != nil ) {
        self.connectedPeripheralUUID = [[NSUUID alloc] initWithUUIDString:knownPeripheralID];
        knownPeripherals = [self.centralManager retrievePeripheralsWithIdentifiers:[NSArray arrayWithObjects:self.connectedPeripheralUUID, nil]];
    }


    if ( knownPeripherals != nil && [knownPeripherals count] > 0 ) {
        NSLog(@"knownPeripherals Peripherals");
        CBPeripheral *foundPeripheral = [knownPeripherals objectAtIndex:0];
        [self connectToPeripheral:foundPeripheral];
    } else {

        NSArray *connectedPeripherals = [self.centralManager retrieveConnectedPeripheralsWithServices:[NSArray arrayWithObjects:UUID_SERIAL_SERVICE_STR, nil]];
        NSLog(@"Connected Peripherals");

        if ( connectedPeripherals != nil && [connectedPeripherals count] > 0 ) {

        } else {
            [self.centralManager scanForPeripheralsWithServices:nil options:nil];
        }

    }
}

你能展示一下你在应用中使用的代码来查找和连接设备吗? - Paulw11
@Paulw11请现在检查。 - Panos
我有一个蓝牙应用程序,但我从未看到过这个“立即配对”弹出窗口?你是否无意中触发了警报?也许你从示例代码中复制了一些代码? - Braains
@Braains 这是弹出窗口,http://i.imgur.com/G1qqN8V.png(只显示我的设备名称)。您能否发布连接所使用的代码片段作为答案? 如果它可以与您的工作,则我将选择它作为答案。 - Panos
我可以帮你查找,但你得等到今晚回到电脑前。同时,你能否在你的项目中搜索“BlueTooth pairing request”这个短语(CMD+shift+f)?如果不是自定义警报,请参考这个答案,它可能会对你有所帮助:https://dev59.com/U-o6XIcBkEYKwwoYLhbh#18226632 - Braains
所以我在谷歌上搜索了一下,因为我注意到你的通知来自Pebble手表。似乎Pebble手表需要一个配对连接,可能是因为它处理来自手机的通知并且需要尽可能安全。我认为由于明显的安全问题,不可能在代码中消耗系统警报。 - Braains
1个回答

3
经过对该问题的研究,我认为除非是像我评论的那样一个自定义警报,否则您将无法摆脱这个问题!无论您尝试连接的耳机是什么,都会要求进行安全连接,并且CoreBluetooth会生成此警报。据我所知,没有办法在代码中消耗警报。
从苹果蓝牙文档中可以得到以下信息: (https://developer.apple.com/library/mac/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForSettingUpYourIOSDeviceAsAPeripheral/BestPracticesForSettingUpYourIOSDeviceAsAPeripheral.html#//apple_ref/doc/uid/TP40013257-CH5-SW7)。

Depending on the use case, you may want to vend a service that has one or more characteristic whose value needs to be secure. For example, imagine that you want to vend a social media profile service. This service may have characteristics whose values represent a member’s profile information, such as first name, last name, and email address. More than likely, you want to allow only trusted devices to retrieve a member’s email address.

You can ensure that only trusted devices have access to sensitive characteristic values by setting the appropriate characteristic properties and permissions. To continue the example above, to allow only trusted devices to retrieve a member’s email address, set the appropriate characteristic’s properties and permissions, like this:

emailCharacteristic = [[CBMutableCharacteristic alloc]
    initWithType:emailCharacteristicUUID
    properties:CBCharacteristicPropertyRead
    | CBCharacteristicPropertyNotifyEncryptionRequired
    value:nil permissions:CBAttributePermissionsReadEncryptionRequired]; In this

example, the characteristic is configured to allow only trusted devices to read or subscribe to its value. When a connected, remote central tries to read or subscribe to this characteristic’s value, Core Bluetooth tries to pair your local peripheral with the central to create a secure connection.

For example, if the central and the peripheral are iOS devices, both devices receive an alert indicating that the other device would like to pair. The alert on the central device contains a code that you must enter into a text field on the peripheral device’s alert to complete the pairing process.

After the pairing process is complete, the peripheral considers the paired central a trusted device and allows the central access to its encrypted characteristic values.

如果你收到这个警报,那么你尝试连接的蓝牙耳机需要一个安全连接来访问其中一个特征。更多信息可以在 @koshyyyk 在 stackoverflow.com/a/18226632/1112794 的回答中找到。还有一份苹果开发者关于蓝牙和配对概念的讲话,链接在评论中,我将在此处复制:http://adcdownload.apple.com//videos/wwdc_2012__hd/session_705__advanced_core_bluetooth.mov 如果你可以访问设备的开发者权限,则可以关闭此设置。如果没有,则可以选择不使用该特性,或者想办法将警报整合到你的应用程序中。
编辑:我的 BLE 代码不会触发此警报(如果相关)。此项目正在从 Red Bear Lab 的开源 BLE 代码进行修改, 可以在这里找到:https://github.com/RedBearLab/iOS
- (void) connectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"Connecting to peripheral with UUID : %@", peripheral.identifier.UUIDString);

    [self.activePeripherals addObject:peripheral];
    [self.CM connectPeripheral:peripheral
                       options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}

- (int) findBLEPeripherals:(int)timeout
{        
    if (self.CM.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"CoreBluetooth not correctly initialized !");
        return -1;
    }

    [NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];

    [self.CM scanForPeripheralsWithServices:nil options:nil];              
    return 0; // Started scanning OK !
}

1
确实,我们更改了BT设备的软件,现在它不再每次都弹出配对请求窗口。不过,直到我亲自拿到设备进行验证之前,这花费了一些时间。非常感谢。 - Panos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接