iOS蓝牙 - 现在配对弹出

Pan*_*nos 7 iphone bluetooth ios core-bluetooth

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

  • 我搜索并将耳机连接到我的iPhone
  • 我将设备与iPhone配对
  • 我打开我的应用程序,它搜索蓝牙设备
  • 当应用程序找到我的设备时,它会请求连接到它.
  • iOS会弹出一条消息,要求用户按"立即配对"按钮连接到设备

由于我在使用我的应用程序之前已经配对了设备,是否有任何方法可以在应用程序中没有"立即配对"弹出窗口进行连接?

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

我改变了我的代码.我第一次连接时保存设备的UUID,当我重新连接设备时,应用程序找到保存的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];
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*ins 3

在围绕这个问题进行了一些研究之后,我认为这是您无法解决的问题(除非它是我评论的自定义警报,通过查找创建创建警报的任何代码应该很容易追踪到它)警报!)。无论您尝试连接的耳机是什么,都要求安全连接,并且 CoreBluetooth 会生成此警报。据我所知,没有办法在代码中使用警报。

\n\n

来自蓝牙上的苹果文档(https://developer.apple.com/library/mac/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForSettingUpYourIOSDeviceAsAPeripheral/BestPracticesForSettingUpYourIOSDeviceAsAPeripheral.html#//apple_ref/doc/uid/TP40013257-CH5-SW7

\n\n
\n

根据使用案例,您可能希望提供具有一个或多个特性(其值需要安全)的服务。例如,假设您想要出售社交媒体资料服务。此服务\n 可能具有其值表示成员\xe2\x80\x99s\n 个人资料信息的特征,例如名字、姓氏和电子邮件地址。\n 很可能,您希望只允许受信任的设备检索a\n 成员\xe2\x80\x99s 电子邮件地址。

\n\n

通过设置适当的特征属性和权限,您可以确保只有受信任的设备才能访问敏感特征值。要继续上面的示例,要仅允许受信任的设备检索成员 xe2\x80\x99s 电子邮件地址,请设置\n 适当的特征\xe2\x80\x99s 属性和权限,如下所示:

\n\n
emailCharacteristic = [[CBMutableCharacteristic alloc]\n    initWithType:emailCharacteristicUUID\n    properties:CBCharacteristicPropertyRead\n    | CBCharacteristicPropertyNotifyEncryptionRequired\n    value:nil permissions:CBAttributePermissionsReadEncryptionRequired]; In this\n
Run Code Online (Sandbox Code Playgroud)\n\n

例如,该特征被配置为仅允许受信任的设备读取或订阅其值。当连接的远程中心尝试读取或订阅此特征值时,核心蓝牙会尝试将本地外围设备与中心配对以创建安全连接。

\n\n

例如,如果中央设备和外围设备都是 iOS 设备,则两个设备都会收到一条警报,表明另一个设备想要配对。中央设备上的警报包含一个代码,您必须将其输入到外围设备上的文本字段中\xe2\x80\x99s 警报才能完成\n 配对过程。

\n\n

配对过程完成后,外围设备将配对的中央设备视为可信设备,并允许中央设备访问其加密特征值。

\n
\n\n

如果您收到此警报,则您尝试连接的蓝牙耳机需要安全连接才能实现您尝试访问的特征之一。更多信息可以从 @koshyyyk 的答案 stackoverflow.com/a/18226632/1112794 找到。还有一个关于蓝牙和配对概念的苹果开发者讨论,链接在评论中,我将在此处复制: http://adcdownload.apple.com//videos/wwdc_2012__hd/session_705__advanced_core_bluetooth.mov

\n\n

如果您拥有该设备的开发人员访问权限,您可以将其关闭。如果没有,那么您可以选择不使用该特性,或者您可以找到某种方法将警报应用到您的应用程序中。

\n\n

编辑我的 BLE 代码,它不会发出警报(如果相关的话)。该项目改编自 Red Bear Lab 的开源 BLE 代码,可在此处找到: https: //github.com/RedBearLab/iOS

\n\n
- (void) connectPeripheral:(CBPeripheral *)peripheral\n{\n    NSLog(@"Connecting to peripheral with UUID : %@", peripheral.identifier.UUIDString);\n\n    [self.activePeripherals addObject:peripheral];\n    [self.CM connectPeripheral:peripheral\n                       options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];\n}\n\n- (int) findBLEPeripherals:(int)timeout\n{        \n    if (self.CM.state != CBCentralManagerStatePoweredOn)\n    {\n        NSLog(@"CoreBluetooth not correctly initialized !");\n        return -1;\n    }\n\n    [NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];\n\n    [self.CM scanForPeripheralsWithServices:nil options:nil];              \n    return 0; // Started scanning OK !\n}\n
Run Code Online (Sandbox Code Playgroud)\n