mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-19 07:24:22 +01:00
Add HRS service
This commit is contained in:
@@ -2,7 +2,7 @@ apply from: rootProject.file("library.gradle")
|
||||
apply plugin: 'kotlin-parcelize'
|
||||
|
||||
dependencies {
|
||||
implementation project(":feature_scanner")
|
||||
implementation project(":lib_theme")
|
||||
|
||||
implementation libs.nordic.ble.common
|
||||
implementation libs.nordic.log
|
||||
|
||||
@@ -2,4 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="no.nordicsemi.android.service">
|
||||
|
||||
<uses-permission android:name="android.permission.BLUETOOTH" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||
|
||||
</manifest>
|
||||
@@ -42,7 +42,6 @@ import no.nordicsemi.android.ble.BleManagerCallbacks
|
||||
import no.nordicsemi.android.ble.utils.ILogger
|
||||
import no.nordicsemi.android.log.ILogSession
|
||||
import no.nordicsemi.android.log.Logger
|
||||
import no.nordicsemi.android.scanner.tools.SelectedBluetoothDeviceHolder
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -68,7 +67,7 @@ abstract class BleProfileService : LifecycleService(), BleManagerCallbacks {
|
||||
* @return bluetooth device
|
||||
*/
|
||||
protected val bluetoothDevice: BluetoothDevice by lazy {
|
||||
bluetoothDeviceHolder.device ?: throw UnsupportedOperationException(
|
||||
bluetoothDeviceHolder.device ?: throw IllegalArgumentException(
|
||||
"No device address at EXTRA_DEVICE_ADDRESS key"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -22,16 +22,29 @@
|
||||
package no.nordicsemi.android.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
private const val CHANNEL_ID = "FOREGROUND_BLE_SERVICE"
|
||||
|
||||
abstract class ForegroundBleService<T : BatteryManager<out BatteryManagerCallbacks>> : BleProfileService() {
|
||||
|
||||
protected abstract val manager: T
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
val result = super.onStartCommand(intent, flags, startId)
|
||||
startForegroundService()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
||||
cancelNotification()
|
||||
stopForegroundService()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
@@ -87,24 +100,30 @@ abstract class ForegroundBleService<T : BatteryManager<out BatteryManagerCallbac
|
||||
* @param defaults
|
||||
*/
|
||||
private fun createNotification(messageResId: Int, defaults: Int): Notification {
|
||||
TODO()
|
||||
// final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||
// parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
// final Intent targetIntent = new Intent(this, CSCActivity.class);
|
||||
//
|
||||
// final Intent disconnect = new Intent(ACTION_DISCONNECT);
|
||||
// final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
//
|
||||
// // both activities above have launchMode="singleTask" in the AndroidManifest.xml file, so if the task is already running, it will be resumed
|
||||
// final PendingIntent pendingIntent = PendingIntent.getActivities(this, OPEN_ACTIVITY_REQ, new Intent[]{parentIntent, targetIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
// final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
|
||||
// builder.setContentIntent(pendingIntent);
|
||||
// builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
||||
// builder.setSmallIcon(R.drawable.ic_stat_notify_csc);
|
||||
// builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
||||
// builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.csc_notification_action_disconnect), disconnectAction));
|
||||
//
|
||||
// return builder.build();
|
||||
createNotificationChannel(CHANNEL_ID)
|
||||
|
||||
val intent: Intent? = packageManager.getLaunchIntentForPackage(packageName)
|
||||
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
||||
|
||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle(getString(R.string.app_name))
|
||||
.setContentText(getString(messageResId, manager.bluetoothDevice?.name ?: "Device"))
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun createNotificationChannel(channelName: String) {
|
||||
val channel = NotificationChannel(
|
||||
channelName,
|
||||
getString(R.string.channel_connected_devices_title),
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
)
|
||||
channel.description = getString(R.string.channel_connected_devices_description)
|
||||
channel.setShowBadge(false)
|
||||
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package no.nordicsemi.android.service
|
||||
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.companion.CompanionDeviceManager
|
||||
import android.content.Context
|
||||
|
||||
class SelectedBluetoothDeviceHolder constructor(
|
||||
private val context: Context,
|
||||
private val bluetoothAdapter: BluetoothAdapter?
|
||||
) {
|
||||
|
||||
val device: BluetoothDevice?
|
||||
get() {
|
||||
val deviceManager = context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as CompanionDeviceManager
|
||||
return deviceManager.associations.firstOrNull()?.let { bluetoothAdapter?.getRemoteDevice(it) }
|
||||
}
|
||||
|
||||
fun forgetDevice() {
|
||||
device?.let {
|
||||
val deviceManager = context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as CompanionDeviceManager
|
||||
deviceManager.disassociate(it.address)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,6 @@
|
||||
<string name="csc_bonding">Bonding with the device…</string>
|
||||
<string name="csc_bonded">The device is now bonded.</string>
|
||||
<string name="csc_notification_connected_message">%s is connected.</string>
|
||||
<string name="channel_connected_devices_title">Background connections</string>
|
||||
<string name="channel_connected_devices_description">Shows a notification when a device is connected in background.</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user