android开发怎么用脚本获取手机的mac地址
1个回答
展开全部
/**
* 获取设备的mac地址
*
* @param ac
* @param callback
* 成功获取到mac地址之后会回调此方法
*/
public static void getMacAddress(final Activity ac, final SimpleCallback callback) {
final WifiManager wm = (WifiManager) ac .getSystemService(Service.WIFI_SERVICE);
// 如果本次开机后打开过WIFI,则能够直接获取到mac信息。立刻返回数据。
WifiInfo info = wm.getConnectionInfo();
if (info != null && info.getMacAddress() != null) {
if (callback != null) {
callback.onComplete(info.getMacAddress());
}
return;
}
// 尝试打开WIFI,并获取mac地址
if (!wm.isWifiEnabled()) {
wm.setWifiEnabled(true);
}
new Thread(new Runnable() {
@Override
public void run() {
int tryCount = 0;
final int MAX_COUNT = 10;
while (tryCount < MAX_COUNT) {
final WifiInfo info = wm.getConnectionInfo();
if (info != null && info.getMacAddress() != null) {
if (callback != null) {
ac.runOnUiThread(new Runnable() {
@Override
public void run() {
callback.onComplete(info.getMacAddress());
}
});
}
return;
}
SystemClock.sleep(300);
tryCount++;
}
// 未获取到mac地址
if (callback != null) {
callback.onComplete(null);
}
}
}).start();
}
* 获取设备的mac地址
*
* @param ac
* @param callback
* 成功获取到mac地址之后会回调此方法
*/
public static void getMacAddress(final Activity ac, final SimpleCallback callback) {
final WifiManager wm = (WifiManager) ac .getSystemService(Service.WIFI_SERVICE);
// 如果本次开机后打开过WIFI,则能够直接获取到mac信息。立刻返回数据。
WifiInfo info = wm.getConnectionInfo();
if (info != null && info.getMacAddress() != null) {
if (callback != null) {
callback.onComplete(info.getMacAddress());
}
return;
}
// 尝试打开WIFI,并获取mac地址
if (!wm.isWifiEnabled()) {
wm.setWifiEnabled(true);
}
new Thread(new Runnable() {
@Override
public void run() {
int tryCount = 0;
final int MAX_COUNT = 10;
while (tryCount < MAX_COUNT) {
final WifiInfo info = wm.getConnectionInfo();
if (info != null && info.getMacAddress() != null) {
if (callback != null) {
ac.runOnUiThread(new Runnable() {
@Override
public void run() {
callback.onComplete(info.getMacAddress());
}
});
}
return;
}
SystemClock.sleep(300);
tryCount++;
}
// 未获取到mac地址
if (callback != null) {
callback.onComplete(null);
}
}
}).start();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询