Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

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

I'm trying to do some proximity detection of iPhones but I need to get their Bluetooth MAC address programmatically. Does anyone knows how ?

I assume Bluetooth is activated but no device is paired with the iPhone.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", base[0], base[1], base[2], base[3], base[4], base[5]);
                    fprintf(stderr, ">>> IPHONE BLUETOOTH MAC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x
", 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
", 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);
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...