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

发布于 2024-09-04 07:04:19 字数 112 浏览 9 评论 0 原文

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

我假设蓝牙已激活,但没有设备与 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕烟庭风 2024-09-11 07:04:19

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

如果人们在 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);
    }

}

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

}
妄断弥空 2024-09-11 07:04:19

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

如果这是内部或越狱应用程序,您可以通过 kLockdownBluetoothAddressKey 键的值“nofollow noreferrer”>liblockdown.dylib

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

短叹 2024-09-11 07:04:19

我的 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文