Android 获取无线蓝牙MAC信息代码

来源:互联网 发布:网络出版是什么意思 编辑:程序博客网 时间:2024/06/02 00:21

转自:http://blog.csdn.net/tody_guo/article/details/6646709

[java] view plaincopy
  1. package org.example.itester;  
  2.   
  3. import android.app.Activity;  
  4. import android.bluetooth.BluetoothAdapter;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.net.wifi.WifiInfo;  
  8. import android.net.wifi.WifiManager;  
  9. import android.os.Bundle;  
  10. import android.widget.TextView;  
  11.   
  12. public class wifibtcheck extends ITester{  
  13.     private static final int REQUEST_ENABLE_BT = 3;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.wifibtcheck);  
  19.   
  20.         mWifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);  
  21.   
  22.         if (!mWifi.isWifiEnabled())  
  23.         {  
  24.             mWifi.setWifiEnabled(true);  
  25.         }  
  26.           
  27.         WifiInfo wifiInfo = mWifi.getConnectionInfo();  
  28.           
  29.         bAdapt= BluetoothAdapter.getDefaultAdapter();  
  30.          
  31.         if (bAdapt != null)  
  32.         {  
  33.             if (!bAdapt.isEnabled())  
  34.             {  
  35.                 Intent enBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  36.                 startActivityForResult(enBT, REQUEST_ENABLE_BT);  
  37.             }  
  38.               
  39.             btMac = bAdapt.getAddress();  
  40.         }else{  
  41.             btMac = "No Bluetooth Device!";  
  42.         }  
  43.           
  44.         if((WifiMac = wifiInfo.getMacAddress())== null)  
  45.         {  
  46.             WifiMac = "No Wifi Device";  
  47.         }  
  48.         
  49.         TextView mac = (TextView)findViewById(R.id.macView);  
  50.         mac.setTextSize(38);  
  51.         mac.setText("   无线MAC:  "+ WifiMac + "\n   蓝牙MAC:  " + btMac);  
  52.   
  53.           
  54.     }  
  55.       
  56.     public void onActivityResult(int requestCode, int resultCode, Intent data) {  
  57.         switch (requestCode) {  
  58.   
  59.         case REQUEST_ENABLE_BT:  
  60.             // When the request to enable Bluetooth returns  
  61.             if (resultCode == Activity.RESULT_OK) {  
  62.                 // Bluetooth is now enabled, so set up a chat session  
  63.             } else {  
  64.                 finish();  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69. }  

0 0
原创粉丝点击