Skip to content

Instantly share code, notes, and snippets.

@NeedPainkiller
Created May 13, 2016 09:41
Show Gist options
  • Save NeedPainkiller/88812ce838f5e284fe531a3978f26a07 to your computer and use it in GitHub Desktop.
Save NeedPainkiller/88812ce838f5e284fe531a3978f26a07 to your computer and use it in GitHub Desktop.
public static final boolean IS_BUILD_VERSION_LM = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
private static final int REQUEST_ENABLE_BLE = 1;
private static final int RESULT_OK = -1;
private static final int PERMISSION_GRANTED = PackageManager.PERMISSION_GRANTED;
private static final String PERMISSION_COARSE = Manifest.permission.ACCESS_COARSE_LOCATION;
private static final String PERMISSION_FINE = Manifest.permission.ACCESS_FINE_LOCATION;
private static WeakReference<Activity> reference;
@TargetApi(Build.VERSION_CODES.M)
public static void requestBluetoothPermission(Activity activity) {
reference = new WeakReference<>(activity);
if (checkPermission(PERMISSION_COARSE) != PERMISSION_GRANTED || checkPermission(PERMISSION_FINE) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(reference.get(), new String[]{PERMISSION_COARSE, PERMISSION_FINE}, REQUEST_ENABLE_BLE);
}
reference.clear();
}
private static int checkPermission(String permissionTag) {
return ContextCompat.checkSelfPermission(reference.get(), permissionTag);
}
public static void requestBluetoothEnable(Activity activity) {
reference = new WeakReference<>(activity);
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
ActivityCompat.startActivityForResult(reference.get(), intent, REQUEST_ENABLE_BLE, null);
reference.clear();
}
public static void onRequestPermissionsResult(int requestCode, int[] grantResults, Activity activity) {
reference = new WeakReference<>(activity);
if (requestCode == BluetoothHelper.REQUEST_ENABLE_BLE) {
if (grantResults.length != 0) {
if (grantResults[0] == PERMISSION_GRANTED || grantResults[1] == PERMISSION_GRANTED) {
Toast.makeText(reference.get(), R.string.permission_thanks, Toast.LENGTH_SHORT).show();
} else {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + reference.get().getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityCompat.startActivityForResult(reference.get(), myAppSettings, 0, null);
Toast.makeText(reference.get(), R.string.permission_request, Toast.LENGTH_SHORT).show();
reference.get().finish();
}
}
} else {
Toast.makeText(reference.get(), R.string.permission_denial, Toast.LENGTH_SHORT).show();
}
reference.clear();
}
public static void onRequestEnableResult(int requestCode, int resultCode, Activity activity) {
reference = new WeakReference<>(activity);
if (requestCode == REQUEST_ENABLE_BLE && resultCode == RESULT_OK) {
Toast.makeText(reference.get(), R.string.bluetooth_on, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(reference.get(), R.string.bluetooth_not_init, Toast.LENGTH_SHORT).show();
reference.get().finish();
}
reference.clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment