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.
I'm not sure bu maybe you have problem with the permission
Make sure you've also requested the necessary permissions in your app's manifest file. You should have something like:
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.