Skip to content

Commit

Permalink
Add Bluetooth permissions for Android 12+
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita36078 authored and woesss committed May 21, 2024
1 parent 1d4b7e0 commit 8c0d106
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

<uses-sdk tools:overrideLibrary="com.arthenica.mobileffmpeg, com.google.oboe, org.sufficientlysecure.donations" />
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/javax/bluetooth/LocalDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Build;

import java.util.Hashtable;

Expand All @@ -36,7 +37,7 @@ public class LocalDevice implements ActivityResultListener {
private Object monitor = new Object();

static {
properties = new Hashtable<String, String>();
properties = new Hashtable<>();
properties.put("bluetooth.api.version", "1.1");
properties.put("bluetooth.master.switch", "true");
properties.put("bluetooth.sd.attr.retrievable.max", "256");
Expand All @@ -52,7 +53,18 @@ public class LocalDevice implements ActivityResultListener {
private LocalDevice() throws BluetoothStateException {
agent = new DiscoveryAgent();
ContextHolder.addActivityResultListener(this);
if (!ContextHolder.requestPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
boolean permissionsGranted;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
String[] permissions = {
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_ADVERTISE,
};
permissionsGranted = ContextHolder.requestPermissions(permissions);
} else {
permissionsGranted = ContextHolder.requestPermission(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (!permissionsGranted) {
throw new BluetoothStateException();
}
if (!DiscoveryAgent.adapter.isEnabled()) {
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/javax/microedition/util/ContextHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ public static boolean requestPermission(String permission) {
}
}

public static boolean requestPermissions(String[] permissions) {
MicroActivity context = currentActivity.get();
if (context == null) {
return false;
}
if (!hasPermissions(context, permissions)) {
ActivityCompat.requestPermissions(context, permissions, 0);
return false;
} else {
return true;
}
}

private static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}

public static String getAssetAsString(String fileName) {
StringBuilder sb = new StringBuilder();

Expand Down

0 comments on commit 8c0d106

Please sign in to comment.