Search code examples
iosswiftswift5ios-bluetooth

How to get the name of currently connected bluetooth device name in swift


I want to get the currently connected Bluetooth name in swift. I have tried `

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
         if central.state == .poweredOn {
             // Bluetooth is enabled,

             // Retrieve a list of connected peripherals

             let connectedPeripherals = central.retrievePeripherals(withIdentifiers: [])
             print("Connected device", connectedPeripherals)
             for peripheral in connectedPeripherals {
                 // Check if the peripheral is currently connected
                 if peripheral.state == .connected {
                     // Store the connected peripheral
//                     connectedPeripheral = peripheral
                     // Print the name of the connected peripheral
                     print(peripheral.name ?? "Unknown")
                 }
             }
         }
     }

` but I am getting 'connectedPeripherals' []. I am currently connecting with Bluetooth classic device. What I am doing wrong?I want to get the name "HR-BT-7cabd4 programmatically? Do I need to pass 'central.retrievePeripherals(withIdentifiers: [])' something in that array? I will attach a screenshot.I want to get the name "HR-BT-7cabd4 programmatically? How can I do that ? Thanks in advance


Solution

  • Core Bluetooth only provides access to Bluetooth Low Energy peripherals using the GATT profile. You can only get the details for which your app has a connection.

    In general, apps do not have visibility of any legacy Bluetooth devices that are connected.

    You can get some details of connected audio devices