【问题标题】:How can I programmatically get the Bluetooth MAC address of an iPhone?如何以编程方式获取 iPhone 的蓝牙 MAC 地址?
【发布时间】:2010-06-03 20:20:59
【问题描述】:

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

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

【问题讨论】:

    标签: iphone objective-c mac-address


    【解决方案1】:

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

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

    您可以通过打开设置-通用-关于 并查看“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 4 上是正确的,但在 iPod 4 上不是——最后 5 个十六进制数字不同。
    • 在 iOS 7 及更高版本中,如果您询问 iOS 设备的 MAC 地址,系统返回值 02:00:00:00:00:00 developer.apple.com/news/?id=8222013a
    【解决方案2】:

    没有公共 API 可以获取此信息。

    如果这是一个内部或越狱应用程序,您可以通过liblockdown.dylib 获取kLockdownBluetoothAddressKey 键的值

    【讨论】:

    • 谢谢。我希望我能或多或少地用我们检索 WiFi MAC 地址的方式来获得它......
    • 我认为如果 iPhone 通过蓝牙处于网络共享模式,这可能是可行的。
    • 请发布代码 sn-p 以通过 liblockdown.dylib 使用 kLockdownBluetoothAddressKey 密钥
    【解决方案3】:

    我的 iPhone4 iOS 5.0.1 的 MAC 地址按以下顺序比较它们的最后一位:

    63 = Bluetooth
    64 = WiFi
    
    iPad2 v5.0.1 was:
    
    0D = Bluetooth
    0E = WiFi
    

    iPod-Touch 第二代 iOS 4.2.1 完全不同。

    ??.FC = WiFi
    xx.04 = Bluetooth
    

    【讨论】:

      猜你喜欢
      • 2018-06-12
      • 1970-01-01
      • 2014-06-25
      • 2014-10-28
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      相关资源
      最近更新 更多