Unable to discover Bluetooth Devices on Android - android

I am coding an app to discover nearby Bluetooth devices. However, I am unable to pick up any devices near me. I have scoured the internet and debugged and found out that after calling the registerReceiver(mBroadcastReceiver3, discoverDevicesIntent) method, nothing happened. My BroadcastReceiver's onReceive is not being passed. I have already requested the required permissions in the manifest and on runtime aswell and even checked if the required permissions are granted (which they are) but to no avail. I am unsure why my BroadcastReceiver is not receiving. Below is my code.
Broadcast Receiver:
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.d(TAG, "mBroadCastReceiver3 Received.");
if (action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());
mDeviceListAdapter = new DeviceListAdapter(context, R.layout.device_adapter_view, mBTDevices);
lvNewDevices.setAdapter(mDeviceListAdapter);
}
}
OnClick Function:
public void btnDiscover(View view) {
Log.d(TAG, "btnDiscover: Looking for unpaired devices.");
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
Log.d(TAG, "btnDiscover: Canceling discovery.");
}
checkBTPermissions();
mBluetoothAdapter.startDiscovery();
Log.d(TAG, "mBluetoothAdapter: started discovery.");
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
Log.d(TAG, "discoverDevicesIntent: IntentFilter established with value of: " + discoverDevicesIntent);
registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
Log.d(TAG, "mBroadcastReceiver3: receiver registered with discoverDevicesIntent.");
}
Checking of Permissions:
private void checkBTPermissions() {
Log.d(TAG, "checkBTPermissions: checking BT Permissions.");
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
int permissionCheck = 0;
permissionCheck += this.checkSelfPermission("Manifest.permission.BLUETOOTH");
permissionCheck += this.checkSelfPermission("Manifest.permission.BLUETOOTH_ADMIN");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
if (permissionCheck != 0) {
Log.d(TAG, "permissionCheck: !=0 .");
this.requestPermissions(new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
}
}else{
Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP.");
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_CODE) {
if (grantResults.length > 0) {
int grantresultslength = grantResults.length;
Log.v(TAG, "" + grantresultslength);
for(int i=0; i<grantresultslength; i++){
if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"PERMISSION" + i + " GRANTED");
}else{
Log.v(TAG,"PERMISSION" + i + " DENIED");
}
}
} else {
Toast.makeText(this, "PERMISSION DENIED", Toast.LENGTH_SHORT).show();
}
}
}

Related

No devices found upon starting bluetooth scan

I'm currently working on an android app to control Bluetooth devices but no available devices show up even though I can find them from the built-in settings>bluetooth menu.
I initially followed the android starter sample out of the box, but couldn't make it work. I then came across several similar posts and tried those as well, but still couldn't make it work. This is my code so far: ```
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_LOCATION_BT = 3;
private static final int REQUEST_ENABLE_BT = 2;
private BluetoothAdapter bluetoothAdapter;
private ArrayList<String> deviceList;
private ActivityMainBinding binding;
private ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
requiredSetup();
setupListView();
}
private void setupListView() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
deviceList = new ArrayList<>();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
deviceList.add(device.getName());
//String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
listAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, deviceList);
binding.listView.setAdapter(listAdapter);
}
private void requiredSetup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_BACKGROUND_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
REQUEST_LOCATION_BT);
}
}
// checking if device supports bluetooth
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
Toast.makeText(this, "Device doesnt support bluetooth", Toast.LENGTH_SHORT).show();
return;
}
// enabling bluetooth if disabled
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
Toast.makeText(this, "Bluetooth enabled", Toast.LENGTH_SHORT).show();
} else if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_CANCELED) {
Toast.makeText(this, "App requires bluetooth to function", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
if (requestCode == REQUEST_LOCATION_BT) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, yay! Start the Bluetooth device scan.
startScan();
} else {
// Alert the user that this application requires the location permission to perform the scan.
Toast.makeText(this, "Requires location permission to work", Toast.LENGTH_SHORT).show();
}
}
}
/**
* initializes device discovery. The list of devices are sent to the broadcast receiver
* The discovery process usually involves an inquiry scan of about 12 seconds,
* followed by a page scan of each device found to retrieve its Bluetooth name.
*/
private void startScan() {
// If we're already discovering, stop it
if (bluetoothAdapter.isDiscovering()) {
Toast.makeText(this, "stopping discovery", Toast.LENGTH_SHORT).show();
bluetoothAdapter.cancelDiscovery();
} else {
try {
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Request discover from BluetoothAdapter
bluetoothAdapter.startDiscovery();
binding.progressBar.setVisibility(View.VISIBLE);
} catch (IllegalArgumentException e) {
e.printStackTrace();
Toast.makeText(this, "receivers not registered", Toast.LENGTH_SHORT).show();
}
}
}
/**
* #param view the button view that is pressed and the scanning begins
*/
public void searchDevices(View view) {
// checking if location permissions enabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_BT);
} else {
startScan();
}
}
/**
* The BroadcastReceiver that listens for discovered devices and changes the title when
* discovery is finished
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "reeiver working", Toast.LENGTH_SHORT).show();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device != null && device.getBondState() != BluetoothDevice.BOND_BONDED) {
listAdapter.add(device.getName() + "\n" + device.getAddress());
Log.i("NEW DEVICE", device.getName());
listAdapter.notifyDataSetChanged();
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//setProgressBarIndeterminateVisibility(false);
//setTitle(R.string.select_device);
binding.progressBar.setVisibility(View.INVISIBLE);
if (listAdapter.getCount() == 0) {
Toast.makeText(context, "no devices found", Toast.LENGTH_SHORT).show();
}
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
Toast.makeText(context, "Bluetooth state changed", Toast.LENGTH_SHORT).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(context, "Discovery started", Toast.LENGTH_SHORT).show();
}
}
};
#Override
protected void onDestroy() {
super.onDestroy();
// Make sure we're not doing discovery anymore
if (bluetoothAdapter != null) {
bluetoothAdapter.cancelDiscovery();
}
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
}}
This is my manifest permissions:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
This is my repository. It would be very helpful if anyone could shed some light here.
It seems like the code was just fine. The problem was with the target SDK version. Upon changing the version from 30 to 28, it worked. Thanks to MatejC's answer.

Read/Write permission are not triggering in android

Hi in the below code When I am trying click it is showing you don't have read and access permission.But in the manifest file I has given those two permission.Both read and write permission are not triggering with my below code.Both the permission I am calling haspermission evry time failing
Can any one help me where I did the mistake.
java:
private static final String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
String modality_name = opportunity.getModality();
if (modality_name.equals("LCS")||modality_name.equals("Ultrasound")||modality_name.equals("DI")) {
// Toast.makeText(mContext, modality_name, Toast.LENGTH_LONG).show();
holder.pdf.setVisibility(View.GONE);
}else {
holder.pdf.setVisibility(View.VISIBLE);
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!hasPermissions(mContext, PERMISSIONS)) {
Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
Toast t = Toast.makeText(mContext, "You don't have read access !", Toast.LENGTH_LONG);
t.show();
} else {
File d = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); // -> filename = maven.pdf
String url_path = opportunity.getPdf_link();
///Log.d("url_path", url_path);
String file_name = url_path.substring(url_path.lastIndexOf('/') + 1);
//Log.d("file_name", file_name);
File pdfFile = new File(d, file_name);
Log.v(TAG, "view() Method pdfFile " + pdfFile.getAbsolutePath());
if(pdfFile!=null) {
Uri path = GenericFileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", pdfFile);
Log.v(TAG, "view() Method path " + url_path);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
mContext.startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(mContext, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
// }
}
Log.v(TAG, "view() Method completed ");
// download(v);
//request(v);
}
download(v);
// request(v);
}
public void request(View v) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, 112);
}
public void download(View v) {
Log.v(TAG, "download() Method invoked ");
if (!hasPermissions(mContext, PERMISSIONS)) {
Log.v(TAG, "download() Method DON'T HAVE PERMISSIONS ");
Toast t = Toast.makeText(mContext, "You don't have write access !", Toast.LENGTH_LONG);
t.show();
} else {
Log.v(TAG, "download() Method HAVE PERMISSIONS ");
String url_path = opportunity.getPdf_link();
Log.d("url_path", url_path);
String file_name = url_path.substring(url_path.lastIndexOf('/') + 1);
Log.d("file_name", file_name);
new DownloadFile().execute(url_path, file_name);
}
Log.v(TAG, "download() Method completed ");
}
});
}
}
// public DragListener getDragInstance() {
// if (mlistener != null) {
// return new DragListener(mlistener);
// } else {
// Log.e("Route Adapter: ", "Initialize listener first!");
// return null;
// }
// }
private class DownloadFile extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... strings) {
Log.v(TAG, "doInBackground() Method invoked ");
String fileUrl = strings[0];
Log.d("fileurl",fileUrl);
String fileName = strings[1]; // -> maven.pdf
Log.d("fileName",fileName);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File pdfFile = new File(folder, fileName);
Log.v(TAG, "doInBackground() pdfFile invoked " + pdfFile.getAbsolutePath());
Log.v(TAG, "doInBackground() pdfFile invoked " + pdfFile.getAbsoluteFile());
try {
pdfFile.createNewFile();
Log.v(TAG, "doInBackground() file created" + pdfFile);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "doInBackground() error" + e.getMessage());
Log.e(TAG, "doInBackground() error" + e.getStackTrace());
}
FileDownloader.downloadFile(fileUrl, pdfFile);
Log.v(TAG, "doInBackground() file download completed");
return null;
}
}
First of all, WRITE permission grants you both READ/WRITE access, so you don't need to send a request for both
Check if permission is granted by this method, no need for all of those API checks:
private boolean isPermissionGranted(String permission) {
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
If permission is not granted first before everything else just request the permission:
requestPermissions(new String[Manifest.permission.WRITE_EXTERNAL_STORAGE], 123);
Check if permission is granted and then continue your work:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == 123 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do your work
}
else {
//Permission is not granted do what you want
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
In your code:
inside holder.pdf.setOnClickListener()'s onCLick()
if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
requestPermissions(new String[Manifest.permission.WRITE_EXTERNAL_STORAGE], 123);
} else {
//Do your job
}

Bluetooth startDiscovery() is not working on Android 10

I´ve got stuck on a problem. I´ve tried some hints from Stack Overflow and other websites.
I would like to program an app, which searches for all Bluetooth devices around and if the matching device has been found (MAC-Address as reference), the app will start a connection.
Therefore I have programmed a test application to test the discovery-function. BUT there is unfortunately a big problem with starting the discovering process on my Android 10 device. I´ve got an older Samsung S3 Mini with Android 4.1.2 (SDK 16) on it, where my code is working fine.
On the Android 10 device startDiscovery() returns false, different to the Android 4 device, which returns true. On the android developer page they say, that false is the returning value, if an error has occurred. The BroadcastReceiver should work fine, because the app on the Android 9 mobile phone detects that a Bluetooth search has been started in the settings. It is only the startDiscovery() function, which the problem is all about (in my opinion).
I am checking all permissions and the Bluetooth state before starting the discovering-process. But i think, it can´t be the written code, because it works perfectly on the older device. Maybe I have something missing for newer devices.
Edit
As Thomas Morris explains below, in Android 10 you need the location turned on by the user. In Android 9 or lower, Thomas Morris answer is correct, because in all SDKs below 29, only the permission is needed and not the enabled location service.
Is there a solution to avoid asking the user to turn on the location themselves?
This is my MainActivity:
public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity";
BluetoothAdapter bluetoothAdapter;
int status = 0; //0 = start discovering, 1 = cancel discovering
public static final int REQUEST_ACCESS_COARSE_LOCATION = 1;
public static final int REQUEST_ENABLE_BLUETOOTH = 11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
checkBluetoothState();
final Button test = findViewById(R.id.testbutton);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(status == 0) {
if(bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
if (checkCoarseLocationPermission()) {
Boolean result = bluetoothAdapter.startDiscovery(); //start discovering and show result of function
Toast.makeText(getApplicationContext(), "Start discovery result: " + result, Toast.LENGTH_SHORT).show();
Log.d(TAG, "Start discovery: " + result);
test.setText("Stop");
status = 1;
}
}else{
checkBluetoothState();
}
}else{
Log.d(TAG,"Stop");
status = 0;
bluetoothAdapter.cancelDiscovery();
test.setText("Start");
}
}
});
checkCoarseLocationPermission();
}
private boolean checkCoarseLocationPermission() {
//checks all needed permissions
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_ACCESS_COARSE_LOCATION);
return false;
}else{
return true;
}
}
private void checkBluetoothState() {
//checks if bluetooth is available and if it´s enabled or not
if(bluetoothAdapter == null){
Toast.makeText(getApplicationContext(), "Bluetooth not available", Toast.LENGTH_SHORT).show();
}else{
if(bluetoothAdapter.isEnabled()){
if(bluetoothAdapter.isDiscovering()){
Toast.makeText(getApplicationContext(), "Device is discovering...", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Bluetooth is enabled", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getApplicationContext(), "You need to enabled bluetooth", Toast.LENGTH_SHORT).show();
Intent enabledIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabledIntent, REQUEST_ENABLE_BLUETOOTH);
}
}
}
// Create a BroadcastReceiver for ACTION_FOUND.
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
Log.d(TAG,"Device found: " + deviceName + "|" + deviceHardwareAddress);
Toast.makeText(getApplicationContext(), "FOUND: " + deviceName + "|" + deviceHardwareAddress, Toast.LENGTH_SHORT).show();
}
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
//report user
Log.d(TAG,"Started");
Toast.makeText(getApplicationContext(), "STARTED", Toast.LENGTH_SHORT).show();
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//change button back to "Start"
status = 0;
final Button test = findViewById(R.id.testbutton);
test.setText("Start");
//report user
Log.d(TAG,"Finished");
Toast.makeText(getApplicationContext(), "FINISHED", Toast.LENGTH_SHORT).show();
}
if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
final int extra = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1);
if(extra == (BluetoothAdapter.STATE_ON)) {
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
Boolean b = bluetoothAdapter.startDiscovery();
Toast.makeText(getApplicationContext(), "Start discovery" + b, Toast.LENGTH_SHORT).show();
}
}
}
};
#Override
protected void onDestroy() {
super.onDestroy();
if (bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
unregisterReceiver(receiver);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == REQUEST_ENABLE_BLUETOOTH){
checkBluetoothState();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
switch (requestCode){
case REQUEST_ACCESS_COARSE_LOCATION:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(),"Permission granted",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Permission denied",Toast.LENGTH_SHORT).show();
}
}
}
}
This is my Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<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"/>
Enable location permission on the application. To do this go to:
Android phone settings
App & notifications
See all app
Locate your application and select it
Permissions
Allow location slide it on
Then
Turn bluetooth on the device on
Turn location on the device on
Or some code to do it automatically via a popup (call in oncreate method)
public void checkPermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,}, 1);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
} else {
checkPermission();
}
}
Starting Android 10 the Location service must be enabled otherwise no devices will be found.
I tested BluetoothAdapter.startDiscovery() on Huawei P30 with Android 10 and this method alway return false but really the discovery is started (with Location permission granted and location service enabled). So i don't check the result of startDiscovery() method.
According to the official Android Documentation you need to have both ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions to start discovering Bluetooth devices.
/**
* From Android 10 onwards it needs Access Location to search Bluetooth Devices
*/
private void checkForLocationPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
discoverDevices();
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,}, 1);
}
}
}
/**
* Request Access Location while using the App, because bluetooth need location to start discovering devices
* #param requestCode
* #param permissions
* #param grantResults
*/
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
discoverDevices();
} else {
checkForLocationPermission();
}
}
The above code snippet will help you to request the above permissions from the user.
P.S: Also, these need to specified on the Android Manifest as well.

Splashscreen with runtime permissions

I have a splash screen on launching my app. So when showing the splash screen run time permission should show. I got this code below from github. But runtime permissions are not showing. Splash screen works fine, but runtime permission is not working when add this code below. I have added permissions read sms, read external storage, access location. I have given all these permissions in the manifest file also.
public class SplashActivity extends AppCompatActivity {
String loginstatus;
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}, 3000);
/* Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 10 seconds
sleep(4*1000);
startActivity(new Intent(SplashScreen.this,HomeActivity.class));
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();*/
}
private void permissioncheck() {
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("READ");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsNeeded.add("COURLOC");
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("FINELOC");
if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
permissionsNeeded.add("SMS");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
// Pre-Marshmallow
}
}
});
return;
}
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
} else {
// Pre-Marshmallow
}
return;
}else
{
// Toast.makeText(this,"Permission",Toast.LENGTH_LONG).show();
LaunchApp();
}
//insertDummyContact();
}
private boolean addPermission(List<String> permissionsList, String permission) {
Boolean cond;
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
// return false;
cond = false;
}
// return true;
cond = true;
} else {
// Pre-Marshmallow
cond = true;
}
return cond;
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(SplashActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == 23) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();
}
}
if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
// insertDummyContact();
//Toast.makeText(SplashScreen.this, " Permissions are l", Toast.LENGTH_SHORT).show();
LaunchApp();
} else {
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
finish();
}
}, 3000);
}
}
}
public void LaunchApp()
{
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 10 seconds
sleep(4*1000);
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
You need to call permissioncheck() on onCreate() method after setContentView(). Replace the code provided below with your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
permissioncheck();
}
I removed Handler codes on onCreate() method. Instead, use onRequestPermissionResult() and start LoginActivity from it. Replace the following code to your onRequestPermissionResult().
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == 23) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();
}
}
if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show();
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
// Here start the activity
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}, 3000);
} else {
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
finish();
}
}
}
Please carefully read comments in the code provided above.
Best Code
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public class Firstclass extends AppCompatActivity {
private int timeoutMillis = 5000;
/** The time when this {#link AppCompatActivity} was created. */
private long startTimeMillis = 0;
/** The code used when requesting permissions */
private static final int PERMISSIONS_REQUEST = 1234;
public int getTimeoutMillis() {
return timeoutMillis;
}
#SuppressWarnings("rawtypes")
public Class getNextActivityClass() {
return SecondActivity.class;
};
public String[] getRequiredPermissions() {
String[] permissions = null;
try {
permissions = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_PERMISSIONS).requestedPermissions;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (permissions == null) {
return new String[0];
} else {
return permissions.clone();
}
}
#TargetApi(23)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTimeMillis = System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= 23) {
checkPermissions();
} else {
startNextActivity();
}
}
#TargetApi(23)
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST) {
checkPermissions();
}
}
private void startNextActivity() {
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
long delayMillis = getTimeoutMillis() - (System.currentTimeMillis() - startTimeMillis);
if (delayMillis < 0) {
delayMillis = 0;
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Firstclass.this, getNextActivityClass()));
finish();
}
}, delayMillis);
}
private void checkPermissions() {
String[] ungrantedPermissions = requiredPermissionsStillNeeded();
if (ungrantedPermissions.length == 0) {
startNextActivity();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(ungrantedPermissions, PERMISSIONS_REQUEST);
}
}
}
#TargetApi(23)
private String[] requiredPermissionsStillNeeded() {
Set<String> permissions = new HashSet<String>();
for (String permission : getRequiredPermissions()) {
permissions.add(permission);
}
for (Iterator<String> i = permissions.iterator(); i.hasNext();) {
String permission = i.next();
if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
Log.d(Firstclass.class.getSimpleName(),
"Permission: " + permission + " already granted.");
i.remove();
} else {
Log.d(Firstclass.class.getSimpleName(),
"Permission: " + permission + " not yet granted.");
}
}
return permissions.toArray(new String[permissions.size()]);
}
}
Manifest xml permission
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

Android onRequestPermissionsResult grantResults size > 1

After requesting permission, the ActivityCompat.OnRequestPermissionsResultCallback sometimes contains multiple grantResults, is it safe to just check the first one?
The training doc check the param like this:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
but it's not clearly and no documents found.
No, It is not a good way to just check first permission, it might be possible that user have allowed first permission but denied for rest permissions. Here is function i am sharing to check whether all permissions are granted or not
public boolean hasAllPermissionsGranted(#NonNull int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
and in your onRequestPermissionsResult
if(hasAllPermissionsGranted(grantResults)){
// all permissions granted
}else {
// some permission are denied.
}
The shortest way you can make sure all the permissions are granted by the user.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (Arrays.binarySearch(grantResults, -1) >= 0) {
/* Some permissions are not granted
request permission again if required */
return;
}
}
The integer array that you can use to validate permissions :
if (Arrays.binarySearch(grantResults, -1) >= 0) { // some permissions are not granted }
You can check them all
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you are good to go
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
According to the docs for requestPermissions
requestCode int: Application specific request code to match with a
result reported to onRequestPermissionsResult(int, String[], int[]).
Should be >= 0.
Since requestCode is Application specific, it's defined by the developer for a specific need, i.e.
public class Main : Activity
{
private Bundle _savedInstanceState;
private bool _bStorageRationaleBefore;
private bool _bStorageRationaleAfter;
private bool _bCameraRationaleBefore;
private bool _bCameraRationaleAfter;
private const int ANDROID_PERMISSION_REQUEST_CODE__SDCARD = 2;
private const int ANDROID_PERMISSION_REQUEST_CODE__CAMERA = 1;
private const int ANDROID_PERMISSION_REQUEST_CODE__NONE = 0;
private bool VerifyWriteExternalStoragePermissionRequestResult(string permission, Permission grantResult)
{
_bStorageRationaleAfter = ShouldShowRequestPermissionRationale(Android.Manifest.Permission.WriteExternalStorage);
if (permission != Android.Manifest.Permission.WriteExternalStorage || grantResult != Permission.Granted)
{
return false;
}
return true;
}
private bool VerifyCameraPermissionRequestResult(string permission, Permission grantResult)
{
_bCameraRationaleAfter = ShouldShowRequestPermissionRationale(Android.Manifest.Permission.Camera);
if (permission != Android.Manifest.Permission.Camera || grantResult != Permission.Granted)
{
return false;
}
return true;
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
// note: OnRequestPermissionsResult() runs in a separate thread.
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
bool bStorage = true;
bool bCamera = true;
switch (requestCode)
{
case ANDROID_PERMISSION_REQUEST_CODE__SDCARD:
bStorage = VerifyWriteExternalStoragePermissionRequestResult(permissions[0],grantResults[0]);
break;
case ANDROID_PERMISSION_REQUEST_CODE__CAMERA:
bCamera = VerifyCameraPermissionRequestResult(permissions[0], grantResults[0]);
break;
case ANDROID_PERMISSION_REQUEST_CODE__SDCARD | ANDROID_PERMISSION_REQUEST_CODE__CAMERA:
bStorage = VerifyWriteExternalStoragePermissionRequestResult(permissions[0], grantResults[0]);
bCamera = VerifyCameraPermissionRequestResult(permissions[1], grantResults[1]);
break;
}
// Could check bCamera, but it isn't necessary to continue, and can be prompted for again when camera is needed.
// Note however that every view that potentially requires the camera will have to:
///////////////////////////////////////////////////////////////////
// 1. Call ApplicationContext.CheckSelfPermission()
// 2. Call RequestPermissions()
// 3. Override OnRequestPermissionsResult()
///////////////////////////////////////////////////////////////////
// hence why I'd rather get it done in one spot during startup (main)
if (bStorage && bCamera)
{
RestartActivity();
}
else
{
// Show error message alert. RestartActivity called from MessageAlertDialogOkClickEventHandler()
// to prevent race condition between StartActivity() and ShowDialog()
System.Text.StringBuilder errMsg = new System.Text.StringBuilder();
string appName = this.ApplicationContext.ApplicationInfo.LoadLabel(PackageManager);
PermissionGroupInfo pgiStorage = this.PackageManager.GetPermissionGroupInfo(Android.Manifest.Permission_group.Storage, PackageInfoFlags.Permissions);
PermissionGroupInfo pgiCamera = this.PackageManager.GetPermissionGroupInfo(Android.Manifest.Permission_group.Camera, PackageInfoFlags.Permissions);
bool bNeverAskForStorage =
!bStorage && (
_bStorageRationaleBefore == true && _bStorageRationaleAfter == false ||
_bStorageRationaleBefore == false && _bStorageRationaleAfter == false
);
bool bNeverAskForCamera =
!bCamera && (
_bCameraRationaleBefore == true && _bCameraRationaleAfter == false ||
_bCameraRationaleBefore == false && _bCameraRationaleAfter == false
);
if (bNeverAskForStorage || bNeverAskForCamera)
{
errMsg.Append("To continue, enable " + appName + " Permissions:\n\n");
if (!bStorage) errMsg.Append("\t* " + pgiStorage.LoadLabel(PackageManager) + "\n");
if (!bCamera) errMsg.Append("\t* " + pgiCamera.LoadLabel(PackageManager) + "\n");
errMsg.Append("\n(Use \"back button\" when finished to return.)");
CommonView.ShowMessageAlertDialog(this.FragmentManager, errMsg.ToString(), PermissionMessageAlertDialogOkClickEventHandler2);
}
else // if (!bNeverAskForStorage && !bNeverAskForCamera)
{
errMsg.Append("To continue, allow " + appName + " to:\n\n");
if (!bStorage) errMsg.Append("\t* " + pgiStorage.LoadDescription(PackageManager) + "\n");
if (!bCamera) errMsg.Append("\t* " + pgiCamera.LoadDescription(PackageManager) + "\n");
CommonView.ShowMessageAlertDialog(this.FragmentManager, errMsg.ToString(), PermissionMessageAlertDialogOkClickEventHandler);
}
}
}
private void PermissionMessageAlertDialogOkClickEventHandler(object sender, EventArgs e)
{
RestartActivity();
}
private void PermissionMessageAlertDialogOkClickEventHandler2(object sender, EventArgs e)
{
Intent intent = new Intent();
intent.SetAction(Settings.ActionApplicationDetailsSettings);
Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
intent.SetData(uri);
StartActivityForResult(intent, 0);
//RestartActivity();
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
RestartActivity();
}
private void RestartActivity()
{
Intent restartThisActivityIntent = new Intent(this, this.GetType());
if (_savedInstanceState != null)
{
restartThisActivityIntent.PutExtras(_savedInstanceState);
}
StartActivity(restartThisActivityIntent);
}
private List<string> GetRequiredPermissions(out int requestCode)
{
// Android v6 requires explicit permission granting from user at runtime for security reasons
requestCode = ANDROID_PERMISSION_REQUEST_CODE__NONE; // 0
List<string> requiredPermissions = new List<string>();
_bStorageRationaleBefore = ShouldShowRequestPermissionRationale(Android.Manifest.Permission.WriteExternalStorage);
Permission writeExternalStoragePerm = ApplicationContext.CheckSelfPermission(Android.Manifest.Permission.WriteExternalStorage);
//if(extStoragePerm == Permission.Denied)
if (writeExternalStoragePerm != Permission.Granted)
{
requestCode |= ANDROID_PERMISSION_REQUEST_CODE__SDCARD;
requiredPermissions.Add(Android.Manifest.Permission.WriteExternalStorage);
}
_bCameraRationaleBefore = ShouldShowRequestPermissionRationale(Android.Manifest.Permission.Camera);
Permission cameraPerm = ApplicationContext.CheckSelfPermission(Android.Manifest.Permission.Camera);
if (cameraPerm != Permission.Granted)
{
requestCode |= ANDROID_PERMISSION_REQUEST_CODE__CAMERA;
requiredPermissions.Add(Android.Manifest.Permission.Camera);
}
return requiredPermissions;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
try
{
// Android v6 requires explicit permission granting from user at runtime for security reasons
int requestCode;
List<string> requiredPermissions = GetRequiredPermissions(out requestCode);
if (requiredPermissions != null && requiredPermissions.Count > 0)
{
//if (requestCode >= ANDROID_PERMISSION_REQUEST_CODE__SDCARD)
if (requestCode >= ANDROID_PERMISSION_REQUEST_CODE__CAMERA)
{
_savedInstanceState = savedInstanceState;
RequestPermissions(requiredPermissions.ToArray(), requestCode);
return;
}
}
}
catch (Exception ex)
{
Global.LogFile.WriteEntry(ex.ToString());
CommonView.ShowMessageAlertDialog(this.FragmentManager, ex.Message);
return;
}
OnCreate2(savedInstanceState);
}
}

Categories

Resources