android点击按钮打开蓝牙,Android打开蓝牙的两种方式
隐式打开方式关键API使用如下:if ( !bluetoothAdapter.isEnabled()) {boolean res = bluetoothAdapter.enable();}完整判断逻辑如下:bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter == null) {Log.e(TAG,
隐式打开方式
关键API使用如下:
if ( !bluetoothAdapter.isEnabled()) {
boolean res = bluetoothAdapter.enable();
}
完整判断逻辑如下:
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Log.e(TAG, "--------------- 不支持蓝牙");
return;
}
if ( !bluetoothAdapter.isEnabled()) {
boolean res = bluetoothAdapter.enable();
if (res == true) {
myBtResultCallback.showToastMsg("蓝牙打开成功");
} else {
myBtResultCallback.showToastMsg("蓝牙打开失败");
}
} else if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
myBtResultCallback.showToastMsg("蓝牙已打开");
} else {
myBtResultCallback.showToastMsg("蓝牙打开失败");
}
显示打开方式
关键代码如下:
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, requestCode);
可以在Activity中的onActivityResult()方法中处理结果, 如果蓝牙模块打开成功, 则返回结果吗RESULT_OK; 如果蓝牙模块打开失败, 则返回结果码RESULT_CANCELED;
更多推荐
所有评论(0)