需要以编程方式打开蓝牙并在 Android 中发现设备 [英] Need to programmatically open bluetooth and discover devices in Android

查看:20
本文介绍了需要以编程方式打开蓝牙并在 Android 中发现设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过我的代码打开蓝牙而不要求用户启用它,然后发现范围内的所有设备并返回这些设备,然后关闭蓝牙连接.

I need to open up bluetooth through my code without asking the user to enable it and then discover all the devices in range and return those devices and then close bluetooth connection.

怎么做?

推荐答案

我想这正是您所需要的.

I suppose here is exactly what you need.

https://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices

https://developer.android.com/guide/topics/connectivity/bluetooth.html#DiscoveringDevices

关于在不询问用户的情况下启用蓝牙,文档如下:

About enabling the Bluetooth without asking the user, here is what the doc says:

未经用户直接同意,不得启用蓝牙.如果你想要打开蓝牙以便创建无线连接,您应该使用 ACTION_REQUEST_ENABLE意图,这将引发一个对话框请求用户许可打开蓝牙.enable() 方法是仅提供给应用程序包括用于更改的用户界面系统设置,例如电源经理"应用.

Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

但是如果您真的想在不询问用户的情况下启用设备蓝牙.添加此后:

But if you really want to Enable device bluetooth without asking the user. after add this:

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

允许您的清单在您的应用程序中使用此代码

permission to your Manifest use this code in your App

BluetoothAdapter.enable()

这篇关于需要以编程方式打开蓝牙并在 Android 中发现设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆