权限添加:
<!--蓝牙-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
建立一个类来管理
public class BluetoothManager {
/**
* 当前 Android 设备是否支持 Bluetooth
* @return true:支持 Bluetooth false:不支持 Bluetooth
*/
public static boolean isBluetoothSupported()
{
return BluetoothAdapter.getDefaultAdapter() != null ? true : false;
}
/**
* 当前 Android 设备的 bluetooth 是否已经开启
* @return true:Bluetooth 已经开启 false:Bluetooth 未开启
*/
public static boolean isBluetoothEnabled()
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null)
{
return bluetoothAdapter.isEnabled();
}
return false;
}
/**
* 强制开启当前 Android 设备的 Bluetooth
* @return true:强制打开 Bluetooth 成功 false:强制打开 Bluetooth 失败
*/
public static boolean turnOnBluetooth()
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null)
{
return bluetoothAdapter.enable();
}
Log.d("long","蓝牙开启失败");
return false;
}
public static boolean turnOffBluetooth(){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
return bluetoothAdapter.disable();
}
Log.d("long","蓝牙关闭失败");
return false;
}
}empty