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_MOCK_LOCATION"/>
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<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"/>
|
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|
|
@ -26,6 +26,8 @@ class BLEMockLocationService : Service() {
|
||||||
private val LOG_TAG = "BLEMockLocationService"
|
private val LOG_TAG = "BLEMockLocationService"
|
||||||
private val CHANNEL_ID = "NotifID"
|
private val CHANNEL_ID = "NotifID"
|
||||||
public val ACTIVATION_CHANGE = 1
|
public val ACTIVATION_CHANGE = 1
|
||||||
|
private var wakeLock: PowerManager.WakeLock? = null
|
||||||
|
private var isServiceStarted = false
|
||||||
private lateinit var pBLE: BLEProvider
|
private lateinit var pBLE: BLEProvider
|
||||||
private lateinit var mockGPS: MockLocationProvider
|
private lateinit var mockGPS: MockLocationProvider
|
||||||
private var locationReceived = false
|
private var locationReceived = false
|
||||||
|
@ -88,18 +90,20 @@ class BLEMockLocationService : Service() {
|
||||||
currentTemperature = intent.getFloatExtra(ServicesConstants.BROADCAST_KEY.KEYTEMP, -1.0f)
|
currentTemperature = intent.getFloatExtra(ServicesConstants.BROADCAST_KEY.KEYTEMP, -1.0f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun taskerChanged(bundle: Bundle) {
|
fun taskerChanged(bundle: Bundle?) {
|
||||||
Log.v(LOG_TAG, "taskerChanged")
|
Log.v(LOG_TAG, "taskerChanged")
|
||||||
if(bundle.containsKey(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
|
if(bundle != null) {
|
||||||
if (bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
|
if (bundle.containsKey(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
|
||||||
//gpsConf.gpsBoost = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBOOST, false)
|
if (bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)) {
|
||||||
sendConfBLE(2)
|
//gpsConf.gpsBoost = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBOOST, false)
|
||||||
} else {
|
sendConfBLE(2)
|
||||||
sendConfBLE(1)
|
} else {
|
||||||
}
|
sendConfBLE(1)
|
||||||
//gpsConf.gpsBoost = bundle.getBoolean(ServicesConstants.BROADCAST_KEY.KEYBOOST)
|
}
|
||||||
|
//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()
|
//pBLE.registerBMP()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if(intent.action == ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION) {
|
if(intent != null) {
|
||||||
Log.i(LOG_TAG, "Received Start Foreground Intent ")
|
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)
|
val startIntent = Intent(this, MainActivity::class.java)
|
||||||
startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
|
startIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
|
||||||
val contentIntent = PendingIntent.getActivity(this, 0, startIntent,
|
val contentIntent = PendingIntent.getActivity(
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT)
|
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) {
|
var notification = NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
// Create the NotificationChannel
|
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
||||||
val name = "ChannelName"
|
.setContentTitle("BLE GPS")
|
||||||
val descriptionText = "ChannelDescription"
|
.setContentText("BLE & Mock active")
|
||||||
val importance = NotificationManager.IMPORTANCE_DEFAULT
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
|
.setContentIntent(contentIntent)
|
||||||
mChannel.description = descriptionText
|
.setAutoCancel(false)
|
||||||
// Register the channel with the system; you can't change the importance
|
.build()
|
||||||
// or other notification behaviors after this
|
|
||||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
pBLE = BLEProvider(application, baseContext)
|
||||||
notificationManager.createNotificationChannel(mChannel)
|
mockGPS.start()
|
||||||
}
|
startForeground(
|
||||||
|
ServicesConstants.NOTIFICATION_ID.FOREGROUND_MOCK_SERVICE,
|
||||||
var notification = NotificationCompat.Builder(this, CHANNEL_ID)
|
notification
|
||||||
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
)
|
||||||
.setContentTitle("BLE GPS")
|
} else if (intent.action == ServicesConstants.MOCKSERVICEACTION.STOPFOREGROUND_ACTION) {
|
||||||
.setContentText("BLE & Mock active")
|
Log.i(LOG_TAG, "Received Stop Foreground Intent ")
|
||||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
try {
|
||||||
.setContentIntent(contentIntent)
|
wakeLock?.let {
|
||||||
.setAutoCancel(false)
|
if (it.isHeld) {
|
||||||
.build()
|
it.release()
|
||||||
|
}
|
||||||
pBLE = BLEProvider(application, baseContext)
|
}
|
||||||
mockGPS.start()
|
pBLE.disconnectBLE()
|
||||||
startForeground(ServicesConstants.NOTIFICATION_ID.FOREGROUND_MOCK_SERVICE,notification)
|
mockGPS.shutdown()
|
||||||
} else if(intent.action == ServicesConstants.MOCKSERVICEACTION.STOPFOREGROUND_ACTION) {
|
stopForeground(true)
|
||||||
Log.i(LOG_TAG, "Received Stop Foreground Intent ")
|
stopSelf()
|
||||||
pBLE.disconnectBLE()
|
} catch (e: Exception) {
|
||||||
mockGPS.shutdown()
|
Log.w(LOG_TAG, "Service stopped without being started: ${e.message}")
|
||||||
stopForeground(true)
|
}
|
||||||
stopSelf()
|
|
||||||
} else if(intent.action == ServicesConstants.MOCKSERVICEACTION.TASKER_ACTION){
|
isServiceStarted = false
|
||||||
Log.i(LOG_TAG, "intent action : ${intent.action}")
|
|
||||||
if(this::pBLE.isInitialized) {
|
} else if (intent.action == ServicesConstants.MOCKSERVICEACTION.TASKER_ACTION) {
|
||||||
taskerChanged(intent.extras)
|
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
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
var currentPressure: Int = -1
|
var currentPressure: Int = -1
|
||||||
var currentTemperature: Float = -1.0f
|
var currentTemperature: Float = -1.0f
|
||||||
var BTConnected = false
|
var BTConnected = false
|
||||||
|
var BTConnecting = false
|
||||||
var lastLocation: Location = Location("LastLocation")
|
var lastLocation: Location = Location("LastLocation")
|
||||||
|
|
||||||
var resumeActivityState: Boolean = true;
|
var resumeActivityState: Boolean = true;
|
||||||
|
@ -63,6 +64,11 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
unbindService(mServiceConnection)
|
unbindService(mServiceConnection)
|
||||||
mServiceBound = false
|
mServiceBound = false
|
||||||
|
text01.text = "BT Disconnected"
|
||||||
|
buttonconnectbt.text = "Connect"
|
||||||
|
editbtaddr.isFocusable = true
|
||||||
|
editbtaddr.isFocusableInTouchMode = true
|
||||||
|
editbtaddr.isClickable = true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun filter01changed(intent: Intent)
|
fun filter01changed(intent: Intent)
|
||||||
|
@ -89,13 +95,11 @@ class MainActivity : AppCompatActivity() {
|
||||||
val connected = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBTSTATUS,false)
|
val connected = intent.getBooleanExtra(ServicesConstants.BROADCAST_KEY.KEYBTSTATUS,false)
|
||||||
if(connected) {
|
if(connected) {
|
||||||
BTConnected=true
|
BTConnected=true
|
||||||
|
BTConnecting=false
|
||||||
if(resumeActivityState == true) { sendGPSModeToBLEService(3) }
|
if(resumeActivityState == true) { sendGPSModeToBLEService(3) }
|
||||||
text01.text = "BT Connected"
|
text01.text = "BT Connected"
|
||||||
buttonconnectbt.text = "Disconnect"
|
|
||||||
Log.v(LOG_TAG, "BT Connected Broadcast received")
|
Log.v(LOG_TAG, "BT Connected Broadcast received")
|
||||||
} else {
|
} else {
|
||||||
text01.text = "BT Disconnected"
|
|
||||||
buttonconnectbt.text = "Connect"
|
|
||||||
Log.v(LOG_TAG, "BT Disconnected Broadcast received")
|
Log.v(LOG_TAG, "BT Disconnected Broadcast received")
|
||||||
BTConnected=false
|
BTConnected=false
|
||||||
}
|
}
|
||||||
|
@ -204,7 +208,8 @@ class MainActivity : AppCompatActivity() {
|
||||||
BleManager.getInstance().init(application)
|
BleManager.getInstance().init(application)
|
||||||
BleManager.getInstance()
|
BleManager.getInstance()
|
||||||
.enableLog(true)
|
.enableLog(true)
|
||||||
.setReConnectCount(1, 5000)
|
.setReConnectCount(0, 0)
|
||||||
|
//.setReConnectCount(1, 5000)
|
||||||
|
|
||||||
BleManager.getInstance().operateTimeout = 5000
|
BleManager.getInstance().operateTimeout = 5000
|
||||||
|
|
||||||
|
@ -281,11 +286,17 @@ class MainActivity : AppCompatActivity() {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
buttonconnectbt.setOnClickListener {
|
buttonconnectbt.setOnClickListener {
|
||||||
if(BTConnected == false && editbtaddr.text.toString() != "") {
|
if(BTConnected == false && BTConnecting == false && editbtaddr.text.toString() != "") {
|
||||||
sharedPreferences
|
sharedPreferences
|
||||||
.edit()
|
.edit()
|
||||||
.putString("macaddr", editbtaddr.text.toString())
|
.putString("macaddr", editbtaddr.text.toString())
|
||||||
.apply()
|
.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 ->
|
Intent(this, BLEMockLocationService::class.java).also { intent ->
|
||||||
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE)
|
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE)
|
||||||
intent.action = ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION
|
intent.action = ServicesConstants.MOCKSERVICEACTION.STARTFOREGROUND_ACTION
|
||||||
|
|
Loading…
Reference in New Issue