I am writing an application with bluetooth functionalities. I am able to set the bluetooth adapter, and fetch my own device information, but I could not use startdiscovery to discover bluetooth devices. When I start a scan, it does nothing, and about broadcastReceiver it like doesn't running.
I'm using onClickListener for discovery:
discoverBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
discover();
}
});
private void discover() {
if (!checkPermission()) {
// Handle the case where permissions are not granted
Log.d("DISCOVER BTN", "Permissions not granted");
return;
}
if (mBluetoothAdapter.isDiscovering()) {
// If discovery is already in progress, cancel it first
mBluetoothAdapter.cancelDiscovery();
Log.d("DISCOVER BTN", "Discovery canceled");
}
// Start device discovery
Log.d("DISCOVER BTN", "Starting discovery");
mBluetoothAdapter.startDiscovery();
// Register the broadcast receiver for device discovery
IntentFilter discoverIntentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3, discoverIntentFilter);
}
The broadcaset object:
private final BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("ACTION", "ACTION_FOUND");
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 deviceInfo = device.getName() + "\n" + device.getAddress();
Log.d("BLUETOOTH DEVICE", deviceInfo);
arrayList.add(deviceInfo);
arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, arrayList);
devicesLV.setAdapter(arrayAdapter);
}
}
};
It should log this message:
Log.d("BLUETOOTH DEVICE", deviceInfo);
but this is the last log:
2024-01-30 16:41:22.780 19749-19749 BluetoothAdapter com.example.bluetoothapp2 I startDiscovery
I have set bluetooth permissions in the android manifest file, and check if all permissions are granted.
It is supposed to show the list of discovered devices, but it log just shows startDiscovery. Please tell me how to use startdiscovery and broadcastreceiver properly to display the result.
This is the bluetooth permissions in the Android manifest file:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
This is the checkPermission method:
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_PERMISSIONS);
return false;
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_CODE_PERMISSIONS);
return false;
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.BLUETOOTH_SCAN},
REQUEST_CODE_PERMISSIONS);
return false;
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.BLUETOOTH_CONNECT},
REQUEST_CODE_PERMISSIONS);
return false;
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_BACKGROUND_LOCATION},
REQUEST_CODE_PERMISSIONS);
return false;
}
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADVERTISE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_ADVERTISE},
REQUEST_CODE_PERMISSIONS);
return false;
}
return true;
}