react-native-mytatva-rn-sdk 1.2.44 → 1.2.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (21) hide show
  1. package/android/src/main/java/com/mytatvarnsdk/CgmTrackyLibModule.kt +21 -4
  2. package/android/src/main/java/com/mytatvarnsdk/activity/ConnectSensorActivity.kt +18 -3
  3. package/android/src/main/java/com/mytatvarnsdk/activity/HelpActivity.kt +118 -59
  4. package/android/src/main/java/com/mytatvarnsdk/activity/PermissionActivity.kt +370 -351
  5. package/android/src/main/java/com/mytatvarnsdk/activity/PlaceSensorActivity.kt +7 -2
  6. package/android/src/main/java/com/mytatvarnsdk/activity/PlaceTransmitterActivity.kt +10 -4
  7. package/android/src/main/java/com/mytatvarnsdk/activity/SearchTransmitterActivity.kt +30 -53
  8. package/android/src/main/java/com/mytatvarnsdk/activity/SensorConnectSuccessActivity.kt +9 -2
  9. package/android/src/main/java/com/mytatvarnsdk/activity/StartCGMActivity.kt +11 -3
  10. package/android/src/main/java/com/mytatvarnsdk/model/BaseViewModel.java +1 -1
  11. package/ios/MyReactNativeBridge.m +60 -55
  12. package/ios/Support/API.swift +1 -1
  13. package/ios/ViewControllers/AttachTransmitterViewController.swift +1 -0
  14. package/ios/ViewControllers/ChatWithExpertViewController.swift +4 -0
  15. package/ios/ViewControllers/ConnectToSensorViewController.swift +1 -0
  16. package/ios/ViewControllers/ConnectToTransmitterViewController.swift +19 -6
  17. package/ios/ViewControllers/ProvidePermissionViewController.swift +3 -0
  18. package/ios/ViewControllers/PutOnTheSensorViewController.swift +1 -0
  19. package/ios/ViewControllers/StartConnectionViewController.swift +4 -2
  20. package/ios/ViewModel/FinalViewModel.swift +15 -22
  21. package/package.json +1 -1
@@ -38,372 +38,391 @@ import com.mytatvarnsdk.base.BasePermissionActivity
38
38
  import com.mytatvarnsdk.databinding.ActivityPermissionBinding
39
39
  import com.mytatvarnsdk.databinding.BluetoothDialogBottomsheetBinding
40
40
  import com.mytatvarnsdk.databinding.ExitDialogBottomsheetBinding
41
+ import org.json.JSONObject
41
42
 
42
43
  class PermissionActivity : BasePermissionActivity() {
43
- private lateinit var binding: ActivityPermissionBinding
44
- private val LOCATION_REQUEST_CODE = 101
45
- private val BLUETOOTH_REQUEST_CODE = 102
46
-
47
- private val REQUEST_ENABLE_BT = 1
48
- private val bluetoothAdapter: BluetoothAdapter? by lazy {
49
- BluetoothAdapter.getDefaultAdapter()
50
- }
51
- private val reactContext = CgmTrackyLibModule.mReactContext
52
-
53
- override fun onCreate(savedInstanceState: Bundle?) {
54
- super.onCreate(savedInstanceState)
55
- binding = ActivityPermissionBinding.inflate(layoutInflater)
56
-
57
- enableEdgeToEdge()
58
- ViewCompat.setOnApplyWindowInsetsListener(binding.rlPopBottom) { v, insets ->
59
- val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
60
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
61
- insets
62
- }
63
- setContentView(binding.root)
64
-
65
- initialize()
66
- }
67
-
68
- override fun getPermission(): Array<out Array<out String?>?>? {
69
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
70
- return arrayOf<Array<String?>?>(ConstantsLibrary.BLUETOOTH_S)
71
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
72
- return arrayOf<Array<String?>?>(
73
- ConstantsLibrary.LOCAL_PERMISSION,
74
- ConstantsLibrary.BACKGROUND_LOCATION_Q
75
- )
76
- } else {
77
- return arrayOf<Array<String?>?>(ConstantsLibrary.LOCAL_PERMISSION)
78
- }
79
- }
44
+ private lateinit var binding: ActivityPermissionBinding
45
+ private val LOCATION_REQUEST_CODE = 101
46
+ private val BLUETOOTH_REQUEST_CODE = 102
80
47
 
81
- override fun onPermissionRequestSuccess() {
82
- }
48
+ private val REQUEST_ENABLE_BT = 1
49
+ private val bluetoothAdapter: BluetoothAdapter? by lazy {
50
+ BluetoothAdapter.getDefaultAdapter()
51
+ }
52
+ private val reactContext = CgmTrackyLibModule.mReactContext
83
53
 
84
- override fun onPermissionRequestFail(vararg permission: String?) {
85
- }
54
+ override fun onCreate(savedInstanceState: Bundle?) {
55
+ super.onCreate(savedInstanceState)
56
+ binding = ActivityPermissionBinding.inflate(layoutInflater)
86
57
 
87
- override fun onPermissionRequestFailForever(vararg permission: String?) {
88
- }
58
+ enableEdgeToEdge()
59
+ ViewCompat.setOnApplyWindowInsetsListener(binding.rlPopBottom) { v, insets ->
60
+ val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
61
+ v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
62
+ insets
63
+ }
64
+ setContentView(binding.root)
89
65
 
90
- fun isBluetoothPermissionGranted(context: Context): Boolean {
91
- return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
92
- val connectPermission = ContextCompat.checkSelfPermission(
93
- context,
94
- android.Manifest.permission.BLUETOOTH_CONNECT
95
- ) == PackageManager.PERMISSION_GRANTED
66
+ initialize()
67
+ }
96
68
 
97
- val scanPermission = ContextCompat.checkSelfPermission(
98
- context,
99
- android.Manifest.permission.BLUETOOTH_SCAN
100
- ) == PackageManager.PERMISSION_GRANTED
69
+ override fun getPermission(): Array<out Array<out String?>?>? {
70
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
71
+ return arrayOf<Array<String?>?>(ConstantsLibrary.BLUETOOTH_S)
72
+ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
73
+ return arrayOf<Array<String?>?>(
74
+ ConstantsLibrary.LOCAL_PERMISSION,
75
+ ConstantsLibrary.BACKGROUND_LOCATION_Q
76
+ )
77
+ } else {
78
+ return arrayOf<Array<String?>?>(ConstantsLibrary.LOCAL_PERMISSION)
79
+ }
80
+ }
101
81
 
102
- connectPermission && scanPermission
103
- } else {
104
- // For Android 11 and below, permission is granted at install time
105
- true
82
+ override fun onPermissionRequestSuccess() {
106
83
  }
107
- }
108
84
 
109
- fun isLocationPermissionGranted(context: Context): Boolean {
110
- val fineLocationGranted = ContextCompat.checkSelfPermission(
111
- context,
112
- android.Manifest.permission.ACCESS_FINE_LOCATION
113
- ) == PackageManager.PERMISSION_GRANTED
85
+ override fun onPermissionRequestFail(vararg permission: String?) {
86
+ }
114
87
 
115
- val coarseLocationGranted = ContextCompat.checkSelfPermission(
116
- context,
117
- android.Manifest.permission.ACCESS_COARSE_LOCATION
118
- ) == PackageManager.PERMISSION_GRANTED
88
+ override fun onPermissionRequestFailForever(vararg permission: String?) {
89
+ }
119
90
 
120
- return fineLocationGranted || coarseLocationGranted
121
- }
91
+ fun isBluetoothPermissionGranted(context: Context): Boolean {
92
+ return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
93
+ val connectPermission = ContextCompat.checkSelfPermission(
94
+ context,
95
+ android.Manifest.permission.BLUETOOTH_CONNECT
96
+ ) == PackageManager.PERMISSION_GRANTED
122
97
 
98
+ val scanPermission = ContextCompat.checkSelfPermission(
99
+ context,
100
+ android.Manifest.permission.BLUETOOTH_SCAN
101
+ ) == PackageManager.PERMISSION_GRANTED
123
102
 
124
- @SuppressLint("MissingPermission")
125
- private fun initialize() {
126
- bluetoothAdapter?.let {
127
- binding.switchBluetooth.isChecked = isBluetoothPermissionGranted(this) && it.isEnabled
103
+ connectPermission && scanPermission
104
+ } else {
105
+ // For Android 11 and below, permission is granted at install time
106
+ true
107
+ }
128
108
  }
129
109
 
130
- binding.switchLocation.isChecked = isLocationPermissionGranted(this)
110
+ fun isLocationPermissionGranted(context: Context): Boolean {
111
+ val fineLocationGranted = ContextCompat.checkSelfPermission(
112
+ context,
113
+ android.Manifest.permission.ACCESS_FINE_LOCATION
114
+ ) == PackageManager.PERMISSION_GRANTED
131
115
 
132
- binding.switchBluetooth.setOnCheckedChangeListener { _, isChecked ->
133
- if (isChecked) {
134
- requestBluetoothPermission()
135
- }
136
- }
116
+ val coarseLocationGranted = ContextCompat.checkSelfPermission(
117
+ context,
118
+ android.Manifest.permission.ACCESS_COARSE_LOCATION
119
+ ) == PackageManager.PERMISSION_GRANTED
137
120
 
138
- binding.switchLocation.setOnCheckedChangeListener { _, isChecked ->
139
- if (isChecked) {
140
- requestLocationPermission()
141
- }
121
+ return fineLocationGranted || coarseLocationGranted
142
122
  }
143
123
 
144
- binding.commonButton.btnProceed.setOnClickListener {
145
- startActivity(
146
- Intent(this@PermissionActivity, SearchTransmitterActivity::class.java).putExtra(
147
- "IsForReconnect",
148
- intent.getBooleanExtra("IsForReconnect", false)
149
- )
150
- )
151
- finish()
152
- }
153
124
 
154
- binding.toolbar.btnWhatsapp.setOnClickListener {
155
- startActivity(Intent(this, HelpActivity::class.java))
156
- }
125
+ @SuppressLint("MissingPermission")
126
+ private fun initialize() {
127
+ sendDataToRN("", "cgm_permissions_page_landing")
157
128
 
158
- binding.toolbar.btnWatchDemo.setOnClickListener {
159
- sendDataToRN("","cgm_watch_demo_clicked")
129
+ bluetoothAdapter?.let {
130
+ binding.switchBluetooth.isChecked = isBluetoothPermissionGranted(this) && it.isEnabled
131
+ }
160
132
 
161
- startActivity(
162
- Intent(this, VideoActivity::class.java).putExtra("VideoId", "r5Zemc4R044")
163
- )
164
- }
133
+ binding.switchLocation.isChecked = isLocationPermissionGranted(this)
165
134
 
166
- binding.tvKnowMore.setOnClickListener {
167
- openBluetoothDialog()
168
- }
135
+ binding.switchBluetooth.setOnCheckedChangeListener { _, isChecked ->
136
+ if (isChecked) {
137
+ requestBluetoothPermission()
138
+ }
139
+ }
169
140
 
170
- binding.toolbar.btnClose.setOnClickListener {
171
- openExitDialog()
172
- }
141
+ binding.switchLocation.setOnCheckedChangeListener { _, isChecked ->
142
+ if (isChecked) {
143
+ requestLocationPermission()
144
+ }
145
+ }
146
+
147
+ binding.commonButton.btnProceed.setOnClickListener {
148
+ val jsonObject = JSONObject()
149
+ jsonObject.put("bluetooth_toggle_status", "ON")
150
+ jsonObject.put("location_toggle_status", "ON")
151
+ sendDataToRN(jsonObject.toString(), "cgm_permissions_proceed_clicked")
152
+
153
+ startActivity(
154
+ Intent(this@PermissionActivity, SearchTransmitterActivity::class.java).putExtra(
155
+ "IsForReconnect",
156
+ intent.getBooleanExtra("IsForReconnect", false)
157
+ )
158
+ )
159
+ finish()
160
+ }
161
+
162
+ binding.toolbar.btnWhatsapp.setOnClickListener {
163
+ startActivity(
164
+ Intent(this, HelpActivity::class.java)
165
+ .putExtra("ScreenName", "Permission Screen")
166
+ )
167
+ }
168
+
169
+ binding.toolbar.btnWatchDemo.setOnClickListener {
170
+ sendDataToRN("", "cgm_watch_demo_clicked")
173
171
 
174
- val callback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
175
- override fun handleOnBackPressed() {
176
- openExitDialog()
177
- }
172
+ startActivity(
173
+ Intent(this, VideoActivity::class.java).putExtra("VideoId", "r5Zemc4R044")
174
+ )
175
+ }
176
+
177
+ binding.tvKnowMore.setOnClickListener {
178
+ openBluetoothDialog()
179
+ }
180
+
181
+ binding.toolbar.btnClose.setOnClickListener {
182
+ openExitDialog()
183
+ }
184
+
185
+ val callback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
186
+ override fun handleOnBackPressed() {
187
+ openExitDialog()
188
+ }
189
+ }
190
+ onBackPressedDispatcher.addCallback(this, callback)
178
191
  }
179
- onBackPressedDispatcher.addCallback(this, callback)
180
- }
181
-
182
- private fun sendDataToRN(data: String, status: String) {
183
- if (reactContext != null) {
184
- try {
185
- val catalystInstance: CatalystInstance = reactContext.catalystInstance
186
- val module = catalystInstance.getNativeModule(CgmTrackyLibModule::class.java)
187
-
188
- if (module == null) {
189
- sendDataToRNDirectly("", status)
190
- Log.d("sendDataToRN: ", "Module null")
192
+
193
+ private fun sendDataToRN(data: String, status: String) {
194
+ if (reactContext != null) {
195
+ try {
196
+ val catalystInstance: CatalystInstance = reactContext.catalystInstance
197
+ val module = catalystInstance.getNativeModule(CgmTrackyLibModule::class.java)
198
+
199
+ if (module == null) {
200
+ sendDataToRNDirectly(data, status)
201
+ Log.d("sendDataToRN: ", "Module null")
202
+ } else {
203
+ module.sendDataToReact(data, status, "cgmDeviceEvent")
204
+ Log.d("sendDataToRN: ", "Module is not null")
205
+ }
206
+ } catch (e: Exception) {
207
+ Log.e("sendDataToRN: Error ", e.message.toString())
208
+ }
191
209
  } else {
192
- module.sendDataToReact(data, status, "cgmDeviceEvent")
193
- Log.d("sendDataToRN: ", "Module is not null")
210
+ Log.e("TrackyActivity", "ReactApplicationContext is null")
194
211
  }
195
- } catch (e: Exception) {
196
- Log.e("sendDataToRN: Error ", e.message.toString())
197
- }
198
- } else {
199
- Log.e("TrackyActivity", "ReactApplicationContext is null")
200
212
  }
201
- }
202
213
 
203
214
 
204
- private fun sendDataToRNDirectly(data: String, status: String) {
205
- try {
206
- val map: WritableMap = Arguments.createMap().apply {
207
- putString("data", data)
208
- putString("status", status)
209
- }
215
+ private fun sendDataToRNDirectly(data: String, status: String) {
216
+ try {
217
+ val map: WritableMap = Arguments.createMap().apply {
218
+ putString("data", data)
219
+ putString("status", status)
220
+ }
210
221
 
211
- reactContext?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
212
- ?.emit("cgmDeviceEvent", map)
213
- } catch (e: Exception) {
214
- Log.e("sendDataToRNDirectly", "Error sending data to React", e)
222
+ reactContext?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
223
+ ?.emit("cgmDeviceEvent", map)
224
+ } catch (e: Exception) {
225
+ Log.e("sendDataToRNDirectly", "Error sending data to React", e)
226
+ }
215
227
  }
216
- }
217
228
 
218
- private fun openBluetoothDialog() {
219
- val binding = BluetoothDialogBottomsheetBinding.inflate(layoutInflater)
229
+ private fun openBluetoothDialog() {
230
+ val binding = BluetoothDialogBottomsheetBinding.inflate(layoutInflater)
220
231
 
221
- val dialog = Dialog(this)
222
- dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
223
- dialog.setContentView(binding.root)
232
+ val dialog = Dialog(this)
233
+ dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
234
+ dialog.setContentView(binding.root)
224
235
 
225
- val window = dialog.window
226
- window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
227
- window?.setGravity(Gravity.BOTTOM)
228
- window?.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
236
+ val window = dialog.window
237
+ window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
238
+ window?.setGravity(Gravity.BOTTOM)
239
+ window?.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
229
240
 
230
- binding.tvKnowMore.setOnClickListener {
231
- val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
232
- data = Uri.fromParts("package", packageName, null)
233
- }
234
- startActivity(intent)
235
- }
241
+ binding.tvKnowMore.setOnClickListener {
242
+ val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
243
+ data = Uri.fromParts("package", packageName, null)
244
+ }
245
+ startActivity(intent)
246
+ }
236
247
 
237
- binding.closeButton.setOnClickListener(View.OnClickListener { v: View? ->
238
- dialog.dismiss()
239
- })
248
+ binding.closeButton.setOnClickListener(View.OnClickListener { v: View? ->
249
+ dialog.dismiss()
250
+ })
240
251
 
241
- dialog.show()
242
- }
252
+ dialog.show()
253
+ }
243
254
 
244
- private fun openExitDialog() {
245
- val binding = ExitDialogBottomsheetBinding.inflate(layoutInflater)
255
+ private fun openExitDialog() {
256
+ val binding = ExitDialogBottomsheetBinding.inflate(layoutInflater)
246
257
 
247
- val dialog = Dialog(this)
248
- dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
249
- dialog.setContentView(binding.root)
258
+ val dialog = Dialog(this)
259
+ dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
260
+ dialog.setContentView(binding.root)
250
261
 
251
- val window = dialog.window
252
- window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
253
- window?.setGravity(Gravity.BOTTOM)
254
- window?.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
262
+ val window = dialog.window
263
+ window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
264
+ window?.setGravity(Gravity.BOTTOM)
265
+ window?.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
255
266
 
256
- binding.btnExit.tvProceed.text = "Exit"
267
+ binding.btnExit.tvProceed.text = "Exit"
257
268
 
258
- binding.btnExit.root.setOnClickListener(View.OnClickListener { v: View? ->
259
- finish()
260
- dialog.dismiss()
261
- })
269
+ binding.btnExit.root.setOnClickListener(View.OnClickListener { v: View? ->
270
+ finish()
271
+ dialog.dismiss()
272
+ })
262
273
 
263
- binding.btnCancel.root.setOnClickListener(View.OnClickListener { v: View? ->
264
- dialog.dismiss()
265
- })
274
+ binding.btnCancel.root.setOnClickListener(View.OnClickListener { v: View? ->
275
+ dialog.dismiss()
276
+ })
266
277
 
267
- binding.closeButton.setOnClickListener(View.OnClickListener { v: View? ->
268
- dialog.dismiss()
269
- })
278
+ binding.closeButton.setOnClickListener(View.OnClickListener { v: View? ->
279
+ dialog.dismiss()
280
+ })
270
281
 
271
- dialog.show()
272
- }
282
+ dialog.show()
283
+ }
273
284
 
274
285
 
275
- private fun requestForegroundLocationPermission() {
276
- val locationPermissions = PermissionUtils.LOCAL_PERMISSION
286
+ private fun requestForegroundLocationPermission() {
287
+ val locationPermissions = PermissionUtils.LOCAL_PERMISSION
277
288
 
278
- if (hasPermissions(locationPermissions)) {
279
- onForegroundLocationGranted()
280
- } else {
281
- ActivityCompat.requestPermissions(
282
- this,
283
- locationPermissions,
284
- LOCATION_REQUEST_CODE
285
- )
286
- }
287
- }
288
-
289
- private fun requestLocationPermission() {
290
- val permissions = mutableListOf(*PermissionUtils.LOCAL_PERMISSION)
291
-
292
- val permissionArray = permissions.toTypedArray()
293
-
294
- if (hasPermissions(permissionArray)) {
295
- onLocationPermissionGranted()
296
- } else {
297
- val rationaleNeeded = permissionArray.any {
298
- ActivityCompat.shouldShowRequestPermissionRationale(this, it)
299
- }
300
-
301
- if (rationaleNeeded) {
302
- showPermissionRationale(
303
- title = "Location Permission Needed",
304
- message = "We need location access for Bluetooth scanning.",
305
- onProceed = {
306
- ActivityCompat.requestPermissions(this, permissionArray, LOCATION_REQUEST_CODE)
307
- }
308
- )
309
- } else {
310
- ActivityCompat.requestPermissions(this, permissionArray, LOCATION_REQUEST_CODE)
311
- }
289
+ if (hasPermissions(locationPermissions)) {
290
+ onForegroundLocationGranted()
291
+ } else {
292
+ ActivityCompat.requestPermissions(
293
+ this,
294
+ locationPermissions,
295
+ LOCATION_REQUEST_CODE
296
+ )
297
+ }
312
298
  }
313
- }
314
-
315
- @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
316
- private fun requestBluetoothPermission() {
317
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
318
- val permissions = PermissionUtils.BLUETOOTH_S
319
-
320
- if (hasPermissions(permissions)) {
321
- onBluetoothPermissionGranted()
322
- } else {
323
- val rationaleNeeded = permissions.any {
324
- ActivityCompat.shouldShowRequestPermissionRationale(this, it)
299
+
300
+ private fun requestLocationPermission() {
301
+ val permissions = mutableListOf(*PermissionUtils.LOCAL_PERMISSION)
302
+
303
+ val permissionArray = permissions.toTypedArray()
304
+
305
+ if (hasPermissions(permissionArray)) {
306
+ onLocationPermissionGranted()
307
+ } else {
308
+ val rationaleNeeded = permissionArray.any {
309
+ ActivityCompat.shouldShowRequestPermissionRationale(this, it)
310
+ }
311
+
312
+ if (rationaleNeeded) {
313
+ showPermissionRationale(
314
+ title = "Location Permission Needed",
315
+ message = "We need location access for Bluetooth scanning.",
316
+ onProceed = {
317
+ ActivityCompat.requestPermissions(
318
+ this,
319
+ permissionArray,
320
+ LOCATION_REQUEST_CODE
321
+ )
322
+ }
323
+ )
324
+ } else {
325
+ ActivityCompat.requestPermissions(this, permissionArray, LOCATION_REQUEST_CODE)
326
+ }
325
327
  }
328
+ }
326
329
 
327
- if (rationaleNeeded) {
328
- showPermissionRationale(
329
- title = "Bluetooth Permission Needed",
330
- message = "We need Bluetooth access to connect your device.",
331
- onProceed = {
332
- ActivityCompat.requestPermissions(this, permissions, BLUETOOTH_REQUEST_CODE)
330
+ @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
331
+ private fun requestBluetoothPermission() {
332
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
333
+ val permissions = PermissionUtils.BLUETOOTH_S
334
+
335
+ if (hasPermissions(permissions)) {
336
+ onBluetoothPermissionGranted()
337
+ } else {
338
+ val rationaleNeeded = permissions.any {
339
+ ActivityCompat.shouldShowRequestPermissionRationale(this, it)
340
+ }
341
+
342
+ if (rationaleNeeded) {
343
+ showPermissionRationale(
344
+ title = "Bluetooth Permission Needed",
345
+ message = "We need Bluetooth access to connect your device.",
346
+ onProceed = {
347
+ ActivityCompat.requestPermissions(
348
+ this,
349
+ permissions,
350
+ BLUETOOTH_REQUEST_CODE
351
+ )
352
+ }
353
+ )
354
+ } else {
355
+ ActivityCompat.requestPermissions(this, permissions, BLUETOOTH_REQUEST_CODE)
356
+ }
333
357
  }
334
- )
335
358
  } else {
336
- ActivityCompat.requestPermissions(this, permissions, BLUETOOTH_REQUEST_CODE)
359
+ onBluetoothPermissionGranted()
337
360
  }
338
- }
339
- } else {
340
- onBluetoothPermissionGranted()
341
361
  }
342
- }
343
362
 
344
- private fun showPermissionRationale(title: String, message: String, onProceed: () -> Unit) {
345
- AlertDialog.Builder(this)
346
- .setTitle(title)
347
- .setMessage(message)
348
- .setPositiveButton("OK") { _, _ -> onProceed() }
349
- .setNegativeButton("Cancel", null)
350
- .show()
351
- }
363
+ private fun showPermissionRationale(title: String, message: String, onProceed: () -> Unit) {
364
+ AlertDialog.Builder(this)
365
+ .setTitle(title)
366
+ .setMessage(message)
367
+ .setPositiveButton("OK") { _, _ -> onProceed() }
368
+ .setNegativeButton("Cancel", null)
369
+ .show()
370
+ }
352
371
 
353
372
 
354
- private fun hasPermissions(permissions: Array<String>): Boolean {
355
- return permissions.all {
356
- ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
373
+ private fun hasPermissions(permissions: Array<String>): Boolean {
374
+ return permissions.all {
375
+ ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
376
+ }
357
377
  }
358
- }
359
378
 
360
- private fun areAllPermissionsGranted(): Boolean {
361
- val requiredPermissions = mutableListOf<String>()
379
+ private fun areAllPermissionsGranted(): Boolean {
380
+ val requiredPermissions = mutableListOf<String>()
362
381
 
363
- // Add Bluetooth permissions
364
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
365
- requiredPermissions.addAll(PermissionUtils.BLUETOOTH_S)
366
- }
382
+ // Add Bluetooth permissions
383
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
384
+ requiredPermissions.addAll(PermissionUtils.BLUETOOTH_S)
385
+ }
367
386
 
368
- // Add location permissions
369
- requiredPermissions.addAll(PermissionUtils.LOCAL_PERMISSION)
387
+ // Add location permissions
388
+ requiredPermissions.addAll(PermissionUtils.LOCAL_PERMISSION)
370
389
 
371
- return requiredPermissions.all {
372
- ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
390
+ return requiredPermissions.all {
391
+ ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
392
+ }
393
+ }
394
+
395
+ private fun updateProceedButtonState() {
396
+ if (areAllPermissionsGranted() && bluetoothAdapter!!.isEnabled) {
397
+ binding.commonButton.btnProceed.alpha = 1f
398
+ binding.commonButton.btnProceed.isEnabled = true
399
+ } else {
400
+ binding.commonButton.btnProceed.alpha = 0.5f
401
+ binding.commonButton.btnProceed.isEnabled = false
402
+ }
373
403
  }
374
- }
375
-
376
- private fun updateProceedButtonState() {
377
- if (areAllPermissionsGranted() && bluetoothAdapter!!.isEnabled) {
378
- binding.commonButton.btnProceed.alpha = 1f
379
- binding.commonButton.btnProceed.isEnabled = true
380
- } else {
381
- binding.commonButton.btnProceed.alpha = 0.5f
382
- binding.commonButton.btnProceed.isEnabled = false
404
+
405
+ private fun onForegroundLocationGranted() {
406
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
407
+ ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
408
+ != PackageManager.PERMISSION_GRANTED
409
+ ) {
410
+ onLocationPermissionGranted()
411
+ } else {
412
+ Log.d("Permission", "All location permissions granted")
413
+ }
383
414
  }
384
- }
385
415
 
386
- private fun onForegroundLocationGranted() {
387
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
388
- ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
389
- != PackageManager.PERMISSION_GRANTED
416
+ @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
417
+ override fun onRequestPermissionsResult(
418
+ requestCode: Int,
419
+ permissions: Array<out String>,
420
+ grantResults: IntArray
390
421
  ) {
391
- onLocationPermissionGranted()
392
- } else {
393
- Log.d("Permission", "All location permissions granted")
394
- }
395
- }
396
-
397
- @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
398
- override fun onRequestPermissionsResult(
399
- requestCode: Int,
400
- permissions: Array<out String>,
401
- grantResults: IntArray
402
- ) {
403
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
404
- val allGranted = grantResults.all { it == PackageManager.PERMISSION_GRANTED }
405
-
406
- when (requestCode) {
422
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
423
+ val allGranted = grantResults.all { it == PackageManager.PERMISSION_GRANTED }
424
+
425
+ when (requestCode) {
407
426
  // LOCATION_REQUEST_CODE -> {
408
427
  // if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
409
428
  // onForegroundLocationGranted()
@@ -412,71 +431,71 @@ class PermissionActivity : BasePermissionActivity() {
412
431
  // }
413
432
  // }
414
433
 
415
- LOCATION_REQUEST_CODE -> {
416
- if (allGranted) {
417
- onLocationPermissionGranted()
418
- }
419
- }
434
+ LOCATION_REQUEST_CODE -> {
435
+ if (allGranted) {
436
+ onLocationPermissionGranted()
437
+ }
438
+ }
420
439
 
421
- BLUETOOTH_REQUEST_CODE -> {
422
- if (allGranted) {
423
- onBluetoothPermissionGranted()
440
+ BLUETOOTH_REQUEST_CODE -> {
441
+ if (allGranted) {
442
+ onBluetoothPermissionGranted()
443
+ }
444
+ }
424
445
  }
425
- }
446
+ updateProceedButtonState()
426
447
  }
427
- updateProceedButtonState()
428
- }
429
-
430
- private fun onLocationPermissionGranted() {
431
- updateProceedButtonState()
432
- // Start location-based action
433
- }
434
-
435
- @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
436
- private fun onBluetoothPermissionGranted() {
437
- if (bluetoothAdapter == null) {
438
- Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show()
439
- } else {
440
- if (!bluetoothAdapter!!.isEnabled) {
441
- val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
442
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
443
- }
448
+
449
+ private fun onLocationPermissionGranted() {
450
+ updateProceedButtonState()
451
+ // Start location-based action
444
452
  }
445
453
 
446
- updateProceedButtonState()
447
- // Start Bluetooth device scan or connect
448
- }
454
+ @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
455
+ private fun onBluetoothPermissionGranted() {
456
+ if (bluetoothAdapter == null) {
457
+ Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show()
458
+ } else {
459
+ if (!bluetoothAdapter!!.isEnabled) {
460
+ val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
461
+ startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
462
+ }
463
+ }
449
464
 
450
- override fun onResume() {
451
- super.onResume()
452
- updateProceedButtonState()
453
- }
465
+ updateProceedButtonState()
466
+ // Start Bluetooth device scan or connect
467
+ }
454
468
 
455
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
456
- super.onActivityResult(requestCode, resultCode, data)
457
- if (requestCode == REQUEST_ENABLE_BT) {
458
- if (resultCode == RESULT_OK) {
469
+ override fun onResume() {
470
+ super.onResume()
459
471
  updateProceedButtonState()
460
- }
461
472
  }
462
- }
473
+
474
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
475
+ super.onActivityResult(requestCode, resultCode, data)
476
+ if (requestCode == REQUEST_ENABLE_BT) {
477
+ if (resultCode == RESULT_OK) {
478
+ updateProceedButtonState()
479
+ }
480
+ }
481
+ }
463
482
  }
464
483
 
465
484
  object PermissionUtils {
466
- @RequiresApi(Build.VERSION_CODES.S)
467
- val BLUETOOTH_S = arrayOf(
468
- Manifest.permission.BLUETOOTH_ADVERTISE,
469
- Manifest.permission.BLUETOOTH_CONNECT,
470
- Manifest.permission.BLUETOOTH_SCAN
471
- )
472
-
473
- val LOCAL_PERMISSION = arrayOf(
474
- Manifest.permission.ACCESS_FINE_LOCATION,
475
- Manifest.permission.ACCESS_COARSE_LOCATION
476
- )
477
-
478
-
479
- val CAMERA_PERMISSION = arrayOf(
480
- Manifest.permission.CAMERA
481
- )
485
+ @RequiresApi(Build.VERSION_CODES.S)
486
+ val BLUETOOTH_S = arrayOf(
487
+ Manifest.permission.BLUETOOTH_ADVERTISE,
488
+ Manifest.permission.BLUETOOTH_CONNECT,
489
+ Manifest.permission.BLUETOOTH_SCAN
490
+ )
491
+
492
+ val LOCAL_PERMISSION = arrayOf(
493
+ Manifest.permission.ACCESS_FINE_LOCATION,
494
+ Manifest.permission.ACCESS_COARSE_LOCATION
495
+ )
496
+
497
+
498
+ val CAMERA_PERMISSION = arrayOf(
499
+ Manifest.permission.CAMERA
500
+ )
482
501
  }