I would like to check if bluetooth is enabled on any Android device periodically. Is there any intents that I could catch using BroadcastReceiver to do so, or is there other ways to do it?
我想检查是否在任何Android设备上定期启用蓝牙。有没有使用BroadcastReceiver可以捕获的意图,还是有其他方法可以做到这一点?
6 个解决方案
#1
130
There you go:
你去:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
} else {
if (!mBluetoothAdapter.isEnabled()) {
// Bluetooth is not enable :)
}
}
With uses-permission
使用权限
<uses-permission android:name="android.permission.BLUETOOTH" android:required="false" />
#2
6
Here I have other alternative as an answer for this question.
在这里,我有其他选择作为这个问题的答案。
First add following lines in your Manifest file.
首先在清单文件中添加以下行。
<uses-feature android:name="android.hardware.BLUETOOTH" android:required="false"/>
Now, where you want to check Bluetooth supportability, use following code.
现在,您要检查蓝牙支持性,请使用以下代码。
boolean isBluetoothSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
#3
4
public boolean isBluetoothEnabled()
{
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return mBluetoothAdapter.isEnabled();
}
with the permission in manifest file:
使用清单文件中的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
#4
2
use can use
使用可以使用
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
for check bt connected
检查bt连接
mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED
for check bt disconnected
检查bt断开连接
mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_DISCONNECTED
#5
2
To check Bluetooth state, ON or OFF, programmatically:
要以编程方式检查蓝牙状态,ON或OFF:
BluetoothAdapter btAdapter = ((Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
?((BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter()
:(BluetoothAdapter.getDefaultAdapter()));
if(btAdapter==null){
return;
}
if(btAdapter.getState()==BluetoothAdapter.STATE_ON){
//Bluetooth is ON
}
You may also listen to Intent action:
您也可以收听Intent动作:
BluetoothAdapter.ACTION_STATE_CHANGED
BluetoothAdapter.ACTION_STATE_CHANGED
#6
1
This is how I did it with the help of @xjaphx's answer, slightly simplified version:
这是我在@ xjaphx的答案,稍微简化的版本的帮助下做到的:
private boolean getBlueToothOn(){
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
return btAdapter != null && btAdapter.isEnabled();
}
<uses-permission android:name="android.permission.BLUETOOTH" />
#1
130
There you go:
你去:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
} else {
if (!mBluetoothAdapter.isEnabled()) {
// Bluetooth is not enable :)
}
}
With uses-permission
使用权限
<uses-permission android:name="android.permission.BLUETOOTH" android:required="false" />
#2
6
Here I have other alternative as an answer for this question.
在这里,我有其他选择作为这个问题的答案。
First add following lines in your Manifest file.
首先在清单文件中添加以下行。
<uses-feature android:name="android.hardware.BLUETOOTH" android:required="false"/>
Now, where you want to check Bluetooth supportability, use following code.
现在,您要检查蓝牙支持性,请使用以下代码。
boolean isBluetoothSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
#3
4
public boolean isBluetoothEnabled()
{
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return mBluetoothAdapter.isEnabled();
}
with the permission in manifest file:
使用清单文件中的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
#4
2
use can use
使用可以使用
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
for check bt connected
检查bt连接
mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED
for check bt disconnected
检查bt断开连接
mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_DISCONNECTED
#5
2
To check Bluetooth state, ON or OFF, programmatically:
要以编程方式检查蓝牙状态,ON或OFF:
BluetoothAdapter btAdapter = ((Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
?((BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter()
:(BluetoothAdapter.getDefaultAdapter()));
if(btAdapter==null){
return;
}
if(btAdapter.getState()==BluetoothAdapter.STATE_ON){
//Bluetooth is ON
}
You may also listen to Intent action:
您也可以收听Intent动作:
BluetoothAdapter.ACTION_STATE_CHANGED
BluetoothAdapter.ACTION_STATE_CHANGED
#6
1
This is how I did it with the help of @xjaphx's answer, slightly simplified version:
这是我在@ xjaphx的答案,稍微简化的版本的帮助下做到的:
private boolean getBlueToothOn(){
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
return btAdapter != null && btAdapter.isEnabled();
}
<uses-permission android:name="android.permission.BLUETOOTH" />