如何在单击按钮时打开蓝牙 [英] How to turn on bluetooth on button click
本文介绍了如何在单击按钮时打开蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我需要通过单击按钮打开设备的蓝牙.我怎样才能做到这一点?一个例子将非常有帮助.另外,我需要在我的 mainfest.xml 中包含哪些权限?
In my application, I need to turn on bluetooth of my device on a button click. How can I achieve that? An example will be really helpful. Also, what permissions I require to include in my mainfest.xml for the same?
推荐答案
以下代码摘自 关于蓝牙的安卓文档
在权限清单文件中:
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
启用蓝牙的源代码
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
如果启用蓝牙成功,您的Activity
将在onActivityResult()
回调中收到RESULT_OK
结果代码.如果蓝牙由于错误而未启用(或用户回答否"),则结果代码将为 RESULT_CANCELED
.
If enabling Bluetooth succeeds, your Activity
will receive the RESULT_OK
result code in the onActivityResult()
callback. If Bluetooth was not enabled due to an error (or the user responded "No") then the result code will be RESULT_CANCELED
.
这篇关于如何在单击按钮时打开蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文