aboutsummaryrefslogtreecommitdiff
path: root/connectivity/bluetooth
diff options
context:
space:
mode:
authorTrevor Johns <trevorjohns@google.com>2017-03-17 14:30:42 -0700
committerTrevor Johns <trevorjohns@google.com>2017-03-17 16:47:52 -0700
commit76561865f404f09f026d79181a7795c00ad98064 (patch)
tree2798a712d3735d5e0700e43a3a4e84bd48846be8 /connectivity/bluetooth
parent40605fae740fbae5e9316513d8d86c6119dac352 (diff)
downloadandroid-76561865f404f09f026d79181a7795c00ad98064.tar.gz
BluetoothAdvertisements: Update for background service restrictions
Ensure that background advertisement service is running as a foreground service so that it is exempt from background service execution limits. Test: manual Bug: 35451065 Change-Id: I911bb3c3c1e097f3d27d11cf83aa152f01116e97
Diffstat (limited to 'connectivity/bluetooth')
-rw-r--r--connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java31
1 files changed, 28 insertions, 3 deletions
diff --git a/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java b/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java
index 0cc3ff03..a3138803 100644
--- a/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java
+++ b/connectivity/bluetooth/BluetoothAdvertisements/Application/src/main/java/com/example/android/bluetoothadvertisements/AdvertiserService.java
@@ -1,5 +1,7 @@
package com.example.android.bluetoothadvertisements;
+import android.app.Notification;
+import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
@@ -25,6 +27,8 @@ public class AdvertiserService extends Service {
private static final String TAG = AdvertiserService.class.getSimpleName();
+ private static final int FOREGROUND_NOTIFICATION_ID = 1;
+
/**
* A global variable to let AdvertiserFragment check if the Service is running without needing
* to start or bind to it.
@@ -34,7 +38,7 @@ public class AdvertiserService extends Service {
public static boolean running = false;
public static final String ADVERTISING_FAILED =
- "com.example.android.bluetoothadvertisements.advertising_failed";
+ "com.example.android.bluetoothadvertisements.advertising_failed";
public static final String ADVERTISING_FAILED_EXTRA_CODE = "failureCode";
@@ -72,6 +76,7 @@ public class AdvertiserService extends Service {
running = false;
stopAdvertising();
mHandler.removeCallbacks(timeoutRunnable);
+ stopForeground(true);
super.onDestroy();
}
@@ -125,6 +130,8 @@ public class AdvertiserService extends Service {
* Starts BLE Advertising.
*/
private void startAdvertising() {
+ goForeground();
+
Log.d(TAG, "Service: Starting Advertising");
if (mAdvertiseCallback == null) {
@@ -134,12 +141,30 @@ public class AdvertiserService extends Service {
if (mBluetoothLeAdvertiser != null) {
mBluetoothLeAdvertiser.startAdvertising(settings, data,
- mAdvertiseCallback);
+ mAdvertiseCallback);
}
}
}
/**
+ * Move service to the foreground, to avoid execution limits on background processes.
+ *
+ * Callers should call stopForeground(true) when background work is complete.
+ */
+ private void goForeground() {
+ Intent notificationIntent = new Intent(this, MainActivity.class);
+ PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
+ notificationIntent, 0);
+ Notification n = new Notification.Builder(this)
+ .setContentTitle("Advertising device via Bluetooth")
+ .setContentText("This device is discoverable to others nearby.")
+ .setSmallIcon(R.drawable.ic_launcher)
+ .setContentIntent(pendingIntent)
+ .build();
+ startForeground(FOREGROUND_NOTIFICATION_ID, n);
+ }
+
+ /**
* Stops BLE Advertising.
*/
private void stopAdvertising() {
@@ -220,4 +245,4 @@ public class AdvertiserService extends Service {
sendBroadcast(failureIntent);
}
-} \ No newline at end of file
+}