微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在按钮点击时打开蓝牙

在我的应用程序中,我需要在按钮单击时打开我的设备的蓝牙.我怎样才能做到这一点?一个例子将非常有用.另外,我需要在mainfest.xml中包含哪些权限?

解决方法

以下是 android documentation on Bluetooth代码摘录

在权限清单文件中:

<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);
}

如果启用蓝牙成功,您的活动将在onActivityResult()回调中收到RESULT_OK结果代码.如果由于错误而未启用蓝牙(或用户回答“否”),则结果代码将为RESULT_CANCELED.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐