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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值