简体   繁体   English

需要以编程方式打开蓝牙并在Android中发现设备

[英]Need to programmatically open bluetooth and discover devices in 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. 我需要通过我的代码打开蓝牙而不要求用户启用它,然后发现范围内的所有设备并返回这些设备,然后关闭蓝牙连接。

How to do it? 怎么做?

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#FindingDevices

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

About enabling the Bluetooth without asking the user, here is what the doc says: 关于在不询问用户的情况下启用蓝牙,以下是该文档所说的内容:

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. 如果要打开蓝牙以创建无线连接,则应使用ACTION_REQUEST_ENABLE Intent,这将引发一个请求用户打开蓝牙权限的对话框。 The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app. enable()方法仅适用于包含用于更改系统设置的用户界面的应用程序,例如“power manager”应用程序。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM