判断蓝牙打开关闭状态+wifi打开关闭状态

Android 蓝牙编程的基本步骤:

  1.  获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter(); 

  2. 如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。

    判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。

  3. 启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  

    in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);  

    startActivity(in);  ,200就表示200秒。

  4. 获取蓝牙适配器中已经配对的设备Set<BluetoothDevice> device=blueadapter.getBondedDevices(); 

  5. 还需要在androidManifest.xml中声明蓝牙的权限

 <uses-permission android:name="android.permission.BLUETOOTH" />

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  

代码:

//判断蓝牙状态
BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
if (blueadapter.isEnabled()){

    text_wifi.setText("蓝牙状态:开");
}else{
    text_wifi.setText("蓝牙状态:关");

}
//判断wifi状态
//得到wifi管理器 ,,getApplicationContext:得到系统应用的Context,它的生命周期长,伴随整个App
WifiManager manager =(WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(manager.isWifiEnabled()){//判断wifi是否打开

    text_lanya.setText("wifi状态:开");
}else {

    text_lanya.setText("wifi状态:关");

}
wifi权限
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值