From bcb91d5e0cf8f425d92d0c1dc797169b3398706d Mon Sep 17 00:00:00 2001 From: Nigreon Date: Sat, 22 Feb 2025 23:44:47 +0100 Subject: [PATCH] Stability on BT connection and ForegroundService --- app/src/main/AndroidManifest.xml | 2 + .../nigreon/blegps/BLEMockLocationService.kt | 136 +++++++++++------- .../java/net/nigreon/blegps/MainActivity.kt | 21 ++- 3 files changed, 102 insertions(+), 57 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dbebd29..3fca3e8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,8 @@ + + = Build.VERSION_CODES.O) { + // Create the NotificationChannel + val name = "ChannelName" + val descriptionText = "ChannelDescription" + val importance = NotificationManager.IMPORTANCE_DEFAULT + val mChannel = NotificationChannel(CHANNEL_ID, name, importance) + mChannel.description = descriptionText + // Register the channel with the system; you can't change the importance + // or other notification behaviors after this + val notificationManager = + getSystemService(NOTIFICATION_SERVICE) as NotificationManager + notificationManager.createNotificationChannel(mChannel) + } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // Create the NotificationChannel - val name = "ChannelName" - val descriptionText = "ChannelDescription" - val importance = NotificationManager.IMPORTANCE_DEFAULT - val mChannel = NotificationChannel(CHANNEL_ID, name, importance) - mChannel.description = descriptionText - // Register the channel with the system; you can't change the importance - // or other notification behaviors after this - val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - notificationManager.createNotificationChannel(mChannel) - } - - var notification = NotificationCompat.Builder(this, CHANNEL_ID) - .setSmallIcon(R.drawable.ic_launcher_foreground) - .setContentTitle("BLE GPS") - .setContentText("BLE & Mock active") - .setPriority(NotificationCompat.PRIORITY_DEFAULT) - .setContentIntent(contentIntent) - .setAutoCancel(false) - .build() - - pBLE = BLEProvider(application, baseContext) - mockGPS.start() - startForeground(ServicesConstants.NOTIFICATION_ID.FOREGROUND_MOCK_SERVICE,notification) - } else if(intent.action == ServicesConstants.MOCKSERVICEACTION.STOPFOREGROUND_ACTION) { - Log.i(LOG_TAG, "Received Stop Foreground Intent ") - pBLE.disconnectBLE() - mockGPS.shutdown() - stopForeground(true) - stopSelf() - } else if(intent.action == ServicesConstants.MOCKSERVICEACTION.TASKER_ACTION){ - Log.i(LOG_TAG, "intent action : ${intent.action}") - if(this::pBLE.isInitialized) { - taskerChanged(intent.extras) + var notification = NotificationCompat.Builder(this, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_launcher_foreground) + .setContentTitle("BLE GPS") + .setContentText("BLE & Mock active") + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setContentIntent(contentIntent) + .setAutoCancel(false) + .build() + + pBLE = BLEProvider(application, baseContext) + mockGPS.start() + startForeground( + ServicesConstants.NOTIFICATION_ID.FOREGROUND_MOCK_SERVICE, + notification + ) + } else if (intent.action == ServicesConstants.MOCKSERVICEACTION.STOPFOREGROUND_ACTION) { + Log.i(LOG_TAG, "Received Stop Foreground Intent ") + try { + wakeLock?.let { + if (it.isHeld) { + it.release() + } + } + pBLE.disconnectBLE() + mockGPS.shutdown() + stopForeground(true) + stopSelf() + } catch (e: Exception) { + Log.w(LOG_TAG, "Service stopped without being started: ${e.message}") + } + + isServiceStarted = false + + } else if (intent.action == ServicesConstants.MOCKSERVICEACTION.TASKER_ACTION) { + Log.i(LOG_TAG, "intent action : ${intent.action}") + if (this::pBLE.isInitialized) { + taskerChanged(intent.extras) + } } + } else { + Log.i(LOG_TAG, "Foreground Service Intent null") } return START_STICKY } diff --git a/app/src/main/java/net/nigreon/blegps/MainActivity.kt b/app/src/main/java/net/nigreon/blegps/MainActivity.kt index 1337892..80f2f5c 100644 --- a/app/src/main/java/net/nigreon/blegps/MainActivity.kt +++ b/app/src/main/java/net/nigreon/blegps/MainActivity.kt @@ -38,6 +38,7 @@ class MainActivity : AppCompatActivity() { var currentPressure: Int = -1 var currentTemperature: Float = -1.0f var BTConnected = false + var BTConnecting = false var lastLocation: Location = Location("LastLocation") var resumeActivityState: Boolean = true; @@ -63,6 +64,11 @@ class MainActivity : AppCompatActivity() { } unbindService(mServiceConnection) mServiceBound = false + text01.text = "BT Disconnected" + buttonconnectbt.text = "Connect" + editbtaddr.isFocusable = true + editbtaddr.isFocusableInTouchMode = true + editbtaddr.isClickable = true } fun filter01changed(intent: Intent) @@ -89,13 +95,11 @@ class MainActivity : AppCompatActivity() { val connected = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBTSTATUS,false) if(connected) { BTConnected=true + BTConnecting=false if(resumeActivityState == true) { sendGPSModeToBLEService(3) } text01.text = "BT Connected" - buttonconnectbt.text = "Disconnect" Log.v(LOG_TAG, "BT Connected Broadcast received") } else { - text01.text = "BT Disconnected" - buttonconnectbt.text = "Connect" Log.v(LOG_TAG, "BT Disconnected Broadcast received") BTConnected=false } @@ -204,7 +208,8 @@ class MainActivity : AppCompatActivity() { BleManager.getInstance().init(application) BleManager.getInstance() .enableLog(true) - .setReConnectCount(1, 5000) + .setReConnectCount(0, 0) + //.setReConnectCount(1, 5000) BleManager.getInstance().operateTimeout = 5000 @@ -281,11 +286,17 @@ class MainActivity : AppCompatActivity() { }*/ buttonconnectbt.setOnClickListener { - if(BTConnected == false && editbtaddr.text.toString() != "") { + if(BTConnected == false && BTConnecting == false && editbtaddr.text.toString() != "") { sharedPreferences .edit() .putString("macaddr", editbtaddr.text.toString()) .apply() + text01.text = "Connecting ..." + buttonconnectbt.text = "Disconnect" + editbtaddr.isFocusable = false + editbtaddr.isFocusableInTouchMode = false + editbtaddr.isClickable = false + BTConnecting = true Intent(this, BLEMockLocationService::class.java).also { intent -> bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE) intent.action = ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION