如何通过C的方式在android NDK下面查找本机的mac地址
答案:2 悬赏:60 手机版
解决时间 2021-04-06 16:21
- 提问者网友:心如荒岛囚我终老
- 2021-04-05 20:55
如何通过C的方式在android NDK下面查找本机的mac地址
最佳答案
- 五星知识达人网友:詩光轨車
- 2021-04-05 21:33
bool GetMac(char *pBuffer)
{
struct ifreq *ifr;
struct ifconf ifc;
int s, i;
int numif;
// find number of interfaces.
memset(&ifc, 0, sizeof(ifc));
ifc.ifc_ifcu.ifcu_req = NULL;
ifc.ifc_len = 0;
if ((s = ::socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
CCLog("SgProfile::GetMac 111111");
return false;
}
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
{
CCLog("SgProfile::GetMac 222222");
return false;
}
if ((ifr = (ifreq*) malloc(ifc.ifc_len)) == NULL)
{
CCLog("SgProfile::GetMac 333333");
return false;
}
ifc.ifc_ifcu.ifcu_req = ifr;
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
{
CCLog("SgProfile::GetMac 333333 ioctl SIOCGIFCONF error!");
return false;
}
numif = ifc.ifc_len / sizeof(struct ifreq);
for (i = 0; i < numif; i++)
{
struct ifreq *r = &ifr[i];
struct sockaddr_in *sin = (struct sockaddr_in*)&r->ifr_addr;
if (!strcmp(r->ifr_name, "lo"))
continue; // skip loopback interface
// get MAC address
if(ioctl(s, SIOCGIFHWADDR, r) < 0)
{
CCLog("SgProfile::GetMac 333333 ioctl(SIOCGIFHWADDR) error!");
continue;
}
memcpy(pBuffer,r->ifr_hwaddr.sa_data,6);
close(s);
free(ifr);
return true;
}
return false;
}
{
struct ifreq *ifr;
struct ifconf ifc;
int s, i;
int numif;
// find number of interfaces.
memset(&ifc, 0, sizeof(ifc));
ifc.ifc_ifcu.ifcu_req = NULL;
ifc.ifc_len = 0;
if ((s = ::socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
CCLog("SgProfile::GetMac 111111");
return false;
}
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
{
CCLog("SgProfile::GetMac 222222");
return false;
}
if ((ifr = (ifreq*) malloc(ifc.ifc_len)) == NULL)
{
CCLog("SgProfile::GetMac 333333");
return false;
}
ifc.ifc_ifcu.ifcu_req = ifr;
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
{
CCLog("SgProfile::GetMac 333333 ioctl SIOCGIFCONF error!");
return false;
}
numif = ifc.ifc_len / sizeof(struct ifreq);
for (i = 0; i < numif; i++)
{
struct ifreq *r = &ifr[i];
struct sockaddr_in *sin = (struct sockaddr_in*)&r->ifr_addr;
if (!strcmp(r->ifr_name, "lo"))
continue; // skip loopback interface
// get MAC address
if(ioctl(s, SIOCGIFHWADDR, r) < 0)
{
CCLog("SgProfile::GetMac 333333 ioctl(SIOCGIFHWADDR) error!");
continue;
}
memcpy(pBuffer,r->ifr_hwaddr.sa_data,6);
close(s);
free(ifr);
return true;
}
return false;
}
全部回答
- 1楼网友:慢性怪人
- 2021-04-05 22:34
不明白啊 = =!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯