简体   繁体   中英

Android Bluetooth function permission problem

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.

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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