Skip to content

Commit

Permalink
Merge pull request #32 from BIDMCDigitalPsychiatry/increase_wifi_scan…
Browse files Browse the repository at this point in the history
…_repeat_interval

Run time permission issue fixed for Bluetooth
  • Loading branch information
ZCOEngineer authored Dec 18, 2023
2 parents b8a868a + 7c4dbb7 commit d1e3a79
Showing 1 changed file with 37 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import android.content.pm.PackageManager
import android.net.wifi.ScanResult
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import digital.lamp.lamp_kotlin.sensor_core.utils.LampConstants
Expand Down Expand Up @@ -55,24 +55,19 @@ class WiFi : Service() {
bluetoothBackgroundService!!,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

// Start discovery
if (ContextCompat.checkSelfPermission(
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.BLUETOOTH_SCAN
) != PackageManager.PERMISSION_GRANTED
) == PackageManager.PERMISSION_GRANTED
) {

}else{
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter != null && bluetoothAdapter.isDiscovering) {
bluetoothAdapter.cancelDiscovery()
}

// Start discovery
val filter = IntentFilter()
filter.addAction(BluetoothDevice.ACTION_FOUND)
registerReceiver(bluetoothMonitor, filter)
val intentFilter = IntentFilter()
intentFilter.addAction(BluetoothDevice.ACTION_FOUND)
registerReceiver(bluetoothMonitor, intentFilter)
bluetoothAdapter?.startDiscovery()
}
}
Expand Down Expand Up @@ -100,26 +95,25 @@ class WiFi : Service() {
alarmManager?.cancel(wifiScan)
alarmManager?.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 1000,
System.currentTimeMillis(),
frequency!! * 1000,
wifiScan
)
}else{
alarmManager?.cancel(wifiScan)
alarmManager?.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 1000,
LampConstants.FREQUENCY_WIFI * 1000.toLong(),
System.currentTimeMillis(),
(LampConstants.FREQUENCY_WIFI * 1000).toLong(),
wifiScan
)

}
} else {
alarmManager?.cancel(wifiScan)
alarmManager?.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 1000,
LampConstants.FREQUENCY_WIFI * 1000.toLong(),
System.currentTimeMillis(),
(LampConstants.FREQUENCY_WIFI * 1000).toLong(),
wifiScan
)

Expand All @@ -135,7 +129,7 @@ class WiFi : Service() {

private fun scheduleBluetoothAlarm() {
val alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val startTime = System.currentTimeMillis() + 1000
val startTime = System.currentTimeMillis()
if (frequency != null) {
if (frequency!! >= LampConstants.FREQUENCY_WIFI) {
alarmMgr.cancel(bluetoothScan)
Expand All @@ -150,7 +144,7 @@ class WiFi : Service() {
alarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
startTime,
LampConstants.FREQUENCY_WIFI * 1000.toLong(),
(LampConstants.FREQUENCY_WIFI * 1000).toLong(),
bluetoothScan
)
}
Expand All @@ -160,7 +154,7 @@ class WiFi : Service() {
alarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
startTime,
LampConstants.FREQUENCY_WIFI * 1000.toLong(),
(LampConstants.FREQUENCY_WIFI * 1000).toLong(),
bluetoothScan
)
}
Expand Down Expand Up @@ -198,30 +192,17 @@ class WiFi : Service() {
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled) {
when (intent?.action) {
BluetoothDevice.ACTION_FOUND -> {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.BLUETOOTH_CONNECT
) != PackageManager.PERMISSION_GRANTED
) {


}else{
val device =
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
device?.let {
// Do something with the discovered Bluetooth device
// For example, display device information in a list
// or add it to a collection
Log.e("device", it.address)
val rowData = ContentValues()
rowData.put(TIMESTAMP, System.currentTimeMillis())
rowData.put(BLUETOOTH_ADDRESS, device.address)
rowData.put(BLUETOOTH_NAME, device.name)
rowData.put(BLUETOOTH_RSSI, device.type)
if (sensorObserver != null) sensorObserver!!.onBluetoothDetected(
rowData
)
}
val device =
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
device?.let {
// Do something with the discovered Bluetooth device
val rowData = ContentValues()
rowData.put(TIMESTAMP, System.currentTimeMillis())
rowData.put(BLUETOOTH_ADDRESS, device.address)
if (sensorObserver != null) sensorObserver!!.onBluetoothDetected(
rowData
)
}
}
}
Expand All @@ -233,6 +214,12 @@ class WiFi : Service() {
class BluetoothBackgroundService : IntentService(TAG) {
private val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

override fun onCreate() {
super.onCreate()
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
registerReceiver(bluetoothReceiver, filter)
}

private val bluetoothReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action
Expand All @@ -250,12 +237,9 @@ class WiFi : Service() {
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
device?.let {

Log.e("device", it.address)
val rowData = ContentValues()
rowData.put(TIMESTAMP, System.currentTimeMillis())
rowData.put(BLUETOOTH_ADDRESS, device.address)
rowData.put(BLUETOOTH_NAME, device.type)
/*rowData.put(BLUETOOTH_RSSI, device.type)*/
if (sensorObserver != null) sensorObserver!!.onBluetoothDetected(
rowData
)
Expand All @@ -273,8 +257,7 @@ class WiFi : Service() {
}

// Register for broadcasts when a device is discovered
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
registerReceiver(bluetoothReceiver, filter)

if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.BLUETOOTH_SCAN
Expand All @@ -292,7 +275,10 @@ class WiFi : Service() {
bluetoothAdapter.cancelDiscovery()
}

// Unregister the receiver
}

override fun onDestroy() {
super.onDestroy()
unregisterReceiver(bluetoothReceiver)
}
}
Expand Down Expand Up @@ -343,6 +329,7 @@ class WiFi : Service() {
rowData.put(SECURITY, ap.capabilities)
rowData.put(FREQUENCY, ap.frequency)
rowData.put(RSSI, ap.level)
Log.e("wifi",ap.BSSID)
if (sensorObserver != null) sensorObserver!!.onWiFiAPDetected(rowData)
}
if (Lamp.DEBUG) Log.d(TAG, ACTION_LAMP_WIFI_SCAN_ENDED)
Expand Down

0 comments on commit d1e3a79

Please sign in to comment.