How get the list of paired bluetooth devices in swift?

前端 未结 4 1234
猫巷女王i
猫巷女王i 2020-12-10 04:34

I need to get the list of paired bluetooth devices(iOS Devices) as same as the list in \'Bluetooth\' section in iOS settings as shown in below picture.



Is

相关标签:
4条回答
  • 2020-12-10 04:59

    It's not possible to retrieve list of paired peripherals from iOS. Neither it's possible to check if specific peripheral is paired.

    Retrieving peripheral which is paired

    There are two cases which you need to consider:

    1. Peripheral may be already connected in the system (iOS connects automatically with some peripherals in order to for example display battery level). In this case peripheral won't be broadcasting and detection using scanForPeripherals won't work.

    2. Peripheral is paired, but disconnected. In this case retrieveConnectedPeripherals(withServices:) won't work.

    Therefore to retrieve your peripheral you need to combine both things. First you need to check if it's in peripherals returned from retrieveConnectedPeripherals(withServices:). If not you should scanForPeripherals.

    If you want to retrieve peripheral which is out of range, you can try to use retrievePeripherals(withIdentifiers:), however it may return also not paired devices and it relies on peripheral's UUID which you have to save after pairing.

    Detecting if peripheral is paired

    There is one way to detect if the specific peripheral is paired. You need to try to read from protected characteristic (which requires encryption - bonding). If you receive expected data, it means that user accepted pairing request. Otherwise you will receive empty response or none.

    References

    You can read more about Bluetooth in my articles:

    • Original answer
    • Swift – Bluetooth Low Energy communication using Flow Controllers
    • How to communicate with Bluetooth Low Energy devices on iOS
    • Bluetooth Low Energy – background mode on iOS
    0 讨论(0)
  • 2020-12-10 05:08

    You can use ShowBluetoothAccessoryPicker if you are using the Classic Bluetooh into an MFi device.

    https://developer.apple.com/documentation/externalaccessory/eaaccessorymanager/1613913-showbluetoothaccessorypicker

    The code below is a C# but you can easily convert it into the IOS (object c your swift)

    EAAccessoryManager.SharedAccessoryManager.ShowBluetoothAccessoryPicker(null, completion: ((Foundation.NSError error) => {
                    Console.WriteLine("My callback");
                    if (error != null && (uint)error.Code != (uint)EABluetoothAccessoryPickerError.AlreadyConnected)
                    {
                        Console.WriteLine(String.Format("Error code: {0} Desc: {1}", error.Code, error.DebugDescription));
                        Console.WriteLine("Failed? " + EABluetoothAccessoryPickerError.Failed.ToString());
                        Console.WriteLine("Failed? " + Convert.ToInt64(EABluetoothAccessoryPickerError.Failed));
                    }
                }));
    
    0 讨论(0)
  • 2020-12-10 05:12

    It is possible to get the list of paired/connected devices if only you have their advertisement UUID.

    dispatch_queue_t centralQueu = dispatch_queue_create("A_NAME", NULL);
    _centralManager = [[CBCentralManager alloc]
                                   initWithDelegate:self
                                   queue:centralQueu
                                   options: @{CBCentralManagerOptionRestoreIdentifierKey:@"RESTORE_KEY",
                                              CBCentralManagerOptionShowPowerAlertKey: @YES}];
    _ServiceUUIDs = @[[CBUUID UUIDWithString:@"THE_UUID"]]  //the array of CBUUIDs which you are looking for
    [_centralManager retrieveConnectedPeripheralsWithServices:_ServiceUUIDs]
    

    You can get the BLE devices advertisement UUID using some iOS applications, such as LightBlue and nRFConnect

    0 讨论(0)
  • 2020-12-10 05:13

    You need to find the service UUID in which you are interested, in my case it works perfectly,

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]
                                                options:options];
    

    and when it will find any device which advertise same service UUID, then it will appear in the screen which you have pointed above.

    handle didDiscoverperipherel in this way:

    -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
    
        _discoveredPeripheral = peripheral;
    
        if(![self.mRemoteDevices containsObject:_discoveredPeripheral])
        {
             NSArray *peripherels = [self.centralManager retrievePeripheralsWithIdentifiers:@[_discoveredPeripheral.identifier]];
            [self.mRemoteDevices addObject:[peripherels objectAtIndex:0]];
        }
    }
    
    0 讨论(0)
提交回复
热议问题