
Creator
By debbuging after @RequiresBluetoothLocationPermission annotation program will exit function startScan in BluetoothLeScanner object
Could you please help me to find the problem why startScan function is not executed. By debugging after @RequiresBluetoothLocationPermission program will not go the next line.
My phone is Samsung Galaxy S22 Ultra Anfroid14, Compile Target SDK 34
Location, Connect, Scan - Permissions will be grandet at Run time This is a function that should be executed with provided ScanCallback
@RequiresLegacyBluetoothAdminPermission
@RequiresBluetoothScanPermission
**@RequiresBluetoothLocationPermission**
@RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN)
public void startScan(final ScanCallback callback) {
startScan(null, new ScanSettings.Builder().build(), callback);
}
My program Code:
package com.example.navigationleftexample.ui.gallery;
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;
import androidx.lifecycle.ViewModelProvider;
import android.os.*;
import com.example.navigationleftexample.MainActivity;
import com.example.navigationleftexample.R;
import com.example.navigationleftexample.databinding.FragmentBluetoothBinding;
import com.example.navigationleftexample.ui.Bluetooth.LeDeviceListAdapter;
import pub.devrel.easypermissions.EasyPermissions;
public class BluetoothFragment extends Fragment {
Switch bluetoothSwitch;
private FragmentBluetoothBinding binding;
private BluetoothAdapter mBluetoothAdapter;
private LayoutInflater layoutInflater;
private BluetoothLeScanner bluetoothLeScanner;
private boolean scanning;
private Handler handler = new Handler();
public static final String[] BLUETOOTH_PERMISSIONS_S =
{ Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT} ;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 456;
private static final int REQUEST_CODE_BLUETOOTH_SCAN = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
//LeDeviceListAdapter leDeviceListAdapter = new LeDeviceListAdapter(layoutInflater);
LeDeviceListAdapter leDeviceListAdapter;
private boolean checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
int permission = ContextCompat.checkSelfPermission
(getContext(), Manifest.permission.ACCESS_FINE_LOCATION);
return permission == PackageManager.PERMISSION_GRANTED;
}else{
return true;
}
}
// Device scan callback.
private ScanCallback leScanCallback =
new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
leDeviceListAdapter.addDevice(result.getDevice());
leDeviceListAdapter.notifyDataSetChanged();
}
};
private void scanLeDevice() {
if (!scanning) {
// Stops scanning after a predefined scan period.
handler.postDelayed(new Runnable() {
@Override
public void run() {
scanning = false;
if (ActivityCompat.checkSelfPermission(getContext(),
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;
}
bluetoothLeScanner.stopScan(leScanCallback);
}
}, SCAN_PERIOD);
scanning = true;
bluetoothLeScanner.startScan(leScanCallback);
} else {
scanning = false;
//bluetoothLeScanner.stopScan(leScanCallback);
scanLeDevice();
}
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
BluetoothViewModel bluetoothViewModel =
new ViewModelProvider(this).get(BluetoothViewModel.class);
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getContext().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(getContext(), R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
getActivity().finish();
}
bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
layoutInflater = inflater;
binding = FragmentBluetoothBinding.inflate(inflater, container, false);
View root = binding.getRoot();
// Check Bluetooth Switch
bluetoothSwitch = (Switch) binding.switchBluetoothOn;
bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(root.getContext(), "Bluetooth was changed", Toast.LENGTH_SHORT).show();
if (isChecked==true)
{
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getActivity().getPackageManager().
hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(getContext(),
R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
getActivity().finish();
}
// Check permissions for Bluetooth Connect
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.BLUETOOTH_CONNECT)
== PackageManager.PERMISSION_DENIED)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
{
ActivityCompat.requestPermissions(getActivity(),
new String[] {Manifest.permission.BLUETOOTH_CONNECT}, 2);
return;
}
}
// Request Scan permission
ActivityCompat.requestPermissions(getActivity(),
new String[]
{Manifest.permission.BLUETOOTH_SCAN}, REQUEST_CODE_BLUETOOTH_SCAN);
boolean fineLocation = checkPermission();
leDeviceListAdapter = new LeDeviceListAdapter(layoutInflater);
scanLeDevice();
}
else
{
leDeviceListAdapter.clear();
//scanLeDevice(false);
}
}
});
// Show Connected Bluetooth Device
final TextView textView = binding.textViewBluetoothDeviceChosen;
bluetoothViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
// Show List of Bluetooth scanned devices
final ListView listView = (ListView) binding.listBluetoothDeviceView;
//String [] deviceArray = bluetoothViewModel.getDeviceArray();
listView.setAdapter(leDeviceListAdapter);
// Create Listener for the List
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Toast.makeText(root.getContext(), "You have selected item: " + item,
Toast.LENGTH_SHORT).show();
}
});
return root;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
ActivityResultLauncher<String[]> locationPermissionRequest =
registerForActivityResult(new ActivityResultContracts
.RequestMultiplePermissions(), result -> {
Boolean fineLocationGranted = result.getOrDefault(
Manifest.permission.ACCESS_FINE_LOCATION, false);
Boolean coarseLocationGranted = result.getOrDefault(
Manifest.permission.ACCESS_COARSE_LOCATION,false);
if (fineLocationGranted != null && fineLocationGranted) {
// Precise location access granted.
Toast.makeText(getContext(), "Precise location granted" ,
Toast.LENGTH_SHORT).show();
} else if (coarseLocationGranted != null && coarseLocationGranted) {
// Only approximate location access granted.
Toast.makeText(getContext(), "Aproximate location granted" ,
Toast.LENGTH_SHORT).show();
} else {
// No location access granted.
Toast.makeText(getContext(), "No location granted" ,
Toast.LENGTH_SHORT).show();
}
}
);
locationPermissionRequest.launch(new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
Permissions from Android manifest:
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BILLING" />
Most Liked

Creator
Code is working fine. I have not initialized leDeviceListAdapter befor i set it to ListView
1
Popular Android topics

I am making an app with Nativescript-vue and I would like to have an event where I click on an icon and then a new box appears. For the i...
New

Hi, I’m working on an app who make the conversion between decimal degrees and degrees minutes seconds. I have to make an “Export” button ...
New

Question: Android Developer
→ is there any way to read the .trash directory?
→ we can copy or save the file before delete ?
#android...
New

Android studio displayed access is denied during gradle build
C:\Program Files\Java\jdk1.8.0_251\native\68d5fa5c4cc2d200863cafc0d521ce42...
New

Good afternoon. Brief story: My institution has several nfc tags in building which have to be scanned with specific app built and used in...
New

Hi
Hope it is a simple question :slight_smile:
I am planning to use a new PC for making my enterprise app.
Everything works perfect, b...
New

Hello everyone, I recently started learning Kotlin and downloaded Android Studio, but after writing my first code and trying to run it, I...
New

I’m stuck trying to understand how to get a variable initialised, and later updated, in kotlin to update in Jetpack Compose and cause a c...
New

I’m using TensorFlow Lite (TFLite) with React Native Expo. When I test the app using the Expo Go app, everything works fine. However, whe...
New

I need to create an android app to receive contactless payment from my clients. how can I start that? My clients will be phisicaly in the...
New
Other popular topics

poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New

On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New

In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first:
asdf plugin-upd...
New

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New

Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New

Saw this on TikTok of all places! :lol:
Anyone heard of them before?
Lite:
New

This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New

Author Spotlight
James Stanier
@jstanier
James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New

Large Language Models like ChatGPT say The Darnedest Things.
The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig.
General-purpose programming language and toolchain for maintaini...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /textmate
- /sublime-text
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /lua
- /nodejs
- /julia
- /c
- /slackware
- /markdown