Skip to content

Commit

Permalink
android sources updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkki Silvola committed Apr 22, 2020
1 parent 3d0f413 commit 0341250
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 436 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,7 @@ public void polarFtpFeatureReady(@NonNull String s) {

ppg.setOnClickListener(v -> {
if(ppgDisposable == null) {
ppgDisposable = api.requestPpgSettings(DEVICE_ID).toFlowable().flatMap(new Function<PolarSensorSetting, Publisher<PolarOhrPPGData>>() {
@Override
public Publisher<PolarOhrPPGData> apply(PolarSensorSetting polarPPGSettings) throws Exception {
return api.startOhrPPGStreaming(DEVICE_ID,polarPPGSettings.maxSettings());
}
}).subscribe(
ppgDisposable = api.requestPpgSettings(DEVICE_ID).toFlowable().flatMap((Function<PolarSensorSetting, Publisher<PolarOhrPPGData>>) polarPPGSettings -> api.startOhrPPGStreaming(DEVICE_ID,polarPPGSettings.maxSettings())).subscribe(
polarOhrPPGData -> {
for( PolarOhrPPGData.PolarOhrPPGSample data : polarOhrPPGData.samples ){
Log.d(TAG," ppg0: " + data.ppg0 + " ppg1: " + data.ppg1 + " ppg2: " + data.ppg2 + "ambient: " + data.ambient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class BleAdvertisementContent {
private HashMap<BleUtils.AD_TYPE,byte[]> advertisementData = new HashMap<>(); // current
private HashMap<BleUtils.AD_TYPE,byte[]> advertisementDataAll = new HashMap<>(); // current + "history"
private HashMap<BleUtils.AD_TYPE, byte[]> advertisementData = new HashMap<>(); // current
private HashMap<BleUtils.AD_TYPE, byte[]> advertisementDataAll = new HashMap<>(); // current + "history"
private String name = "";
private String polarDeviceType = "";
private long polarDeviceIdInt = 0;
Expand All @@ -24,46 +25,48 @@ public class BleAdvertisementContent {
private int medianRssi = -100;
private int rssi = -100;

public void processAdvertisementData( HashMap<BleUtils.AD_TYPE,byte[]> advData,
BleUtils.EVENT_TYPE advertisementEventType,
int rssi ) {
public void processAdvertisementData(HashMap<BleUtils.AD_TYPE, byte[]> advData,
BleUtils.EVENT_TYPE advertisementEventType,
int rssi) {
// start access of atomic section
this.advertisementData.clear();
this.advertisementData.putAll(advData);
this.advertisementDataAll.putAll(advData);
this.advertisementEventType = advertisementEventType;
this.advertisetTimeStamp = System.currentTimeMillis() / 1000L;
if (advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_LOCAL_NAME_COMPLETE)) {
String name = new String(advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_LOCAL_NAME_COMPLETE));
String name = new String(Objects.requireNonNull(advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_LOCAL_NAME_COMPLETE)));
processName(name);
} else if (advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_LOCAL_NAME_SHORT)) {
String name = new String(advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_LOCAL_NAME_SHORT));
String name = new String(Objects.requireNonNull(advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_LOCAL_NAME_SHORT)));
processName(name);
}
// manufacturer data
if (advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_MANUFACTURER_SPECIFIC)) {
byte[] content = advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_MANUFACTURER_SPECIFIC);
if ( content.length > 3 && content[0] == 0x6B && content[1] == 0x00) {
int offset=2;
while (offset < content.length){
if (Objects.requireNonNull(content).length > 3 && content[0] == 0x6B && content[1] == 0x00) {
int offset = 2;
while (offset < content.length) {
int gpbDataBit = (content[offset] & 0x40);
switch (gpbDataBit){
case 0:{
if ( (offset+3) <= content.length ) {
byte[] subset = Arrays.copyOfRange(content,offset - 2, content.length );
switch (gpbDataBit) {
case 0: {
if ((offset + 3) <= content.length) {
byte[] subset = Arrays.copyOfRange(content, offset - 2, content.length);
polarHrAdvertisement.processPolarManufacturerData(subset);
}
offset += 5;
break;
}
case 0x40:{
case 0x40: {
// gpb data, no handling for now
offset += 1;
if( offset < content.length ) offset += (content[offset] & 0x000000FF) + 1;
if (offset < content.length)
offset += (content[offset] & 0x000000FF) + 1;
else offset = content.length;
break;
}
default: offset = content.length;
default:
offset = content.length;
}
}
}
Expand Down Expand Up @@ -91,14 +94,14 @@ private void processName(final String name) {
String[] names = this.name.split(" ");
if (names.length > 2) {
polarDeviceType = names[1];
polarDeviceId = names[names.length-1];
if( polarDeviceId.length() == 7 ){
polarDeviceId = BlePolarDeviceIdUtility.assemblyFullPolarDeviceId(names[names.length-1]);
polarDeviceId = names[names.length - 1];
if (polarDeviceId.length() == 7) {
polarDeviceId = BlePolarDeviceIdUtility.assemblyFullPolarDeviceId(names[names.length - 1]);
this.name = "Polar " + polarDeviceType + " " + polarDeviceId;
}
try {
polarDeviceIdInt = Long.parseLong(polarDeviceId, 16);
} catch(NumberFormatException ex){
} catch (NumberFormatException ex) {
// ignore
polarDeviceIdInt = 0;
}
Expand All @@ -111,13 +114,14 @@ private void processName(final String name) {
}
}

public void resetAdvertisementData(){
public void resetAdvertisementData() {
advertisementData.clear();
advertisementDataAll.clear();
}

/**
* Get the Device ID contain in GAP local name for Polar Devices.
*
* @return String that for "Polar H7 20346EAB" will contain "20346EAB"
*/
public String getPolarDeviceId() {
Expand All @@ -126,6 +130,7 @@ public String getPolarDeviceId() {

/**
* Get the Device type contain in GAP local name for Polar Devices.
*
* @return String that for "Polar H7 20346EAB" will contain "H7"
*/
public String getPolarDeviceType() {
Expand Down Expand Up @@ -201,22 +206,22 @@ public BleUtils.EVENT_TYPE getAdvertisementEventType() {
public boolean isNonConnectableAdvertisement() {
return polarDeviceId.length() != 0 &&
!(advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_MORE) ||
advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_COMPLETE));
advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_COMPLETE));
}

/**
* @param service in hex string format like "180D"
* @return true if found
*/
public boolean containsService(final String service){
public boolean containsService(final String service) {
if (advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_MORE) ||
advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_COMPLETE)) {
advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_COMPLETE)) {
byte[] uuids = advertisementData.containsKey(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_MORE) ?
advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_MORE) :
advertisementData.get(BleUtils.AD_TYPE.GAP_ADTYPE_16BIT_COMPLETE);
for(int i=0; i < uuids.length; i += 2){
String hexUUid = String.format("%02X%02X",uuids[i+1],uuids[i]);
if( hexUUid.equals(service) ){
for (int i = 0; i < Objects.requireNonNull(uuids).length; i += 2) {
String hexUUid = String.format("%02X%02X", uuids[i + 1], uuids[i]);
if (hexUUid.equals(service)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.androidcommunications.polar.api.ble.model.advertisement;

public class BlePolarHrAdvertisement {

private int batteryStatus;
private int sensorContact;
private int ucAdvFrameCounter;
Expand All @@ -13,7 +13,7 @@ public class BlePolarHrAdvertisement {
private int slowAverageHr;
private byte[] currentData;

public void processPolarManufacturerData(byte[] data){
void processPolarManufacturerData(byte[] data) {
currentData = data;
batteryStatus = data[2] & 0x01;
sensorContact = (data[2] & 0x02) >> 1;
Expand All @@ -23,14 +23,14 @@ public void processPolarManufacturerData(byte[] data){
statusFlags = (data[2] & 0x80) >> 7;
khzCode = data[3];
fastAverageHr = (data[4] & 0x000000FF);
if( data.length == 6 ){
if (data.length == 6) {
slowAverageHr = (data[5] & 0x000000FF);
} else {
slowAverageHr = (data[4] & 0x000000FF);
}
}

public boolean isPresent(){
public boolean isPresent() {
return currentData != null;
}

Expand Down Expand Up @@ -74,11 +74,11 @@ public int getHrForDisplay() {
return slowAverageHr;
}

public boolean isOldH7H6(){
public boolean isOldH7H6() {
return currentData.length == 5;
}

public boolean isH7Update(){
public boolean isH7Update() {
return !isOldH7H6();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public void reset(){

public abstract void processServiceData(UUID characteristic, byte[] data, int status, boolean notifying);
public abstract void processServiceDataWritten(UUID characteristic, int status);
public abstract String toString();

public Completable clientReady(boolean checkConnection) {
// override in client if required
Expand Down Expand Up @@ -327,28 +326,25 @@ public void setIsPrimaryService(boolean isPrimaryService) {
* @return Observable stream, only complete or error is produced
*/
public Completable waitServiceDiscovered(final boolean checkConnection){
return Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(CompletableEmitter emitter) {
try {
if (!checkConnection || txInterface.isConnected()) {
synchronized (serviceDiscovered) {
if (serviceDiscovered.get()) {
emitter.onComplete();
return;
}
serviceDiscovered.wait();
if (txInterface.isConnected() && serviceDiscovered.get()) {
emitter.onComplete();
return;
}
return Completable.create(emitter -> {
try {
if (!checkConnection || txInterface.isConnected()) {
synchronized (serviceDiscovered) {
if (serviceDiscovered.get()) {
emitter.onComplete();
return;
}
serviceDiscovered.wait();
if (txInterface.isConnected() && serviceDiscovered.get()) {
emitter.onComplete();
return;
}
}
throw new BleDisconnected();
} catch (Exception ex){
if(!emitter.isDisposed()){
emitter.tryOnError(ex);
}
}
throw new BleDisconnected();
} catch (Exception ex){
if(!emitter.isDisposed()){
emitter.tryOnError(ex);
}
}
}).subscribeOn(Schedulers.io());
Expand All @@ -363,43 +359,40 @@ public void subscribe(CompletableEmitter emitter) {
*/
public Completable waitNotificationEnabled(final UUID uuid, final boolean checkConnection, final Scheduler scheduler) {
final AtomicInteger integer = getNotificationAtomicInteger(uuid);
return Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(CompletableEmitter emitter) {
try {
if (!checkConnection || txInterface.isConnected()) {
if (integer != null) {
return Completable.create(emitter -> {
try {
if (!checkConnection || txInterface.isConnected()) {
if (integer != null) {
if (integer.get() == ATT_SUCCESS) {
emitter.onComplete();
return;
}
synchronized (integer) {
if (integer.get() == ATT_SUCCESS) {
emitter.onComplete();
return;
} else if (integer.get() != -1) {
throw new BleAttributeError("Failed to set characteristic notification or indication ", integer.get());
}
synchronized (integer) {
if (integer.get() == ATT_SUCCESS) {
emitter.onComplete();
return;
} else if (integer.get() != -1) {
integer.wait();
if (integer.get() != ATT_SUCCESS && !emitter.isDisposed()) {
if (integer.get() != -1) {
throw new BleAttributeError("Failed to set characteristic notification or indication ", integer.get());
} else {
throw new BleDisconnected();
}
integer.wait();
if (integer.get() != ATT_SUCCESS && !emitter.isDisposed()) {
if (integer.get() != -1) {
throw new BleAttributeError("Failed to set characteristic notification or indication ", integer.get());
} else {
throw new BleDisconnected();
}
}
emitter.onComplete();
}
} else {
throw new BleCharacteristicNotFound();
emitter.onComplete();
}
} else {
throw new BleDisconnected();
}
} catch (Exception ex){
if(!emitter.isDisposed()){
emitter.tryOnError(ex);
throw new BleCharacteristicNotFound();
}
} else {
throw new BleDisconnected();
}
} catch (Exception ex){
if(!emitter.isDisposed()){
emitter.tryOnError(ex);
}
}
}).subscribeOn(scheduler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.androidcommunications.polar.api.ble.model.gatt.client;

import android.support.annotation.NonNull;

import com.androidcommunications.polar.api.ble.exceptions.BleDisconnected;
import com.androidcommunications.polar.api.ble.model.gatt.BleGattBase;
import com.androidcommunications.polar.api.ble.model.gatt.BleGattTxInterface;
Expand Down Expand Up @@ -57,7 +59,7 @@ public void processServiceDataWritten(UUID characteristic, int status) {
}

@Override
public String toString() {
public @NonNull String toString() {
return "Battery service";
}

Expand Down Expand Up @@ -86,8 +88,8 @@ public Single<Integer> waitBatteryLevelUpdate(final boolean checkConnection){

/**
* monitor battery notifications
* @param checkConnection
* @return
* @param checkConnection false = no is connected check before observer added, true = check's is connected <BR>
* @return Observable
*/
public Flowable<Integer> monitorBatteryLevelUpdate(final boolean checkConnection) {
final FlowableEmitter<? super Integer>[] observer = new FlowableEmitter[1];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.androidcommunications.polar.api.ble.model.gatt.client;

import android.support.annotation.NonNull;
import android.util.Pair;

import com.androidcommunications.polar.api.ble.exceptions.BleAttributeError;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void processServiceDataWritten(UUID characteristic, int status) {
}

@Override
public String toString() {
public @NonNull String toString() {
return "Device info service";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.androidcommunications.polar.api.ble.model.gatt.client;

import android.support.annotation.NonNull;

import com.androidcommunications.polar.api.ble.exceptions.BleDisconnected;
import com.androidcommunications.polar.api.ble.model.gatt.BleGattBase;
import com.androidcommunications.polar.api.ble.model.gatt.BleGattTxInterface;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void processServiceDataWritten(UUID characteristic, int status) {
}

@Override
public String toString() {
public @NonNull String toString() {
return "GAP service with values device name: ";
}

Expand Down
Loading

0 comments on commit 0341250

Please sign in to comment.