Android:擷取裝置的mac地址可靠的方法

來源:互聯網
上載者:User

標籤:android   wifi   mac   

/** * 擷取裝置的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() {@Overridepublic 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() {@Overridepublic void run() {callback.onComplete(info.getMacAddress());}});}return;}SystemClock.sleep(300);tryCount++;}// 未擷取到mac地址if (callback != null) {callback.onComplete(null);}}}).start();}

SimpleCallback是一個簡單的回調介面:

public interface SimpleCallback {void onComplete(String result);}

Android:擷取裝置的mac地址可靠的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.