1- //
2- // BluetoothUtil.kt
3- // DocumentReader
4- //
5- // Created by Pavel Masiuk on 21.09.2023.
6- // Copyright © 2023 Regula. All rights reserved.
7- //
8- @file:SuppressLint(" MissingPermission" )
9-
10- package io.flutter.plugins.regula.documentreader.flutter_document_reader_api
1+ package com.regula.plugin.documentreader
112
123import android.Manifest.permission.ACCESS_FINE_LOCATION
134import android.Manifest.permission.BLUETOOTH_CONNECT
@@ -22,6 +13,7 @@ import android.content.pm.PackageManager.PERMISSION_GRANTED
2213import android.os.Build
2314import android.os.IBinder
2415import android.provider.Settings
16+ import android.util.Log
2517import androidx.core.content.ContextCompat.checkSelfPermission
2618import com.regula.common.ble.BLEWrapper
2719import com.regula.common.ble.BleWrapperCallback
@@ -38,45 +30,41 @@ const val INTENT_REQUEST_ENABLE_LOCATION = 196
3830const val INTENT_REQUEST_ENABLE_BLUETOOTH = 197
3931
4032@SuppressLint(" StaticFieldLeak" )
41- lateinit var bluetooth: BLEWrapper
33+ var bluetooth: BLEWrapper ? = null
4234lateinit var savedCallbackForPermissionResult: Callback
43- var deviceConnected = false
4435
4536fun connectBluetoothDevice (callback : Callback ) {
46- // return if already connected
47- if (deviceConnected) return
37+ if (bluetooth?.isConnected == true ) {
38+ Log .e(" REGULA" , " Bluetooth device already connected, returning false" )
39+ callback(false )
40+ return
41+ }
4842
49- // If some of the bluetooth permissions/settings don't match the requirements,
50- // save callback for later and request the permissions/settings.
51- // Callback will then be used in onRequestPermissionsResult for permission requests
52- // and in onActivityResult for settings change requests.
5343 if (! isBluetoothSettingsReady(activity)) {
5444 savedCallbackForPermissionResult = callback
5545 return
5646 }
5747
58- // set searching timeout
59- val timer = object : TimerTask () {
48+ val timeout = object : TimerTask () {
6049 override fun run () {
61- callback.success (false )
62- bluetooth.stopDeviceScan()
63- bluetooth.disconnect()
50+ callback(false )
51+ bluetooth? .stopDeviceScan()
52+ bluetooth? .disconnect()
6453 }
6554 }
66- Timer ().schedule(timer , SEARCHING_TIMEOUT )
55+ Timer ().schedule(timeout , SEARCHING_TIMEOUT )
6756
68- // start searching devices
6957 val bleIntent = Intent (context, RegulaBleService ::class .java)
7058 context.startService(bleIntent)
7159 context.bindService(bleIntent, object : ServiceConnection {
7260 override fun onServiceConnected (name : ComponentName , service : IBinder ) {
7361 bluetooth = (service as RegulaBleService .LocalBinder ).service.bleManager
74- bluetooth.addCallback(object : BleWrapperCallback () {
62+ if (bluetooth!! .isConnected) callback(true )
63+ else bluetooth!! .addCallback(object : BleWrapperCallback () {
7564 override fun onDeviceReady () {
76- timer.cancel()
77- bluetooth.removeCallback(this )
78- deviceConnected = true
79- callback.success(true )
65+ timeout.cancel()
66+ bluetooth!! .removeCallback(this )
67+ callback(true )
8068 }
8169 })
8270 }
@@ -92,7 +80,7 @@ fun onRequestPermissionsResult(
9280): Boolean {
9381 if (requestCode != BLE_ACCESS_PERMISSION || permissions.isEmpty()) return false
9482 if (grantResults.isEmpty() || grantResults[0 ] != PERMISSION_GRANTED ) {
95- savedCallbackForPermissionResult.success (false )
83+ savedCallbackForPermissionResult(false )
9684 return true
9785 }
9886 connectBluetoothDevice(savedCallbackForPermissionResult)
@@ -109,7 +97,7 @@ fun onActivityResult(requestCode: Int, rc: Int, @Suppress("UNUSED_PARAMETER") da
10997 if (resultCode == Activity .RESULT_OK )
11098 connectBluetoothDevice(savedCallbackForPermissionResult)
11199 else
112- savedCallbackForPermissionResult.success (false )
100+ savedCallbackForPermissionResult(false )
113101 return true
114102 }
115103 return false
0 commit comments