2017-03-1<Bluetooth基本操作>

public class Bluetoothcontroller {
private BluetoothAdapter ba;

public Bluetoothcontroller() {
this.ba = BluetoothAdapter.getDefaultAdapter();
}
/*
* 判断设备是否支持蓝牙
* true支持
* false不支持
* */
public boolean isBluetooth(){
if(ba!=null){
return true;
}else {
return false;
}
}
/*
* 判断蓝牙状态
* 条件一:ba!=null 蓝牙设备不为空
* 条件二:ba.isEnabled() 判断蓝牙是否开启
* 满足条件一执行条件二 不满足条件一 执行false
* 如果不判断蓝牙设备是否存在 直接判断蓝牙状态 在没有蓝牙设备的情况下 将会空指针
* */
public boolean BluetoothStatu(){
return ba!=null ? ba.isEnabled():false;
}
/*
* 打开蓝牙
* */
public void OpenBluetooth(Activity activity,int requestcode){
//获取蓝牙;唤起界面
Intent i=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(i,requestcode);
}
/*
* 关闭蓝牙
* */
public void OffBluetooth(){
ba.disable();
}
}
原文地址:https://www.cnblogs.com/livelihood/p/6557944.html