Skip to content

Instantly share code, notes, and snippets.

@duanhong169
Created April 1, 2014 08:30
Show Gist options
  • Save duanhong169/9910162 to your computer and use it in GitHub Desktop.
Save duanhong169/9910162 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/netdevice.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/system_properties.h>
#include <stdio.h>
char cmd_res_line[256];
char total_cmd_res[25600];
char* exec_get_out(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe)
return NULL;
total_cmd_res[0] = 0;
while(!feof(pipe)) {
if(fgets(cmd_res_line, 256, pipe) != NULL)
{
//TODO: add handling for long command reads...
strcat(total_cmd_res,cmd_res_line);
}
}
pclose(pipe);
return total_cmd_res;
}
void getIMEI(char *g_imei)
{
char *imei_start = new char[PROP_VALUE_MAX];
//returns the string length of the value.
// int ir = __system_property_get("ro.product.manufacturer", imei_start);
int ir = __system_property_get("ro.product.model", imei_start);
// int ir = __system_property_get("ro.product.serialno", imei_start);
if(ir > 0){
imei_start[31]=0;//strz end
strcpy(g_imei,imei_start);
__android_log_print(ANDROID_LOG_ERROR, "TAG", "method IMEI [%s] len %d\r\n",imei_start,strlen(imei_start));
}
}
bool getMacAddress(char *pBuffer)
{
struct ifreq *ifr;
struct ifconf ifc;
int sock, i;
int numif;
// find number of interfaces.
memset(&ifc, 0, sizeof(ifc));
ifc.ifc_ifcu.ifcu_req = NULL;
ifc.ifc_len = 0;
if ((sock = ::socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
return false;
}
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
{
return false;
}
if ((ifr = (ifreq*) malloc(ifc.ifc_len)) == NULL)
{
return false;
}
ifc.ifc_ifcu.ifcu_req = ifr;
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
{
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(sock, SIOCGIFHWADDR, r) < 0)
{
continue;
}
memcpy(pBuffer, r->ifr_hwaddr.sa_data, 6);
close(sock);
free(ifr);
return true;
}
return false;
}
int main() {
char *mac = new char[14];
bool success = getMacAddress(mac);
if (success) {
__android_log_print(ANDROID_LOG_ERROR, "TAG", "MAC Address : %02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char) mac[0],
(unsigned char) mac[1],
(unsigned char) mac[2],
(unsigned char) mac[3],
(unsigned char) mac[4],
(unsigned char) mac[5]);
} else {
__android_log_print(ANDROID_LOG_ERROR, "TAG", "~~~get mac address failed~~");
}
char *imei = new char[32];
getIMEI(imei);
__android_log_print(ANDROID_LOG_ERROR, "TAG", "IMEI : %s", imei);
}
@melodicdeath
Copy link

你的getimei获取到的不是imei,而是设备型号啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment