我正在开发和应用,其中我需要显示设备的蓝牙信息。我终于获得了一些关于蓝牙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:%02xn", base[0], base[1], base[2], base[3], base[4], base[5]);
//fprintf(stderr, ">>> IPHONE BLUETOOTH MAC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02xn", 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);
}
那是不是有点像:[[UIDevice currentDevice] name];
给出了iOS设备的名称,比如"Henriks iPhone"
这在文档中不是明确的,但是在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
"错误)。