1 .宣布权限
2 .获取本地蓝牙适配器
蓝牙adapterbluetoothadapter=蓝牙adapter.getdefaultadapter (;
3 .打开本地蓝牙
打开蓝牙有2中打开方式
1.1 (第一个(强行打开) ) ) ) ) ) ) ) )。
蓝牙适配器. enable (;
2 .第二个(调用系统Api打开) )
私有staticfinalintexqueat _ open=1;
intentopen=new intent (蓝牙适配器. action _ request _ enable;
startactivityforresult(open,EXQUEAT_OPEN );
@Override
protectedvoidonactivityresult (intrequestcode,int resultCode,Intent data ) {
super.onactivityresult (请求代码,结果代码,数据);
if (exqueat _ open==请求代码) {
if(resultcode==result_canceled ) {
toast.maketext(this,“无法打开蓝牙”,Toast.LENGTH_SHORT ).show );
} else {
toast.maketext(this,'打开蓝牙成功',Toast.LENGTH_SHORT ).show );
}
}
}
4 .关闭本地蓝牙
蓝牙适配器. disable (;
//打开在其他蓝牙设备中发现的功能
if (蓝牙适配器. getscanmode (!=蓝牙适配器. scan _ mode _ connectable _ discoverable ) {
intenti=new intent (蓝牙适配器. action _ request _ discoverable );
//一直打开
I.put extra (蓝牙适配器. extra _ discoverable _ duration,0 );
开始活动(I;
}
//获取名称mac地址
string name=蓝牙adapter.getname (;
string MAC=蓝牙adapter.get address (;
//获取当前蓝牙状态,
int state=蓝牙适配器. getstate (;
交换机(状态) {
casebluetoothadapter.state _ on :
toast.maketext(this,'蓝牙打开',Toast.LENGTH_SHORT ).show );
布雷克;
casebluetoothadapter.state _ turning _ on :
toast.maketext(this,'蓝牙已打开。 请参阅。toast.Length_short(.show );
布雷克;
casebluetoothadapter.state _ turning _ off :
toast.maketext(this,'蓝牙已关闭。 请参阅。toast.Length_short(.show );
布雷克;
casebluetoothadapter.state _ off :
toast.maketext(this,'蓝牙关闭',Toast.LENGTH_SHORT ).show );
布雷克;
}
(二)搜索和展示蓝牙设备) https://blog.csdn.net/Luming zang/article/details/8992235
(三)蓝牙设备连接与通信) https://blog.csdn.net/Luming zang/article/details/8992252