mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-22 08:54:21 +01:00
start foreground service
This commit is contained in:
@@ -33,6 +33,7 @@
|
|||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
<uses-permission android:name="android.permission.NFC" />
|
<uses-permission android:name="android.permission.NFC" />
|
||||||
<uses-permission android:name="no.nordicsemi.android.LOG" />
|
<uses-permission android:name="no.nordicsemi.android.LOG" />
|
||||||
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
|
|
||||||
<uses-feature
|
<uses-feature
|
||||||
android:name="android.hardware.bluetooth_le"
|
android:name="android.hardware.bluetooth_le"
|
||||||
|
|||||||
@@ -8,11 +8,12 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.util.SparseArray;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||||
import android.util.SparseArray;
|
|
||||||
|
|
||||||
import no.nordicsemi.android.log.Logger;
|
import no.nordicsemi.android.log.Logger;
|
||||||
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
||||||
import no.nordicsemi.android.nrftoolbox.R;
|
import no.nordicsemi.android.nrftoolbox.R;
|
||||||
@@ -147,7 +148,7 @@ public class CGMService extends BleProfileService implements CGMSManagerCallback
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
||||||
cancelNotification();
|
stopForegroundService();
|
||||||
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
||||||
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
@@ -155,14 +156,40 @@ public class CGMService extends BleProfileService implements CGMSManagerCallback
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRebind() {
|
protected void onRebind() {
|
||||||
// when the activity rebinds to the service, remove the notification
|
startForegroundService();
|
||||||
cancelNotification();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onUnbind() {
|
protected void onUnbind() {
|
||||||
// when the activity closes we need to show the notification that user is connected to the sensor
|
startForegroundService();
|
||||||
createNotification(R.string.csc_notification_connected_message, 0);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void startForegroundService(){
|
||||||
|
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||||
|
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||||
|
final Notification notification = createNotification(R.string.uart_notification_connected_message, 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
} else {
|
||||||
|
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(NOTIFICATION_ID, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void stopForegroundService(){
|
||||||
|
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||||
|
// on devices running Android 8.0 (Oreo) or above
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
stopForeground(true);
|
||||||
|
} else {
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -172,7 +199,7 @@ public class CGMService extends BleProfileService implements CGMSManagerCallback
|
|||||||
* f.e. <code><string name="name">%s is connected</string></code>
|
* f.e. <code><string name="name">%s is connected</string></code>
|
||||||
* @param defaults signals that will be used to notify the user
|
* @param defaults signals that will be used to notify the user
|
||||||
*/
|
*/
|
||||||
private void createNotification(final int messageResId, final int defaults) {
|
private Notification createNotification(final int messageResId, final int defaults) {
|
||||||
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||||
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
final Intent targetIntent = new Intent(this, CGMSActivity.class);
|
final Intent targetIntent = new Intent(this, CGMSActivity.class);
|
||||||
@@ -189,9 +216,7 @@ public class CGMService extends BleProfileService implements CGMSManagerCallback
|
|||||||
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
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));
|
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.csc_notification_action_disconnect), disconnectAction));
|
||||||
|
|
||||||
final Notification notification = builder.build();
|
return builder.build();
|
||||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.notify(NOTIFICATION_ID, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,10 +30,11 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||||
|
|
||||||
import no.nordicsemi.android.log.Logger;
|
import no.nordicsemi.android.log.Logger;
|
||||||
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
||||||
import no.nordicsemi.android.nrftoolbox.R;
|
import no.nordicsemi.android.nrftoolbox.R;
|
||||||
@@ -46,11 +47,17 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
|||||||
private static final String TAG = "CSCService";
|
private static final String TAG = "CSCService";
|
||||||
|
|
||||||
public static final String BROADCAST_WHEEL_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_WHEEL_DATA";
|
public static final String BROADCAST_WHEEL_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_WHEEL_DATA";
|
||||||
/** Speed in meters per second. */
|
/**
|
||||||
|
* Speed in meters per second.
|
||||||
|
*/
|
||||||
public static final String EXTRA_SPEED = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_SPEED";
|
public static final String EXTRA_SPEED = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_SPEED";
|
||||||
/** Distance in meters. */
|
/**
|
||||||
|
* Distance in meters.
|
||||||
|
*/
|
||||||
public static final String EXTRA_DISTANCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_DISTANCE";
|
public static final String EXTRA_DISTANCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_DISTANCE";
|
||||||
/** Total distance in meters. */
|
/**
|
||||||
|
* Total distance in meters.
|
||||||
|
*/
|
||||||
public static final String EXTRA_TOTAL_DISTANCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_TOTAL_DISTANCE";
|
public static final String EXTRA_TOTAL_DISTANCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_TOTAL_DISTANCE";
|
||||||
|
|
||||||
public static final String BROADCAST_CRANK_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_CRANK_DATA";
|
public static final String BROADCAST_CRANK_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_CRANK_DATA";
|
||||||
@@ -106,8 +113,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRebind() {
|
protected void onRebind() {
|
||||||
// when the activity rebinds to the service, remove the notification
|
stopForegroundService();
|
||||||
cancelNotification();
|
|
||||||
|
|
||||||
if (isConnected()) {
|
if (isConnected()) {
|
||||||
// This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property).
|
// This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property).
|
||||||
@@ -122,9 +128,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
|||||||
// But we will still be receiving other values, if enabled.
|
// But we will still be receiving other values, if enabled.
|
||||||
if (isConnected())
|
if (isConnected())
|
||||||
mManager.disableBatteryLevelCharacteristicNotifications();
|
mManager.disableBatteryLevelCharacteristicNotifications();
|
||||||
|
startForegroundService();
|
||||||
// when the activity closes we need to show the notification that user is connected to the sensor
|
|
||||||
createNotification(R.string.csc_notification_connected_message, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -154,16 +158,42 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
|||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void startForegroundService(){
|
||||||
|
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||||
|
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||||
|
final Notification notification = createNotification(R.string.uart_notification_connected_message, 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
} else {
|
||||||
|
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(NOTIFICATION_ID, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void stopForegroundService(){
|
||||||
|
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||||
|
// on devices running Android 8.0 (Oreo) or above
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
stopForeground(true);
|
||||||
|
} else {
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the notification
|
* Creates the notification
|
||||||
*
|
*
|
||||||
* @param messageResId
|
* @param messageResId the message resource id. The message must have one String parameter,<br />
|
||||||
* the message resource id. The message must have one String parameter,<br />
|
|
||||||
* f.e. <code><string name="name">%s is connected</string></code>
|
* f.e. <code><string name="name">%s is connected</string></code>
|
||||||
* @param defaults
|
* @param defaults
|
||||||
* signals that will be used to notify the user
|
|
||||||
*/
|
*/
|
||||||
private void createNotification(final int messageResId, final int defaults) {
|
private Notification createNotification(final int messageResId, final int defaults) {
|
||||||
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||||
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
final Intent targetIntent = new Intent(this, CSCActivity.class);
|
final Intent targetIntent = new Intent(this, CSCActivity.class);
|
||||||
@@ -172,7 +202,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
|||||||
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
|
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
|
// 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 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);
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
|
||||||
builder.setContentIntent(pendingIntent);
|
builder.setContentIntent(pendingIntent);
|
||||||
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
||||||
@@ -180,9 +210,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
|||||||
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
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));
|
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.csc_notification_action_disconnect), disconnectAction));
|
||||||
|
|
||||||
final Notification notification = builder.build();
|
return builder.build();
|
||||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.notify(NOTIFICATION_ID, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
@@ -110,14 +112,12 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRebind() {
|
protected void onRebind() {
|
||||||
// when the activity rebinds to the service, remove the notification
|
stopForegroundService();
|
||||||
cancelNotification();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onUnbind() {
|
protected void onUnbind() {
|
||||||
// when the activity closes we need to show the notification that user is connected to the sensor
|
startForegroundService();
|
||||||
createNotification(R.string.hts_notification_connected_message, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -153,16 +153,42 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
|
|||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void startForegroundService(){
|
||||||
|
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||||
|
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||||
|
final Notification notification = createNotification(R.string.uart_notification_connected_message, 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
} else {
|
||||||
|
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(NOTIFICATION_ID, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void stopForegroundService(){
|
||||||
|
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||||
|
// on devices running Android 8.0 (Oreo) or above
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
stopForeground(true);
|
||||||
|
} else {
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the notification
|
* Creates the notification
|
||||||
*
|
|
||||||
* @param messageResId
|
* @param messageResId
|
||||||
* message resource id. The message must have one String parameter,<br />
|
* message resource id. The message must have one String parameter,<br />
|
||||||
* f.e. <code><string name="name">%s is connected</string></code>
|
* f.e. <code><string name="name">%s is connected</string></code>
|
||||||
* @param defaults
|
* @param defaults
|
||||||
* signals that will be used to notify the user
|
|
||||||
*/
|
*/
|
||||||
private void createNotification(final int messageResId, final int defaults) {
|
private Notification createNotification(final int messageResId, final int defaults) {
|
||||||
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||||
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
final Intent targetIntent = new Intent(this, HTSActivity.class);
|
final Intent targetIntent = new Intent(this, HTSActivity.class);
|
||||||
@@ -179,9 +205,7 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
|
|||||||
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
||||||
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.hts_notification_action_disconnect), disconnectAction));
|
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.hts_notification_action_disconnect), disconnectAction));
|
||||||
|
|
||||||
final Notification notification = builder.build();
|
return builder.build();
|
||||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.notify(NOTIFICATION_ID, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,12 +30,13 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||||
|
|
||||||
import no.nordicsemi.android.log.Logger;
|
import no.nordicsemi.android.log.Logger;
|
||||||
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
||||||
import no.nordicsemi.android.nrftoolbox.R;
|
import no.nordicsemi.android.nrftoolbox.R;
|
||||||
@@ -64,13 +65,21 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
|
|
||||||
private RSCManager mManager;
|
private RSCManager mManager;
|
||||||
|
|
||||||
/** The last value of a cadence */
|
/**
|
||||||
|
* The last value of a cadence
|
||||||
|
*/
|
||||||
private float mCadence;
|
private float mCadence;
|
||||||
/** Trip distance in cm */
|
/**
|
||||||
|
* Trip distance in cm
|
||||||
|
*/
|
||||||
private long mDistance;
|
private long mDistance;
|
||||||
/** Stride length in cm */
|
/**
|
||||||
|
* Stride length in cm
|
||||||
|
*/
|
||||||
private Integer mStrideLength;
|
private Integer mStrideLength;
|
||||||
/** Number of steps in the trip */
|
/**
|
||||||
|
* Number of steps in the trip
|
||||||
|
*/
|
||||||
private int mStepsNumber;
|
private int mStepsNumber;
|
||||||
private boolean mTaskInProgress;
|
private boolean mTaskInProgress;
|
||||||
private final Handler mHandler = new Handler();
|
private final Handler mHandler = new Handler();
|
||||||
@@ -110,7 +119,7 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
||||||
cancelNotification();
|
stopForegroundService();
|
||||||
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
||||||
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
@@ -118,8 +127,7 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRebind() {
|
protected void onRebind() {
|
||||||
// when the activity rebinds to the service, remove the notification
|
stopForegroundService();
|
||||||
cancelNotification();
|
|
||||||
|
|
||||||
if (isConnected()) {
|
if (isConnected()) {
|
||||||
// This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property).
|
// This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property).
|
||||||
@@ -135,8 +143,7 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
if (isConnected())
|
if (isConnected())
|
||||||
mManager.disableBatteryLevelCharacteristicNotifications();
|
mManager.disableBatteryLevelCharacteristicNotifications();
|
||||||
|
|
||||||
// when the activity closes we need to show the notification that user is connected to the sensor
|
startForegroundService();
|
||||||
createNotification(R.string.rsc_notification_connected_message, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Runnable mUpdateStridesTask = new Runnable() {
|
private final Runnable mUpdateStridesTask = new Runnable() {
|
||||||
@@ -196,16 +203,42 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void startForegroundService(){
|
||||||
|
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||||
|
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||||
|
final Notification notification = createNotification(R.string.uart_notification_connected_message, 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
} else {
|
||||||
|
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(NOTIFICATION_ID, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void stopForegroundService(){
|
||||||
|
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||||
|
// on devices running Android 8.0 (Oreo) or above
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
stopForeground(true);
|
||||||
|
} else {
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the notification
|
* Creates the notification
|
||||||
*
|
*
|
||||||
* @param messageResId
|
* @param messageResId message resource id. The message must have one String parameter,<br />
|
||||||
* message resource id. The message must have one String parameter,<br />
|
|
||||||
* f.e. <code><string name="name">%s is connected</string></code>
|
* f.e. <code><string name="name">%s is connected</string></code>
|
||||||
* @param defaults
|
* @param defaults
|
||||||
* signals that will be used to notify the user
|
|
||||||
*/
|
*/
|
||||||
private void createNotification(final int messageResId, final int defaults) {
|
private Notification createNotification(final int messageResId, final int defaults) {
|
||||||
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||||
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
final Intent targetIntent = new Intent(this, RSCActivity.class);
|
final Intent targetIntent = new Intent(this, RSCActivity.class);
|
||||||
@@ -214,7 +247,7 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
|
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
|
// 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 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);
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
|
||||||
builder.setContentIntent(pendingIntent);
|
builder.setContentIntent(pendingIntent);
|
||||||
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
||||||
@@ -222,9 +255,7 @@ public class RSCService extends BleProfileService implements RSCManagerCallbacks
|
|||||||
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
||||||
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.rsc_notification_action_disconnect), disconnectAction));
|
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.rsc_notification_action_disconnect), disconnectAction));
|
||||||
|
|
||||||
final Notification notification = builder.build();
|
return builder.build();
|
||||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.notify(NOTIFICATION_ID, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,10 +30,11 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||||
|
|
||||||
import no.nordicsemi.android.log.Logger;
|
import no.nordicsemi.android.log.Logger;
|
||||||
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
||||||
import no.nordicsemi.android.nrftoolbox.R;
|
import no.nordicsemi.android.nrftoolbox.R;
|
||||||
@@ -96,7 +97,7 @@ public class TemplateService extends BleProfileService implements TemplateManage
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
||||||
cancelNotification();
|
stopForegroundService();
|
||||||
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
||||||
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
@@ -104,14 +105,12 @@ public class TemplateService extends BleProfileService implements TemplateManage
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRebind() {
|
protected void onRebind() {
|
||||||
// when the activity rebinds to the service, remove the notification
|
stopForegroundService();
|
||||||
cancelNotification();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onUnbind() {
|
protected void onUnbind() {
|
||||||
// when the activity closes we need to show the notification that user is connected to the sensor
|
startForegroundService();
|
||||||
createNotification(R.string.template_notification_connected_message, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -132,6 +131,34 @@ public class TemplateService extends BleProfileService implements TemplateManage
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void startForegroundService(){
|
||||||
|
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||||
|
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||||
|
final Notification notification = createNotification(R.string.uart_notification_connected_message, 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
} else {
|
||||||
|
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(NOTIFICATION_ID, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void stopForegroundService(){
|
||||||
|
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||||
|
// on devices running Android 8.0 (Oreo) or above
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
stopForeground(true);
|
||||||
|
} else {
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the notification.
|
* Creates the notification.
|
||||||
*
|
*
|
||||||
@@ -139,7 +166,7 @@ public class TemplateService extends BleProfileService implements TemplateManage
|
|||||||
* f.e. <code><string name="name">%s is connected</string></code>
|
* f.e. <code><string name="name">%s is connected</string></code>
|
||||||
* @param defaults signals that will be used to notify the user
|
* @param defaults signals that will be used to notify the user
|
||||||
*/
|
*/
|
||||||
private void createNotification(final int messageResId, final int defaults) {
|
private Notification createNotification(final int messageResId, final int defaults) {
|
||||||
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||||
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
final Intent targetIntent = new Intent(this, TemplateActivity.class);
|
final Intent targetIntent = new Intent(this, TemplateActivity.class);
|
||||||
@@ -156,9 +183,7 @@ public class TemplateService extends BleProfileService implements TemplateManage
|
|||||||
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
||||||
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.template_notification_action_disconnect), disconnectAction));
|
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.template_notification_action_disconnect), disconnectAction));
|
||||||
|
|
||||||
final Notification notification = builder.build();
|
return builder.build();
|
||||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.notify(NOTIFICATION_ID, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,9 +30,7 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import androidx.annotation.NonNull;
|
import android.os.Build;
|
||||||
import androidx.core.app.NotificationCompat;
|
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -42,6 +40,9 @@ import com.google.android.gms.wearable.Node;
|
|||||||
import com.google.android.gms.wearable.NodeApi;
|
import com.google.android.gms.wearable.NodeApi;
|
||||||
import com.google.android.gms.wearable.Wearable;
|
import com.google.android.gms.wearable.Wearable;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||||
import no.nordicsemi.android.log.Logger;
|
import no.nordicsemi.android.log.Logger;
|
||||||
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
||||||
import no.nordicsemi.android.nrftoolbox.R;
|
import no.nordicsemi.android.nrftoolbox.R;
|
||||||
@@ -57,13 +58,21 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
public static final String BROADCAST_UART_RX = "no.nordicsemi.android.nrftoolbox.uart.BROADCAST_UART_RX";
|
public static final String BROADCAST_UART_RX = "no.nordicsemi.android.nrftoolbox.uart.BROADCAST_UART_RX";
|
||||||
public static final String EXTRA_DATA = "no.nordicsemi.android.nrftoolbox.uart.EXTRA_DATA";
|
public static final String EXTRA_DATA = "no.nordicsemi.android.nrftoolbox.uart.EXTRA_DATA";
|
||||||
|
|
||||||
/** A broadcast message with this action and the message in {@link Intent#EXTRA_TEXT} will be sent t the UART device. */
|
/**
|
||||||
|
* A broadcast message with this action and the message in {@link Intent#EXTRA_TEXT} will be sent t the UART device.
|
||||||
|
*/
|
||||||
public final static String ACTION_SEND = "no.nordicsemi.android.nrftoolbox.uart.ACTION_SEND";
|
public final static String ACTION_SEND = "no.nordicsemi.android.nrftoolbox.uart.ACTION_SEND";
|
||||||
/** A broadcast message with this action is triggered when a message is received from the UART device. */
|
/**
|
||||||
|
* A broadcast message with this action is triggered when a message is received from the UART device.
|
||||||
|
*/
|
||||||
private final static String ACTION_RECEIVE = "no.nordicsemi.android.nrftoolbox.uart.ACTION_RECEIVE";
|
private final static String ACTION_RECEIVE = "no.nordicsemi.android.nrftoolbox.uart.ACTION_RECEIVE";
|
||||||
/** Action send when user press the DISCONNECT button on the notification. */
|
/**
|
||||||
|
* Action send when user press the DISCONNECT button on the notification.
|
||||||
|
*/
|
||||||
public final static String ACTION_DISCONNECT = "no.nordicsemi.android.nrftoolbox.uart.ACTION_DISCONNECT";
|
public final static String ACTION_DISCONNECT = "no.nordicsemi.android.nrftoolbox.uart.ACTION_DISCONNECT";
|
||||||
/** A source of an action. */
|
/**
|
||||||
|
* A source of an action.
|
||||||
|
*/
|
||||||
public final static String EXTRA_SOURCE = "no.nordicsemi.android.nrftoolbox.uart.EXTRA_SOURCE";
|
public final static String EXTRA_SOURCE = "no.nordicsemi.android.nrftoolbox.uart.EXTRA_SOURCE";
|
||||||
public final static int SOURCE_NOTIFICATION = 0;
|
public final static int SOURCE_NOTIFICATION = 0;
|
||||||
public final static int SOURCE_WEARABLE = 1;
|
public final static int SOURCE_WEARABLE = 1;
|
||||||
@@ -116,7 +125,7 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
||||||
cancelNotification();
|
stopForegroundService();
|
||||||
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
unregisterReceiver(mDisconnectActionBroadcastReceiver);
|
||||||
unregisterReceiver(mIntentBroadcastReceiver);
|
unregisterReceiver(mIntentBroadcastReceiver);
|
||||||
|
|
||||||
@@ -127,14 +136,12 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRebind() {
|
protected void onRebind() {
|
||||||
// when the activity rebinds to the service, remove the notification
|
stopForegroundService();
|
||||||
cancelNotification();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onUnbind() {
|
protected void onUnbind() {
|
||||||
// when the activity closes we need to show the notification that user is connected to the sensor
|
startForegroundService();
|
||||||
createNotification(R.string.uart_notification_connected_message, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -190,17 +197,18 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the given message to all connected wearables. If the path is equal to {@link Constants.UART#DEVICE_DISCONNECTED} the service will be stopped afterwards.
|
* Sends the given message to all connected wearables. If the path is equal to {@link Constants.UART#DEVICE_DISCONNECTED} the service will be stopped afterwards.
|
||||||
|
*
|
||||||
* @param path message path
|
* @param path message path
|
||||||
* @param message the message
|
* @param message the message
|
||||||
*/
|
*/
|
||||||
private void sendMessageToWearables(final @NonNull String path, final @NonNull String message) {
|
private void sendMessageToWearables(final @NonNull String path, final @NonNull String message) {
|
||||||
if(mGoogleApiClient.isConnected()) {
|
if (mGoogleApiClient.isConnected()) {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
|
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
|
||||||
for(Node node : nodes.getNodes()) {
|
for (Node node : nodes.getNodes()) {
|
||||||
Logger.v(getLogSession(), "[WEAR] Sending message '" + path + "' to " + node.getDisplayName());
|
Logger.v(getLogSession(), "[WEAR] Sending message '" + path + "' to " + node.getDisplayName());
|
||||||
final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await();
|
final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await();
|
||||||
if(result.getStatus().isSuccess()){
|
if (result.getStatus().isSuccess()) {
|
||||||
Logger.i(getLogSession(), "[WEAR] Message sent");
|
Logger.i(getLogSession(), "[WEAR] Message sent");
|
||||||
} else {
|
} else {
|
||||||
Logger.w(getLogSession(), "[WEAR] Sending message failed: " + result.getStatus().getStatusMessage());
|
Logger.w(getLogSession(), "[WEAR] Sending message failed: " + result.getStatus().getStatusMessage());
|
||||||
@@ -216,16 +224,42 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void startForegroundService() {
|
||||||
|
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||||
|
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||||
|
final Notification notification = createNotification(R.string.uart_notification_connected_message, 0);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
startForeground(NOTIFICATION_ID, notification);
|
||||||
|
} else {
|
||||||
|
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.notify(NOTIFICATION_ID, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the service as a foreground service
|
||||||
|
*/
|
||||||
|
private void stopForegroundService() {
|
||||||
|
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||||
|
// on devices running Android 8.0 (Oreo) or above
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
stopForeground(true);
|
||||||
|
} else {
|
||||||
|
cancelNotification();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the notification
|
* Creates the notification
|
||||||
*
|
*
|
||||||
* @param messageResId
|
* @param messageResId message resource id. The message must have one String parameter,<br />
|
||||||
* message resource id. The message must have one String parameter,<br />
|
|
||||||
* f.e. <code><string name="name">%s is connected</string></code>
|
* f.e. <code><string name="name">%s is connected</string></code>
|
||||||
* @param defaults
|
* @param defaults signals that will be used to notify the user
|
||||||
* signals that will be used to notify the user
|
|
||||||
*/
|
*/
|
||||||
private void createNotification(final int messageResId, final int defaults) {
|
protected Notification createNotification(final int messageResId, final int defaults) {
|
||||||
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
|
||||||
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
final Intent targetIntent = new Intent(this, UARTActivity.class);
|
final Intent targetIntent = new Intent(this, UARTActivity.class);
|
||||||
@@ -235,7 +269,7 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
|
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
|
// 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 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);
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
|
||||||
builder.setContentIntent(pendingIntent);
|
builder.setContentIntent(pendingIntent);
|
||||||
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
|
||||||
@@ -243,9 +277,7 @@ public class UARTService extends BleProfileService implements UARTManagerCallbac
|
|||||||
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
|
||||||
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.uart_notification_action_disconnect), disconnectAction));
|
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.uart_notification_action_disconnect), disconnectAction));
|
||||||
|
|
||||||
final Notification notification = builder.build();
|
return builder.build();
|
||||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.notify(NOTIFICATION_ID, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user