-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jenkins
committed
Nov 16, 2022
1 parent
dc7a708
commit 9563045
Showing
24 changed files
with
906 additions
and
1,649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.regula.documentreader"> | ||
|
||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | ||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
<uses-permission android:name="android.permission.CAMERA" /> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.NFC" /> | ||
</manifest> | ||
|
||
|
108 changes: 108 additions & 0 deletions
108
android/src/main/java/com/regula/documentreader/BluetoothUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.regula.documentreader | ||
|
||
import android.Manifest.permission.* | ||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.bluetooth.BluetoothAdapter | ||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.content.ServiceConnection | ||
import android.content.pm.PackageManager.PERMISSION_GRANTED | ||
import android.os.Build | ||
import android.os.IBinder | ||
import android.provider.Settings | ||
import androidx.annotation.RequiresPermission | ||
import androidx.core.app.ActivityCompat.requestPermissions | ||
import androidx.core.content.ContextCompat.checkSelfPermission | ||
import com.regula.documentreader.api.ble.BLEWrapper | ||
import com.regula.documentreader.api.ble.BleWrapperCallback | ||
import com.regula.documentreader.api.ble.RegulaBleService | ||
import com.regula.documentreader.api.internal.permission.BluetoothPermissionHelper.BLE_ACCESS_PERMISSION | ||
import com.regula.documentreader.api.internal.permission.BluetoothSettingsHelper.isBluetoothEnabled | ||
import com.regula.documentreader.api.internal.permission.BluetoothSettingsHelper.isLocationServiceEnabled | ||
|
||
class BluetoothUtil { | ||
companion object { | ||
private const val REQUEST_ENABLE_LOCATION = 196 | ||
private const val REQUEST_ENABLE_BT = 197 | ||
|
||
@SuppressLint("StaticFieldLeak") | ||
var bleManager: BLEWrapper? = null | ||
|
||
@RequiresPermission("android.permission.BLUETOOTH_CONNECT") | ||
private fun requestEnableBle(activity: Activity) { | ||
val enableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) | ||
activity.startActivityForResult(enableIntent, REQUEST_ENABLE_BT) | ||
} | ||
|
||
private fun requestEnableLocationService(activity: Activity) { | ||
val myIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS) | ||
activity.startActivityForResult(myIntent, REQUEST_ENABLE_LOCATION) | ||
} | ||
|
||
// requestEnableBle() is called after a check for permission | ||
@SuppressLint("MissingPermission") | ||
fun isBlePermissionsGranted(activity: Activity): Boolean { | ||
if (!isLocationServiceEnabled(activity)) { | ||
requestEnableLocationService(activity) | ||
return false | ||
} | ||
deniedBluetoothPermissions(activity)?.let { | ||
requestPermissions(activity, it, BLE_ACCESS_PERMISSION) | ||
return false | ||
} | ||
if (!isBluetoothEnabled(activity)) { | ||
requestEnableBle(activity) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
private fun deniedBluetoothPermissions(activity: Activity): Array<String>? { | ||
val result = mutableListOf<String>() | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
result.addAll(deniedBluetoothPermission(activity, BLUETOOTH_SCAN)) | ||
result.addAll(deniedBluetoothPermission(activity, BLUETOOTH_CONNECT)) | ||
} else | ||
result.addAll(deniedBluetoothPermission(activity, ACCESS_FINE_LOCATION)) | ||
return result.let { if (it.size > 0) it.toTypedArray() else null } | ||
} | ||
|
||
private fun deniedBluetoothPermission( | ||
activity: Activity, | ||
permission: String | ||
): Array<String> { | ||
if (checkSelfPermission(activity, permission) != PERMISSION_GRANTED) | ||
return arrayOf(permission) | ||
return arrayOf() | ||
} | ||
|
||
fun startBluetoothService( | ||
activity: Activity, | ||
onConnected: (Boolean) -> Unit, | ||
onDisconnected: () -> Unit, | ||
onReady: () -> Unit, | ||
) { | ||
val bleIntent = Intent(activity, RegulaBleService::class.java) | ||
activity.startService(bleIntent) | ||
|
||
activity.bindService(bleIntent, object : ServiceConnection { | ||
override fun onServiceConnected(name: ComponentName, service: IBinder) { | ||
bleManager = (service as RegulaBleService.LocalBinder).service.bleManager | ||
val isBleManagerConnected = bleManager?.isConnected == true | ||
onConnected(isBleManagerConnected) | ||
if (!isBleManagerConnected) { | ||
bleManager?.addCallback(object : BleWrapperCallback() { | ||
override fun onDeviceReady() { | ||
bleManager!!.removeCallback(this) | ||
onReady() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName) = onDisconnected() | ||
}, 0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.