iphone - 如何以编程方式获取 iPhone 的蓝牙 MAC 地址?

标签 iphone objective-c mac-address

我正在尝试对 iPhone 进行一些接近检测,但我需要以编程方式获取它们的蓝牙 MAC 地址。有谁知道怎么做?

我假设蓝牙已激活,但没有设备与 iPhone 配对。

最佳答案

在我能拿到手的所有设备上,以下规则似乎都适用 - iPhone wifi MAC地址比iPhone蓝牙MAC地址大一 - iPad wifi MAC地址比iPad蓝牙MAC地址少一位。

如果人们在他们的 iPhone 或 iPad 上检查它会很有帮助,这样我们就可以增加对该理论的信心。我检查了一些 iPhone4、iPhone3 和 iPad1 设备。

可以打开设置-通用-关于 native 查看 并查看“Wi-Fi 地址”和“蓝牙”

如果理论正确,下面的合法代码将检索您的蓝牙 mac 地址:

#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);
    }

}

关于iphone - 如何以编程方式获取 iPhone 的蓝牙 MAC 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2969472/

相关文章:

objective-c - 在 View Controller 和 OS X 之间转换

ios - 如何检查使用 UISegmentedControl 选择了哪个段

objective-c - NSXMLParser 只读取一半的属性

bluetooth-lowenergy - 蓝牙 LE : Address Type

iphone - iPhone 上的 Safari iOS 中没有显示“赞”按钮

ios - 当我们使用Objective-C使用手指拖动时xib的垂直滑动

Objective-C:获取默认网关信息

c# - 获取计算机的MAC地址

iphone - 是否可以在 iPhone 应用程序中获取音乐播放器的数据或歌曲?

ios - ABCreateStringWithAddressDictionary 弃用