Stability on BT connection and ForegroundService
This commit is contained in:
		
							parent
							
								
									12d8046c5c
								
							
						
					
					
						commit
						bcb91d5e0c
					
				| 
						 | 
				
			
			@ -8,6 +8,8 @@
 | 
			
		|||
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
 | 
			
		||||
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 | 
			
		||||
    <uses-permission android:name="android.permission.INTERNET" />
 | 
			
		||||
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 | 
			
		||||
    <uses-permission android:name="android.permission.WAKE_LOCK" />
 | 
			
		||||
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
 | 
			
		||||
    <application
 | 
			
		||||
        android:allowBackup="true"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,8 @@ class BLEMockLocationService : Service() {
 | 
			
		|||
    private val LOG_TAG = "BLEMockLocationService"
 | 
			
		||||
    private val CHANNEL_ID = "NotifID"
 | 
			
		||||
    public val ACTIVATION_CHANGE = 1
 | 
			
		||||
    private var wakeLock: PowerManager.WakeLock? = null
 | 
			
		||||
    private var isServiceStarted = false
 | 
			
		||||
    private lateinit var pBLE: BLEProvider
 | 
			
		||||
    private lateinit var mockGPS: MockLocationProvider
 | 
			
		||||
    private var locationReceived = false
 | 
			
		||||
| 
						 | 
				
			
			@ -88,18 +90,20 @@ class BLEMockLocationService : Service() {
 | 
			
		|||
        currentTemperature = intent.getFloatExtra(ServicesConstants.BROADCAST_KEY.KEYTEMP, -1.0f)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun taskerChanged(bundle: Bundle) {
 | 
			
		||||
    fun taskerChanged(bundle: Bundle?) {
 | 
			
		||||
        Log.v(LOG_TAG, "taskerChanged")
 | 
			
		||||
        if(bundle.containsKey(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
 | 
			
		||||
            if (bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
 | 
			
		||||
                //gpsConf.gpsBoost = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBOOST, false)
 | 
			
		||||
                sendConfBLE(2)
 | 
			
		||||
            } else {
 | 
			
		||||
                sendConfBLE(1)
 | 
			
		||||
            }
 | 
			
		||||
            //gpsConf.gpsBoost = bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)
 | 
			
		||||
        if(bundle != null) {
 | 
			
		||||
            if (bundle.containsKey(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
 | 
			
		||||
                if (bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
 | 
			
		||||
                    //gpsConf.gpsBoost = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBOOST, false)
 | 
			
		||||
                    sendConfBLE(2)
 | 
			
		||||
                } else {
 | 
			
		||||
                    sendConfBLE(1)
 | 
			
		||||
                }
 | 
			
		||||
                //gpsConf.gpsBoost = bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)
 | 
			
		||||
 | 
			
		||||
            //Log.v(LOG_TAG, "Received Tasker Intent : Boost ${gpsConf.gpsBoost}")
 | 
			
		||||
                //Log.v(LOG_TAG, "Received Tasker Intent : Boost ${gpsConf.gpsBoost}")
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -134,52 +138,80 @@ class BLEMockLocationService : Service() {
 | 
			
		|||
        //pBLE.registerBMP()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
 | 
			
		||||
        if(intent.action == ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION) {
 | 
			
		||||
            Log.i(LOG_TAG, "Received Start Foreground Intent ")
 | 
			
		||||
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
 | 
			
		||||
        if(intent != null) {
 | 
			
		||||
            if (intent.action == ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION && isServiceStarted == false) {
 | 
			
		||||
                Log.i(LOG_TAG, "Received Start Foreground Intent ")
 | 
			
		||||
                isServiceStarted = true
 | 
			
		||||
                wakeLock =
 | 
			
		||||
                    (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
 | 
			
		||||
                        newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EndlessService::lock").apply {
 | 
			
		||||
                            acquire()
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
            val startIntent = Intent(this, MainActivity::class.java)
 | 
			
		||||
            startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
 | 
			
		||||
            val contentIntent = PendingIntent.getActivity(this, 0, startIntent,
 | 
			
		||||
                PendingIntent.FLAG_UPDATE_CURRENT)
 | 
			
		||||
                val startIntent = Intent(this, MainActivity::class.java)
 | 
			
		||||
                startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
 | 
			
		||||
                val contentIntent = PendingIntent.getActivity(
 | 
			
		||||
                    this, 0, startIntent,
 | 
			
		||||
                    PendingIntent.FLAG_UPDATE_CURRENT
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
                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)
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            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
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue