How to get Mac Address From CBPeripheral And CBCenter
Asked Answered
I

2

12

I need to take target mac address from input connection and outgoing connection from CBPeripheral And CBCenter. identifier dose not define in them. look was remove from iOS 7. Is there any other way?

https://developer.apple.com/library/prerelease/ios/documentation/CoreBluetooth/Reference/CBPeripheral_Class/index.html

Issacissachar answered 15/10, 2015 at 10:14 Comment(7)
The hardware MAC address of the peripheral is not available.Horsy
Thanks, and how about CBCenter?Issacissachar
No, no MAC addresses are available. You can get the identifier but this is a UUID that is computed from the underlying MACHorsy
Is there any unique name or address? i want to store it in db, but in disconnect and reconnect i want that same address again.Issacissachar
that's right. but identifier not exist any more.Issacissachar
Use the identifier. As far as I know the documentation is incorrect. That property is still availableHorsy
you right. i have to update my XCode to last version. thanks @Paulw11. write them in answer i give u upvote and mark as answerIssacissachar
H
18

You can't get the MAC address for a CBPeripheral but you can get the identifier property, which is a UUID that iOS computes from the MAC amongst other information.

This value can be safely stored and used to identify the same peripheral in the future on this particular iOS device.

It cannot be used on another iOS device to identify the same peripheral.

Horsy answered 15/10, 2015 at 11:54 Comment(4)
I didn't found any proves of your words about "iOS computes from the MAC", maybe you can share the link to prove it? I've checked and different iPhones show different UUIDs for the same BLE device. Maybe this can identify the device for current iOs device, but not in general.Necrophobia
I should clarify my answer; the identifier can only be used on the same device. It cannot be used across devicesHorsy
But in android you can get MacAddress for my BLE deviceBindweed
Yes. But on iOS you can't.Horsy
M
-3

You can access to the MAC ADDRESS without problem in iOS 12. To get the mac address you have to follow the next steps.

  1. Parse the Data received by the BLE device to String.
extension Data{
func hexEncodedString() -> String {
        let hexDigits = Array("0123456789abcdef".utf16)
        var hexChars = [UTF16.CodeUnit]()
        hexChars.reserveCapacity(count * 2)

        for byte in self {
            let (index1, index2) = Int(byte).quotientAndRemainder(dividingBy: 16)
            hexChars.insert(hexDigits[index2], at: 0)
            hexChars.insert(hexDigits[index1], at: 0)
        }
        return String(utf16CodeUnits: hexChars, count: hexChars.count)
    }
}

  1. Add a separator ":" to the address.
extension String {
    func separate(every stride: Int = 4, with separator: Character = " ") -> String {
        return String(enumerated().map { $0 > 0 && $0 % stride == 0 ? [separator, $1] : [$1]}.joined())
    }
}
  1. In didReadValueForCharacteristic( characteristic: CBCharacteritic) you can use the previous 2 functions to get the mac address.
func didReadValueForCharacteristic(_ characteristic: CBCharacteristic) {
if characteristic.uuid == BleDeviceProfile.MAC_ADDRESS, let mac_address = characteristic.value?.hexEncodedString().uppercased(){
            let macAddress = mac_address.separate(every: 2, with: ":")
            print("MAC_ADDRESS: \(macAddress)")
        }
}
  1. enjoy your mac address: "MAC_ADDRESS: 00:0A:57:4E:86:F2"
Makkah answered 29/8, 2019 at 11:5 Comment(7)
Thanks a lot for the excellent workaround Can you please explain what exactly is this BleDeviceProfile.MAC_ADDRESSGaekwar
In my example, the MAC ADDRESS of the BLE Device came inside a characteristicMakkah
In this example, it seems like you had exposed intentionally the MAC as a characteristic. Is it that all BLE Devices exposed their mac as a Characteristic?Beatify
It does appear this is what he is doing and no, peripherals don't implement this characteristic. It is a custom characteristic and as far as I can tell is the only way to get the peripheral MAC address on iOS (Android makes it available via getAddress() method on the BLE device). This appears to be one more thing that is not exposed in the CoreBluetooth API. I had to implement a similar solution in order to obtain the MTU size. Again a simple thing to receive on Android but impossible in iOS forcing a workaround.Bye
@Bye Does it still work today?Pearlstein
I'm not sure what you are asking. Does creating a custom characteristic on the peripheral to use to expose the MAC address of the peripheral to the central still work? Sure, you can create any custom characteristic and put anything in it you want. But on iOS it is useless anyway except for your own identification purposes because iOS doesn't provide an interface to connect to a MAC address or even to their UUID identifier as far as I can tell. A scan is always required.Bye
And if you want to use the MAC address to find the same device a second time you would need to put the MAC address into the advertising data of your peripheral in order to compare with the MAC you retrieved from your custom characteristic the previous time you connected in the method: - (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)rssi{ }Bye

© 2022 - 2024 — McMap. All rights reserved.