如何在iOS编程中以程序方式获取iPhone蓝牙适配器名称

4

我正在开发一个应用程序,需要显示设备的蓝牙信息。我已经成功获取了蓝牙MAC地址,但无法获取设备名称。是否有通过公共API合法获取设备名称的方法?

NSString*btMacAddr = @"Bt ";
BOOL                        success;
struct ifaddrs *            addrs;
const struct ifaddrs *      cursor;
const struct sockaddr_dl *  dlAddr;
const uint8_t *             base;

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);
                btMacAddr = [NSString stringWithFormat:@"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);
}

您是指运行该应用程序的iPhone的蓝牙名称,还是连接的设备的蓝牙名称? - Wildaker
运行该应用程序的iPhone的蓝牙名称 - Muhammad Usman
嗨@MuhammadUsman,你能解释一下你是如何获取蓝牙MAC地址的吗?我正在寻找这方面的信息。 - JuJoDi
1
嘿@JuJoDi,我已经使用了上面的代码来查找蓝牙MAC地址。 - Muhammad Usman
我在iPhone5上的iOS7上尝试了您的代码,但是无法获取蓝牙MAC地址和WIFI MAC地址,而只得到了以下信息:WIFI MAC地址:02:00:00:00:00:00 IPHONE蓝牙MAC地址:02:00:00:00:00:01 MAC地址:02:00:00:00:00:01 - Sjoerd Perfors
2个回答

1

那么它不是类似于以下内容吗: [[UIDevice currentDevice] name];

这将提供iOS设备的名称,例如“Henriks iPhone”。


正确,但蓝牙设备名称可能是除设备名称之外的任何内容。 - V-Xtreme
这是个好观点,Henrick。我认为在大多数情况下这都是正确的,因为我没有看到用户更改BT设备名称的方法。@V-Xtreme,你确定BT设备名称可以更改为UIDevice.name以外的内容吗? - phreakhead

0

文档中没有明确说明,但在CBCentralManagerDelegate方法中,例如centralManager:didDiscoverPeripheral:advertisementData:RSSI:,传递到委托的参数之一是CBPeripheral。

有趣的是,苹果没有列出任何关于CBPeripheral的官方文档,但如果您查看CBPeripheral.h的头文件,似乎有一个“_name”字段,您可以从中提取出来(对我而言,当我执行“peripheral.name”时,编译器没有抱怨,好像有一些属性被定义)

不过,为了验证我的答案,我尝试下载苹果的CoreBluetooth温度传感器示例代码,并在我的iPhone 4S上运行了一个命令"[centralManager scanForPeripheralsWithServices:nil options:nil];",但并没有取得太大的成功(即使蓝牙在设备设置中明确设置为开启,仍会生成一个"CoreBluetooth[WARNING] <CBConcreteCentralManager: 0x2087fc00> is not powered on"错误)。

无论如何,我希望我的提示能真正帮助到你。


抱歉,我没有听懂。您能否再解释一下或者举个例子来说明如何从头文件中提取信息? - Muhammad Usman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接