Android 获取连接WiFi和蓝牙名称

一、获取WiFi名称:

//获取连接的wifi名称
public String getConnectWifiSsid() {
	WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	WifiInfo wifiInfo = wifiManager.getConnectionInfo();
	//获取wifi信号强度
	rssi = wifiInfo.getRssi();
	String wifiid = wifiInfo.getSSID().replace("\"", "").replace("\"", "");
	MyLog.getInstance().PrintLog(TAG, "ssid======" + wifiid);
	MyLog.getInstance().PrintLog(TAG, "rssi======" + rssi);
    return wifiid;
}

根据信号强度设置wifi格数图片

if (rssi >= -55) {
	//满格信号
    image_wifi.setImageDrawable(ContextCompat.getDrawable(activity, R.mipmap.wifi4));
} else if (rssi < -55 && rssi >= -70) {
	//三格信号
 	image_wifi.setImageDrawable(ContextCompat.getDrawable(activity, R.mipmap.wifi3));
} else if (rssi < -70 && rssi >= -85) {
	//二格信号
	image_wifi.setImageDrawable(ContextCompat.getDrawable(activity, R.mipmap.wifi2));
} else if (rssi < -85&& rssi >= -100) {
	//一格信号
 	image_wifi.setImageDrawable(ContextCompat.getDrawable(activity, R.mipmap.wifi1));
}

二、获取蓝牙名称:

添加蓝牙权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
//获取连接的蓝牙名称
 public String getConnectedBtDevice() {
     //获取蓝牙适配器
     BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     //得到已匹配的蓝牙设备列表
     Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
     if (bondedDevices != null && bondedDevices.size() > 0) {
         for (BluetoothDevice bondedDevice : bondedDevices) {
			  try {
                  //使用反射调用被隐藏的方法
                  Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
                  isConnectedMethod.setAccessible(true);
                  boolean isConnected = (boolean) isConnectedMethod.invoke(bondedDevice, (Object[]) null);
                  MyLog.getInstance().PrintLog(TAG, "isConnected:" + isConnected);
			      if (isConnected) {
                        return bondedDevice.getName();
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();

                } catch (IllegalAccessException e) {
                    e.printStackTrace();

                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//判断蓝牙功能是否存在
if (mBluetoothAdapter == null) {
showToest("该设备不支持蓝牙");
return;
}
//获取名字 MAC地址
String name = mBluetoothAdapter.getName();
String address = mBluetoothAdapter.getAddress();
Log.i(TAG, "蓝牙名: "+name+"mac地址:"+address);
//获取蓝牙的状态
int state = mBluetoothAdapter.getState();
switch (state){
	case BluetoothAdapter.STATE_ON:
		showToest("蓝牙已经打开");
		Log.i(TAG, "蓝牙已经打开 ");
		break;
	case BluetoothAdapter.STATE_OFF:
		showToest("蓝牙已经关闭");
		Log.i(TAG, "蓝牙已经关闭 ");
		break;
	case BluetoothAdapter.STATE_TURNING_OFF:
		showToest("蓝牙正在关闭");
		Log.i(TAG, "蓝牙正在关闭 ");
		break;
	case BluetoothAdapter.STATE_TURNING_ON:
		showToest("蓝牙正在打开");
		Log.i(TAG, "蓝牙正在打开 ");
		break;
		default:
		break;
}

你可能感兴趣的:(android,java)