4
votes

I want to get the mac address of the nearby devices using bluetooh Thats the format I want to get.

FE:4F:AD:37:67:5D

I have completely different format of mac address inside the delegate

5962C58F-BAD1-65D4-DCAC-06BBB06307C6

These are the delegates I'm using

CBCentralManagerDelegate

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)

This is my code

  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
            if(!peripherals.contains(peripheral)) {
                peripherals.append(peripheral)
            }
            
            let identifier = "\(peripheral.identifier)"
            if let data = identifier.data(using: .utf8) {
                let mac_address = data.hexEncodedString().uppercased()
                let macAddress = mac_address.separate(every: 2, with: ":")
                if let name = peripheral.name {
                    print("\(name) \n\(peripheral.identifier)\nMAC_ADDRESS: \(macAddress)")
                }
                
            }
        }
        
        
        func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
            mainPeripheral = nil
            print("Disconnected" + peripheral.name!)
        }
        
        
        func centralManagerDidUpdateState(_ central: CBCentralManager) {
            print(central.state)
            switch central.state {
            case .unknown:
                print("unknown")
            case .resetting:
                print("resetting")
            case .unsupported:
                print("unsupported")
            case .unauthorized:
                print("unauthorized")
            case .poweredOff:
                print("poweredOff")
                UIView.animate(withDuration: 0.7) {
                    self.lblBluetoothAlert.alpha = 1.0
                }
                stopScanForBLEDevices()
            case .poweredOn:
                UIView.animate(withDuration: 0.7) {
                    self.lblBluetoothAlert.alpha = 0.0
                }
                print("poweredOn")
                scanBLEDevices()
            default:
                print("default")
            }
        }
1
On iOS, we don't have real "MAC Addresses", their are "transformed" by the OS to avoid too much tracking. Until you pair (not only "connect", but a real pairing), they might change every 15 minutes. And did you look at the documentation of identifier It's an UUID, not a Mac Adress, not the same length/format...Larme
Agreed. This is intentionally impossible. What problem are you trying to solve?Rob Napier
I have a mac address of the device from my backend. I want to confirm if that device ( with that mac address) is present in the bluetooth range.I am scanning for all the devices and going through the mac addresses to confirm.Taimur Ajmal

1 Answers

1
votes

Core Bluetooth does not provide any access to the peripheral MAC address.

In order to achieve the requirement you outlined in your comment, namely identify a peripheral based on data from a back-end database, you would need your peripheral too expose its identifier (possibly its MAC address, possibly some other unique identifier) via a characteristic. Your app could then connect to target peripherals and interrogate the characteristic.