iOS 蓝牙 - 立即配对弹出窗口

iOS Bluetooth - Pair now popup

我正在创建一个连接到蓝牙耳机 (BLE) 的 iOS 应用程序。

因为我在使用我的应用程序之前已经配对了设备,有没有什么方法可以在没有应用程序中的 "Pair now" 弹出窗口的情况下进行连接?

---------------- 编辑 1 ----------

我稍微修改了一下代码。我在第一次连接到设备时保存了设备的 UUID,当我重新连接设备时,应用程序找到保存的 UUID 并尝试找到 "known peripheral" 并重新连接到它。该代码实际上找到了 "known peripheral" 但在我尝试重新连接到它之后,它再次要求配对。有什么方法可以避免设备重新连接时弹出 "pair now" 吗?

片段:

-(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];
        }

    }
}

在围绕这个问题做了一些研究之后,我不认为这是你可以解决的问题(除非我评论了一个自定义警报,它应该很容易通过寻找任何东西来追踪)创建警报的代码!)。无论您尝试连接的是什么耳机,都在请求安全连接,并且 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 的回答中找到,这里是 Whosebug。com/a/18226632/1112794。还有一个 Apple Developer 谈论蓝牙和配对的概念链接在评论中,我将在此处复制: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 !
}