Broadcast receiver for discovering available Bluetooth devices

35 Views Asked by At

I was trying to discover available Bluetooth device on my app but it doesn't work, I think the problem is from my broadcast receiver. Could someone please tell me what's wrong in this broadcast receiver:

package com.example.wheelchairapp;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class AvailableBroadcaseReciever extends BroadcastReceiver {
    RecyclerView.Adapter adapter2;

    public AvailableBroadcaseReciever(RecyclerView.Adapter adapter2) {
        this.adapter2 = adapter2;
    }

    @Override
    public void onReceive(Context context, @NonNull Intent intent) {
        if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction())){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            DevicesList.availableDevices.add(device.getName());
            adapter2.notifyDataSetChanged();
        }
    }
}

I tried to exactly follow YouTube developers but the problem wasn't solved.

2

There are 2 best solutions below

1
user23464952 On

I'm not sure bu maybe you have problem with the permission

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.RecyclerView;

public class AvailableBroadcaseReciever extends BroadcastReceiver {
    RecyclerView.Adapter adapter2;

    public AvailableBroadcaseReciever(RecyclerView.Adapter adapter2) {
        this.adapter2 = adapter2;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Check for BLUETOOTH permission
            if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
                // Request the missing permission
                // Do something here like requesting the permission or notifying the user
                // This is just an example, you may want to handle this differently based on your app's requirements
                Toast.makeText(context, "Bluetooth permission not granted", Toast.LENGTH_SHORT).show();
                return;
            }
            // You may also want to check for null device.getName() before adding it to the list
            if(device != null && device.getName() != null) {
                DevicesList.availableDevices.add(device.getName());
                adapter2.notifyDataSetChanged();
            }
        }
    }
}

Make sure you've also requested the necessary permissions in your app's manifest file. You should have something like:

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

These permissions are necessary for discovering and interacting with Bluetooth devices. Make sure to request these permissions at runtime as well, if your app targets Android 6.0 (API level 23) or higher.

1
Shady Abdulmunim On

What was wrong is that I didn't use: Import android.Manifest, that is why I was getting the error "cannot resolve symbol" when I try to add the permission