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")
}
}
identifier
It's an UUID, not a Mac Adress, not the same length/format... – Larme