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,8 +90,9 @@ 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 != null) {
|
||||
if (bundle.containsKey(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
|
||||
if (bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
|
||||
//gpsConf.gpsBoost = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBOOST, false)
|
||||
|
@ -102,6 +105,7 @@ class BLEMockLocationService : Service() {
|
|||
//Log.v(LOG_TAG, "Received Tasker Intent : Boost ${gpsConf.gpsBoost}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
@ -134,15 +138,24 @@ class BLEMockLocationService : Service() {
|
|||
//pBLE.registerBMP()
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||
if(intent.action == ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION) {
|
||||
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 contentIntent = PendingIntent.getActivity(
|
||||
this, 0, startIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// Create the NotificationChannel
|
||||
|
@ -153,7 +166,8 @@ class BLEMockLocationService : Service() {
|
|||
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
|
||||
val notificationManager =
|
||||
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(mChannel)
|
||||
}
|
||||
|
||||
|
@ -161,26 +175,44 @@ class BLEMockLocationService : Service() {
|
|||
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
||||
.setContentTitle("BLE GPS")
|
||||
.setContentText("BLE & Mock active")
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||
.setContentIntent(contentIntent)
|
||||
.setAutoCancel(false)
|
||||
.build()
|
||||
|
||||
pBLE = BLEProvider(application, baseContext)
|
||||
mockGPS.start()
|
||||
startForeground(ServicesConstants.NOTIFICATION_ID.FOREGROUND_MOCK_SERVICE,notification)
|
||||
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
|
||||
}
|
||||
override fun onDestroy() {
|
||||
|
|
|
@ -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