需要示例android蓝牙权限代码

发布于 2025-01-16 17:07:32 字数 783 浏览 4 评论 0原文

我需要一个蓝牙权限的示例java代码。 Android 12。

我知道我必须将其放入 AndroidManifest.xml

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

根据我的研究,我认为我必须使用这些工具让我的手机向我询问蓝牙权限:

ActivityCompat
ContextCompat
PackageManager
onRequestPermissionsResult

但经过多个小时的测试后没有运气。 我什至尝试使用 GitHub 上的库为我请求“简单”权限,但没有成功:

https://github。 com/googlesamples/easypermissions https://github.com/guolindev/PermissionX

我尝试降低目标 SDK 版本,所以我不这样做不需要要求用户输入,但“AndroidX”给了我一个错误,告诉我它与较低的 SDK 不兼容。

如果您有一个基本的工作蓝牙应用程序 java 代码,这将对我有很大帮助,这样我就可以弄清楚如何编写我自己的应用程序。


I need an example java code of Bluetooth permissions. Android 12.

I know that I have to put this in the AndroidManifest.xml

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

From my research I think I have to use these tools to get my phone to ask me for Bluetooth permissions:

ActivityCompat
ContextCompat
PackageManager
onRequestPermissionsResult

But no luck after many hours of tests.
I even tried using libraries from GitHub to ask for "easy" permissions for me but no luck:

https://github.com/googlesamples/easypermissions
https://github.com/guolindev/PermissionX

I tried lowering my target SDK version so I don't need to ask for the user entry but "AndroidX" gave me an error telling that its not compatible with lower SDK's.

It would help me a ton if you have a basic working Bluetooth app java code so I can figure out how to code my own app.


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2025-01-23 17:07:32

对于Android 12上新的蓝牙权限,您需要这些权限:

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

您可以参考此博客文章了解更多详细信息。

For the new Bluetooth permissions on Android 12, you need these permissions:

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

You can refer to this blog post for more details.

凤舞天涯 2025-01-23 17:07:32

对于 Android 12 上的蓝牙权限,您需要这些权限,具体取决于您的使用情况:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

在您的 Activity 中,您可以添加此权限来检查是否允许:

private static final int ACCESS_COARSE_LOCATION_RESULT_CODE = 4;
    private static final int BLUETOOTH_RESULT_CODE = 5;
    private static final int DANGEROUS_RESULT_CODE = 1;

if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        ACCESS_COARSE_LOCATION_RESULT_CODE);
            }
            else if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.BLUETOOTH},
                        BLUETOOTH_RESULT_CODE);
            }
            else if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.BLUETOOTH_SCAN},
                            DANGEROUS_RESULT_CODE);
                }
            }
            else if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.BLUETOOTH_CONNECT},
                            DANGEROUS_RESULT_CODE);
                }
            }

要了解更多信息,您可以检查 文档

For bluetooth permission on Android 12, you need these permission, depend on your usage:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

In your activity, you can add this to check the permission is allowed or not:

private static final int ACCESS_COARSE_LOCATION_RESULT_CODE = 4;
    private static final int BLUETOOTH_RESULT_CODE = 5;
    private static final int DANGEROUS_RESULT_CODE = 1;

if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        ACCESS_COARSE_LOCATION_RESULT_CODE);
            }
            else if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.BLUETOOTH},
                        BLUETOOTH_RESULT_CODE);
            }
            else if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.BLUETOOTH_SCAN},
                            DANGEROUS_RESULT_CODE);
                }
            }
            else if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.BLUETOOTH_CONNECT},
                            DANGEROUS_RESULT_CODE);
                }
            }

To understand more, you can check the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文