隐式打开方式

关键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;

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐