使用蓝牙的时候需要申请的权限
1.获取蓝牙管理对象
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //如果不存在该设备不存在蓝牙设备则 返回null
2.判断蓝牙是否正在运行
boolena isEnable = bluetoothAdapter.isEnabled() //正在运行则返回true
3.获取蓝牙运行状态
int state = bluetoothAdapter.getState();
4.打开蓝牙设备
方法一:
boolean isOpen = bluetoothAdapter.enable();//打开蓝牙设备,返回值表示打开是否成功
方法二:
public void openBluetooth(){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);//调用系统的API
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100){
if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "请求失败", Toast.LENGTH_SHORT).show();
}else if(requestCode == RESULT_OK){
Toast.makeText(this, "请求成功", Toast.LENGTH_SHORT).show();
}
}
}
5.关闭蓝牙
boolean isclose = bluetoothAdapter.disable(); //返回值表示是否关闭成功