Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0f0ab7e
add basic sense service
Feb 24, 2016
09998d1
fix incorrectly named test cases
Feb 24, 2016
1928a95
move common sense test case class
Feb 24, 2016
6f3e181
rename sense service helper; add test case for connection lifecycle
Feb 24, 2016
e64c8d5
add sense service to android manifest
Feb 24, 2016
2035b7b
bump minimum SDK version; add task serialization to SenseService
Feb 24, 2016
a129425
separate repeated mock creation into utility class; add some tests fo…
Feb 25, 2016
1b0e151
bump library version
Feb 25, 2016
0f919dc
scan criteria doesn't require instance state
Feb 25, 2016
d1c2231
add observable interface to SenseServiceConnection; add convenience m…
Feb 25, 2016
857640f
yield SenseService for effectful operations to make chaining simpler;…
Feb 25, 2016
e39d799
convenience method for checking sense connectivity status; more tests
Feb 25, 2016
30db7fa
clean up SenseService command APIs
Feb 25, 2016
d1556b8
remove useless #removeBond() method; add #getDeviceId method
Feb 26, 2016
bcd7b44
add missing method to SenseService; add better interface for connection
Feb 29, 2016
b86cfdc
remove unused getter for stronger encapsulation guarantees
Feb 29, 2016
fade5ba
remove troublesome nullability annotations
Feb 29, 2016
a219ab3
better management of connections in SenseService
Feb 29, 2016
9b0bc54
remove broken magic registration of BuruberiReportingProvider
Feb 29, 2016
9a4c8dd
guard against connection race conditions after discover services
Feb 29, 2016
dbea3ca
expose observable serialization for fun and profit
Mar 1, 2016
b0c2e07
add support for foregrounding the SenseService for better multi-taski…
Mar 2, 2016
9066c4d
fix some bugs around foregrounding
Mar 2, 2016
6d20ba8
deprecations; tests + code clean up; documentation
Mar 2, 2016
4b5526c
switch to connected broadcast for foreground side-effects
Mar 2, 2016
a6a2550
bump version
Mar 3, 2016
db76d82
Merge branch 'master' into km/sense-service
Mar 3, 2016
57a7368
switch to snapshot deploy mode
Mar 4, 2016
eabd2a2
add missing task serialization calls
Mar 4, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions ble/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.text.SimpleDateFormat

// Used for both the 'aar' file and publish.
def VERSION_NAME = "2016.3.3.1" // <year>.<month>.<day>.<build-number>
def VERSION_NAME = "2016.3.7.1-SNAPSHOT" // <year>.<month>.<day>.<build-number>
def PACKAGE = 'is.hello.commonsense'

apply plugin: 'com.android.library'
Expand All @@ -12,13 +12,12 @@ def generateVersionCode() {
def formatter = new SimpleDateFormat("yyMMddHH")
Integer.parseInt(formatter.format(now))
}

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 18
minSdkVersion 19
targetSdkVersion 23
versionCode generateVersionCode()
versionName VERSION_NAME
Expand All @@ -44,7 +43,7 @@ dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

testCompile "org.robolectric:robolectric:3.0"
testCompile 'org.robolectric:robolectric:3.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.hamcrest:hamcrest-library:1.3'
Expand Down Expand Up @@ -90,7 +89,7 @@ publishing {
resolveStrategy = DELEGATE_FIRST
name 'android-commonsense'
description 'Client classes for Sense.'
url 'https://github.com/hello/buruberi'
url 'https://github.com/hello/android-commonsense'
scm {
url 'https://github.com/hello/android-commonsense'
connection 'scm:git:[email protected]:hello/android-commonsense.git'
Expand Down
9 changes: 9 additions & 0 deletions ble/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application>

<service android:name=".service.SenseService"
android:exported="false"
android:permission="android.permission.BLUETOOTH"
android:label="Sense Bluetooth Service"/>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import is.hello.buruberi.bluetooth.errors.ConnectionStateException;
import is.hello.buruberi.bluetooth.errors.LostConnectionException;
import is.hello.buruberi.bluetooth.errors.OperationTimeoutException;
import is.hello.buruberi.bluetooth.stacks.BluetoothStack;
Expand All @@ -28,7 +29,6 @@
import is.hello.buruberi.bluetooth.stacks.util.LoggerFacade;
import is.hello.buruberi.bluetooth.stacks.util.PeripheralCriteria;
import is.hello.buruberi.util.Operation;
import is.hello.commonsense.bluetooth.errors.BuruberiReportingProvider;
import is.hello.commonsense.bluetooth.errors.SenseBusyError;
import is.hello.commonsense.bluetooth.errors.SenseConnectWifiError;
import is.hello.commonsense.bluetooth.errors.SenseNotFoundError;
Expand All @@ -41,8 +41,9 @@
import is.hello.commonsense.bluetooth.model.SenseNetworkStatus;
import is.hello.commonsense.bluetooth.model.protobuf.SenseCommandProtos;
import is.hello.commonsense.bluetooth.model.protobuf.SenseCommandProtos.wifi_endpoint;
import is.hello.commonsense.service.SenseService;
import is.hello.commonsense.util.ConnectProgress;
import is.hello.commonsense.util.Functions;
import is.hello.commonsense.util.Func;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
Expand All @@ -54,13 +55,12 @@
import static is.hello.commonsense.bluetooth.model.protobuf.SenseCommandProtos.MorpheusCommand.CommandType;
import static is.hello.commonsense.bluetooth.model.protobuf.SenseCommandProtos.wifi_connection_state;

/**
* Prefer {@link SenseService} for all new code.
*/
public class SensePeripheral {
public static final String LOG_TAG = SensePeripheral.class.getSimpleName();

static {
BuruberiReportingProvider.register();
}

//region Versions

/**
Expand Down Expand Up @@ -112,7 +112,11 @@ public enum CountryCode {

//region Lifecycle

/**
* @deprecated Use {@link SenseService} with your own peripheral discovery code.
*/
@CheckResult
@Deprecated
public static Observable<List<SensePeripheral>> discover(@NonNull BluetoothStack bluetoothStack,
@NonNull PeripheralCriteria criteria) {
criteria.addExactMatchPredicate(AdvertisingData.TYPE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS,
Expand All @@ -125,7 +129,11 @@ public List<SensePeripheral> call(List<GattPeripheral> peripherals) {
});
}

/**
* @deprecated Use {@link SenseService} with your own peripheral discovery code.
*/
@CheckResult
@Deprecated
public static Observable<SensePeripheral> rediscover(@NonNull BluetoothStack bluetoothStack,
@NonNull String deviceId,
boolean includeHighPowerPreScan) {
Expand Down Expand Up @@ -183,16 +191,22 @@ public Observable<ConnectProgress> connect() {
connectFlags = GattPeripheral.CONNECT_FLAG_TRANSPORT_LE;
}
final OperationTimeout timeout = createStackTimeout("Connect");
final Func1<GattService, ConnectProgress> onDiscoveredServices = new Func1<GattService, ConnectProgress>() {
final Func1<GattService, Observable<ConnectProgress>> onDiscoveredServices = new Func1<GattService, Observable<ConnectProgress>>() {
@Override
public ConnectProgress call(GattService service) {
SensePeripheral.this.gattService = service;
SensePeripheral.this.commandCharacteristic =
gattService.getCharacteristic(SenseIdentifiers.CHARACTERISTIC_PROTOBUF_COMMAND);
SensePeripheral.this.responseCharacteristic =
gattService.getCharacteristic(SenseIdentifiers.CHARACTERISTIC_PROTOBUF_COMMAND_RESPONSE);
responseCharacteristic.setPacketListener(packetListener);
return ConnectProgress.CONNECTED;
public Observable<ConnectProgress> call(GattService service) {
// Guard against the device connection being dropped in the
// 3 second delay that happens after discovering services.
if (gattPeripheral.getConnectionStatus() == GattPeripheral.STATUS_CONNECTED) {
SensePeripheral.this.gattService = service;
SensePeripheral.this.commandCharacteristic =
gattService.getCharacteristic(SenseIdentifiers.CHARACTERISTIC_PROTOBUF_COMMAND);
SensePeripheral.this.responseCharacteristic =
gattService.getCharacteristic(SenseIdentifiers.CHARACTERISTIC_PROTOBUF_COMMAND_RESPONSE);
responseCharacteristic.setPacketListener(packetListener);
return Observable.just(ConnectProgress.CONNECTED);
} else {
return Observable.error(new ConnectionStateException());
}
}
};

Expand All @@ -203,18 +217,18 @@ public ConnectProgress call(GattService service) {
// behavior in KitKat and Gingerbread, which cannot establish
// bonds without an active connection.
sequence = Observable.concat(
Observable.just(ConnectProgress.BONDING),
gattPeripheral.createBond().map(Functions.createMapperToValue(ConnectProgress.CONNECTING)),
gattPeripheral.connect(connectFlags, timeout).map(Functions.createMapperToValue(ConnectProgress.DISCOVERING_SERVICES)),
gattPeripheral.discoverService(SenseIdentifiers.SERVICE, timeout).map(onDiscoveredServices)
);
Observable.just(ConnectProgress.BONDING),
gattPeripheral.createBond().map(Func.justValue(ConnectProgress.CONNECTING)),
gattPeripheral.connect(connectFlags, timeout).map(Func.justValue(ConnectProgress.DISCOVERING_SERVICES)),
gattPeripheral.discoverService(SenseIdentifiers.SERVICE, timeout).flatMap(onDiscoveredServices)
);
} else {
sequence = Observable.concat(
Observable.just(ConnectProgress.CONNECTING),
gattPeripheral.connect(connectFlags, timeout).map(Functions.createMapperToValue(ConnectProgress.BONDING)),
gattPeripheral.createBond().map(Functions.createMapperToValue(ConnectProgress.DISCOVERING_SERVICES)),
gattPeripheral.discoverService(SenseIdentifiers.SERVICE, timeout).map(onDiscoveredServices)
);
Observable.just(ConnectProgress.CONNECTING),
gattPeripheral.connect(connectFlags, timeout).map(Func.justValue(ConnectProgress.BONDING)),
gattPeripheral.createBond().map(Func.justValue(ConnectProgress.DISCOVERING_SERVICES)),
gattPeripheral.discoverService(SenseIdentifiers.SERVICE, timeout).flatMap(onDiscoveredServices)
);
}

return sequence.subscribeOn(gattPeripheral.getStack().getScheduler())
Expand Down Expand Up @@ -252,7 +266,7 @@ public void onNext(SensePeripheral sensePeripheral) {
@CheckResult
public Observable<SensePeripheral> disconnect() {
return gattPeripheral.disconnect()
.map(Functions.createMapperToValue(this))
.map(Func.justValue(this))
.finallyDo(new Action0() {
@Override
public void call() {
Expand All @@ -269,7 +283,7 @@ public Observable<SensePeripheral> removeBond() {
REMOVE_BOND_TIMEOUT_S,
TimeUnit.SECONDS);
return gattPeripheral.removeBond(timeout)
.map(Functions.createMapperToValue(this));
.map(Func.justValue(this));
}

//endregion
Expand Down Expand Up @@ -624,7 +638,7 @@ public Observable<Void> putIntoNormalMode() {
.setAppVersion(APP_VERSION)
.build();
return performSimpleCommand(morpheusCommand, createSimpleCommandTimeout())
.map(Functions.createMapperToVoid());
.map(Func.justVoid());
}

@CheckResult
Expand All @@ -642,7 +656,7 @@ public Observable<Void> putIntoPairingMode() {
.setAppVersion(APP_VERSION)
.build();
return performDisconnectingCommand(morpheusCommand, createSimpleCommandTimeout())
.map(Functions.createMapperToVoid());
.map(Func.justVoid());
}

@CheckResult
Expand Down Expand Up @@ -870,7 +884,7 @@ public Observable<Void> linkAccount(final String accountToken) {
.setAccountId(accountToken)
.build();
return performSimpleCommand(morpheusCommand, createSimpleCommandTimeout())
.map(Functions.createMapperToVoid());
.map(Func.justVoid());
}

@CheckResult
Expand All @@ -888,7 +902,7 @@ public Observable<Void> factoryReset() {
.setAppVersion(APP_VERSION)
.build();
return performDisconnectingCommand(morpheusCommand, createSimpleCommandTimeout())
.map(Functions.createMapperToVoid());
.map(Func.justVoid());
}

@CheckResult
Expand All @@ -906,7 +920,7 @@ public Observable<Void> pushData() {
.setAppVersion(APP_VERSION)
.build();
return performSimpleCommand(morpheusCommand, createSimpleCommandTimeout())
.map(Functions.createMapperToVoid());
.map(Func.justVoid());
}

@CheckResult
Expand All @@ -924,7 +938,7 @@ public Observable<Void> runLedAnimation(@NonNull SenseLedAnimation animationType
.setAppVersion(APP_VERSION)
.build();
return performSimpleCommand(morpheusCommand, createAnimationTimeout())
.map(Functions.createMapperToVoid());
.map(Func.justVoid());
}

@CheckResult
Expand Down
Loading