我想通过该程序启用/禁用蓝牙。我有以下代码。
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
但这段代码在 SDK 1.5 中不起作用。我怎样才能让它发挥作用?
这段代码对我有用..
//Disable bluetooth
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
为此,您必须具有以下权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
API 33+ 更新
在 Android API 33+ (13+) 中,此方法已被弃用,并且无法在未经用户同意的情况下启用/禁用蓝牙。根据文档 https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#enable(),你应该使用ACTION_REQUEST_ENABLE https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#ACTION_REQUEST_ENABLE反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)