How can I programmatically get the Bluetooth MAC address of an iPhone?

扶醉桌前 提交于 2019-11-28 12:20:46

There is no public API to get this information.

If this is an internal or jailbreak application you can get the value of the kLockdownBluetoothAddressKey key via liblockdown.dylib

On all devices I could get my hands on, the following rule seems to apply - iPhone wifi MAC address is one larger than iPhone bluetooth MAC address - iPad wifi MAC address is one less than iPad bluetooth MAC address.

It would be helpful if people check this on their iPhone or iPad, such that we can increase the confidence in the theory. I've checked on a few iPhone4, iPhone3 and iPad1 devices.

You can check it by opening Settings - General - About and looking at "Wi-Fi Address" and "Bluetooth"

If the theory is correct, the following legal code will retrieve your bluetooth mac address:

#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <net/if_dl.h>
#include <string.h>

#if ! defined(IFT_ETHER)
#define IFT_ETHER 0x6/* Ethernet CSMACD */
#endif

void doMacTest() {
    BOOL                        success;
    struct ifaddrs *            addrs;
    const struct ifaddrs *      cursor;
    const struct sockaddr_dl *  dlAddr;
    const uint8_t *             base;

    // We look for interface "en0" on iPhone

    success = getifaddrs(&addrs) == 0;
    if (success) {
        cursor = addrs;
        while (cursor != NULL) {
            if ( (cursor->ifa_addr->sa_family == AF_LINK)
                  && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER)
                  && (strcmp(cursor->ifa_name, "en0") == 0)) {
                dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
                base = (const uint8_t *) &dlAddr->sdl_data[dlAddr->sdl_nlen];

                if (dlAddr->sdl_alen == 6) {
                    fprintf(stderr, ">>>             WIFI MAC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n", base[0], base[1], base[2], base[3], base[4], base[5]);
                    fprintf(stderr, ">>> IPHONE BLUETOOTH MAC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n", base[0], base[1], base[2], base[3], base[4], base[5]-1);
                    fprintf(stderr, ">>>   IPAD BLUETOOTH MAC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n", base[0], base[1], base[2], base[3], base[4], base[5]+1);
                } else {
                    fprintf(stderr, "ERROR - len is not 6");
                }
            }
            cursor = cursor->ifa_next;
        }
        freeifaddrs(addrs);
    }

}
HotaruTech

MAC Address for my iPhone4 iOS 5.0.1 was in the following order comparing their last digits:

63 = Bluetooth
64 = WiFi

iPad2 v5.0.1 was:

0D = Bluetooth
0E = WiFi

The iPod-Touch 2nd Generation iOS 4.2.1 was totally different set.

??.FC = WiFi
xx.04 = Bluetooth
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!