Android蓝牙功能权限问题

发布于 2025-01-27 09:50:18 字数 3916 浏览 3 评论 0原文

我试图建立蓝牙连接Android应用。有一个按钮单击“蓝牙搜索BTN”事件处理程序。我想实现蓝牙搜索功能。 但是,许可有一个问题,并向我展示了代码下的红线。

问题发生了:
1。 bluetoothsearchbtn.setonClickListener(new View.OnClickListener()){}
2。私有最终broadcastreceiver reveriver = new broadcastreceiver(){}

  • 变量声明
    Set<BluetoothDevice> pairedDevices;
    ArrayAdapter<String> btArrayAdapter;
    ArrayList<String> deviceAddressArray;
    BluetoothAdapter btAdapter;

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    btArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    deviceAddressArray = new ArrayList<>();

这是我的代码,显示了红线。

  1. bluetoothsearchbtn.setOnClickListener(new View.OnClickListener()
BluetoothSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Check if the device is already discovering
                if(btAdapter.isDiscovering()){
                    btAdapter.cancelDiscovery();
                } else {
                    if (btAdapter.isEnabled()) {
                        btAdapter.startDiscovery();
                        btArrayAdapter.clear();
                        if (deviceAddressArray != null && !deviceAddressArray.isEmpty()) {
                            deviceAddressArray.clear();
                        }
                        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                        registerReceiver(receiver, filter);
                    } else {
                        Toast.makeText(getApplicationContext(), "bluetooth not on", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

红色下划线位置:btadpater.isdiscovering()btadapter.canceldster.canceldiscovering(cancanceldiscovering() btadatper.startdiscovering() 问题: 呼叫需要用户可能拒绝的权限:代码应明确检查以查看是否可用(使用'Checkperssmerion)或明确处理潜在的“ SecurityException” 。。

  1. 私有最终broadcastreceiver接收器=新的broadcastreceiver()
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                btArrayAdapter.add(deviceName);
                deviceAddressArray.add(deviceHardwareAddress);
                btArrayAdapter.notifyDataSetChanged();
            }
        }
    };

红色下划线位置:device.getName() 问题: 呼叫需要用户可能拒绝的权限:代码应明确检查以查看是否可用(使用'Checkperssmerion)或明确处理潜在的“ SecurityException” 。。

我已经在androidmanifest.xml上添加了权限

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

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

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

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

,但我仍然找不到权限出了什么问题... 对不起,我的英语不好..但是我想知道这个问题发生以及如何解决的原因。

I'am trying to make bluetooth connection android App. There is a button click 'BluetoothSearchBtn' event handler. And I wanted to implement bluetooth searching function.
But there is a problem with permission, and shows me redlines under the codes.

The problems occured methods:
1. BluetoothSearchBtn.setOnClickListener(new View.OnClickListener()){}
2. private final BroadcastReceiver receiver = new BroadcastReceiver(){}

  • variable declaration
    Set<BluetoothDevice> pairedDevices;
    ArrayAdapter<String> btArrayAdapter;
    ArrayList<String> deviceAddressArray;
    BluetoothAdapter btAdapter;

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    btArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    deviceAddressArray = new ArrayList<>();

This is my code which shows the redlines.

  1. BluetoothSearchBtn.setOnClickListener(new View.OnClickListener()
BluetoothSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Check if the device is already discovering
                if(btAdapter.isDiscovering()){
                    btAdapter.cancelDiscovery();
                } else {
                    if (btAdapter.isEnabled()) {
                        btAdapter.startDiscovery();
                        btArrayAdapter.clear();
                        if (deviceAddressArray != null && !deviceAddressArray.isEmpty()) {
                            deviceAddressArray.clear();
                        }
                        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                        registerReceiver(receiver, filter);
                    } else {
                        Toast.makeText(getApplicationContext(), "bluetooth not on", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

Red underline locations : btAdpater.isDiscovering(), btAdapter.cancelDiscovering(),
btAdatper.startDiscovering()
Problem : Call requires permission which may be rejected by user:code should explicitly check to see if permission is available (with 'checkPersmission) or explicitly handle a potential 'SecurityException'.

  1. private final BroadcastReceiver receiver = new BroadcastReceiver()
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                btArrayAdapter.add(deviceName);
                deviceAddressArray.add(deviceHardwareAddress);
                btArrayAdapter.notifyDataSetChanged();
            }
        }
    };

Red underline locations : device.getName()
Problem : Call requires permission which may be rejected by user:code should explicitly check to see if permission is available (with 'checkPersmission) or explicitly handle a potential 'SecurityException'.

I have already added permissions on AndroidManifest.xml

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

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

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

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

But I can't still find out what is wrong with permissions...
I'm sorry my english bad.. but I want to know the reason why this problem occurs and how to solve.

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

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

发布评论

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

评论(1

蘸点软妹酱 2025-02-03 09:50:18

首先,您应该检查是否需要许可,只需单击此按钮添加权限检查:

“在此处输入图像说明”

或按Alt+Enter,然后Android Studio将为您生成所需的代码

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

First of all, you should check whether you have required permission or not, just add permission check by clicking on this button:

enter image description here

or press alt+enter and then Android Studio will generate the needed code for you

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文