Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Bluetooth - Pair now popup

I am creating an iOS application that connects to a bluetooth headset (BLE).

  • I search & connect the headset to my iPhone
  • I pair the device with my iPhone
  • I open my application, it searches for bluetooth devices
  • When the application finds my device, it requests to connect to it.
  • The iOS pops up a message that asks the user to press the button "Pair now" to connect to the device

Since I have already paired the device before using my application, is there any way to connect without the "Pair now" popup within the application?

--------------- EDIT 1 ---------

I changed my code a bit. I save my device's UUID when I first connect to it and when I reconnect my device the application finds the saved UUID and tries to find the "known peripheral" and reconnect to it. The code actually finds the "known peripheral" but after I try to reconnect to it, it asks again to pair. Is there any way to avoid the "pair now" popup when the device reconnects?

Snippets:

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

    }
}
like image 882
Panos Avatar asked Jul 23 '15 08:07

Panos


People also ask

How do I get my Bluetooth to pop up on my iPhone?

On iPhone, go to Settings > Bluetooth, turn on Bluetooth, then tap the name of the device.

What does now discoverable mean on Bluetooth?

Discoverable mode is a state within Bluetooth technology integrated devices that enables Bluetooth devices to search, connect and transfer data with each other. Discoverable mode is used to propagate the availability of a Bluetooth device and to establish a connection with another device.


1 Answers

After doing some research around this issue I do not think that this is something that you'll be able to get around (unless it is as I commented a custom alert which should be easy to track down by looking for any code that creates an alert!). Whatever headset it is that you are trying to connect to is asking for a secure connection and CoreBluetooth is generating this alert. And as far as I know there is no way to consume an alert in code.

From the apple docs on Bluetooth (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.

If you're getting this alert then the BlueTooth headset you're trying to connect to is requiring a secure connection for one of the characteristics that you are trying to access. More information can be found from @koshyyyk 's answer here stackoverflow.com/a/18226632/1112794. There is also an Apple Developer talk about BlueTooth and the concept of pairing linked in the comments that I'll duplicate here: http://adcdownload.apple.com//videos/wwdc_2012__hd/session_705__advanced_core_bluetooth.mov

If you have developer access to the device you may be able to turn this off. If not then you can either choose to not use the characteristic or you can figure out some way of working the alert into your app.

EDIT My code for BLE that doesn't throw up the alert (in case it is relevant). This project is being adapted from Red Bear Lab's open source BLE code found here: 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 !
}
like image 81
Braains Avatar answered Oct 03 '22 22:10

Braains