Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Commit

Permalink
Auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
google-automerger committed Mar 21, 2017
1 parent 3b1dc7f commit 76653d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.2.0'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -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";

Expand Down Expand Up @@ -72,6 +76,7 @@ public void onDestroy() {
running = false;
stopAdvertising();
mHandler.removeCallbacks(timeoutRunnable);
stopForeground(true);
super.onDestroy();
}

Expand Down Expand Up @@ -125,6 +130,8 @@ public void run() {
* Starts BLE Advertising.
*/
private void startAdvertising() {
goForeground();

Log.d(TAG, "Service: Starting Advertising");

if (mAdvertiseCallback == null) {
Expand All @@ -134,11 +141,29 @@ private void startAdvertising() {

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.
*/
Expand Down Expand Up @@ -220,4 +245,4 @@ private void sendFailureIntent(int errorCode){
sendBroadcast(failureIntent);
}

}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ submitting a pull request through GitHub. Please see CONTRIBUTING.md for more de
License
-------

Copyright 2016 The Android Open Source Project, Inc.
Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
Expand Down

0 comments on commit 76653d3

Please sign in to comment.