mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2026-01-07 00:34:33 +01:00
Moving battery level views to profile activities, part 1
This commit is contained in:
@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Deque;
|
||||
@@ -74,7 +75,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
if (mICPCharacteristic != null)
|
||||
requests.add(Request.newEnableNotificationsRequest(mICPCharacteristic));
|
||||
@@ -83,7 +84,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
BluetoothGattService service = gatt.getService(BP_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mBPMCharacteristic = service.getCharacteristic(BPM_CHARACTERISTIC_UUID);
|
||||
@@ -93,7 +94,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
return mICPCharacteristic != null;
|
||||
}
|
||||
|
||||
@@ -104,7 +105,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// Intermediate Cuff Pressure characteristic read
|
||||
Logger.a(mLogSession, "\"" + IntermediateCuffPressureParser.parse(characteristic) + "\" received");
|
||||
|
||||
@@ -112,7 +113,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// Blood Pressure Measurement characteristic read
|
||||
Logger.a(mLogSession, "\"" + BloodPressureMeasurementParser.parse(characteristic) + "\" received");
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import java.util.Deque;
|
||||
@@ -111,7 +112,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
return managerInstance;
|
||||
}
|
||||
|
||||
public CGMSManager(Context context) {
|
||||
public CGMSManager(final Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@@ -126,7 +127,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newEnableNotificationsRequest(mCGMMeasurementCharacteristic));
|
||||
if (mCGMOpsControlPointCharacteristic != null) {
|
||||
@@ -139,7 +140,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(CGMS_UUID);
|
||||
if (service != null) {
|
||||
mCGMMeasurementCharacteristic = service.getCharacteristic(CGM_MEASUREMENT_UUID);
|
||||
@@ -150,7 +151,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(CGMS_UUID);
|
||||
if (service != null) {
|
||||
mCGMOpsControlPointCharacteristic = service.getCharacteristic(CGM_OPS_CONTROL_POINT_UUID);
|
||||
@@ -159,7 +160,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -170,7 +171,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
if (characteristic.getUuid().equals(RACP_UUID)) {
|
||||
Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" sent");
|
||||
} else { // uuid == CGM_OPS_CONTROL_POINT_UUID
|
||||
@@ -179,7 +180,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + CGMMeasurementParser.parse(characteristic) + "\" received");
|
||||
|
||||
// CGM Measurement characteristic may have one or more CGM records
|
||||
@@ -201,7 +202,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
if (characteristic.getUuid().equals(RACP_UUID)) {
|
||||
Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" received");
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
private TextView mTotalDistanceView;
|
||||
private TextView mTotalDistanceUnitView;
|
||||
private TextView mGearRatioView;
|
||||
private TextView mBatteryLevelView;
|
||||
|
||||
@Override
|
||||
protected void onCreateView(final Bundle savedInstanceState) {
|
||||
@@ -79,6 +80,7 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
mTotalDistanceView = findViewById(R.id.distance_total);
|
||||
mTotalDistanceUnitView = findViewById(R.id.distance_total_unit);
|
||||
mGearRatioView = findViewById(R.id.ratio);
|
||||
mBatteryLevelView = findViewById(R.id.battery);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,6 +96,7 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
mDistanceView.setText(R.string.not_available_value);
|
||||
mTotalDistanceView.setText(R.string.not_available_value);
|
||||
mGearRatioView.setText(R.string.not_available_value);
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
|
||||
setUnits();
|
||||
}
|
||||
@@ -178,6 +181,18 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
// not used
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceDisconnected(final BluetoothDevice device) {
|
||||
super.onDeviceDisconnected(device);
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
}
|
||||
|
||||
// This method will not be called, as CSC Service connects with autoConnect = false
|
||||
@Override
|
||||
public void onLinklossOccur(final BluetoothDevice device) {
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
}
|
||||
|
||||
private void onMeasurementReceived(float speed, float distance, float totalDistance) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
final int unit = Integer.parseInt(preferences.getString(SettingsFragment.SETTINGS_UNIT, String.valueOf(SettingsFragment.SETTINGS_UNIT_DEFAULT)));
|
||||
@@ -219,6 +234,10 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
mCadenceView.setText(String.format(Locale.US, "%d", cadence));
|
||||
}
|
||||
|
||||
public void onBatteryLevelChanged(final int value) {
|
||||
mBatteryLevelView.setText(getString(R.string.battery, value));
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
@@ -235,6 +254,10 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
final int cadence = intent.getIntExtra(CSCService.EXTRA_CADENCE, 0);
|
||||
// Update GUI
|
||||
onGearRatioUpdate(ratio, cadence);
|
||||
} else if (CSCService.BROADCAST_BATTERY_LEVEL.equals(action)) {
|
||||
final int batteryLevel = intent.getIntExtra(CSCService.EXTRA_BATTERY_LEVEL, 0);
|
||||
// Update GUI
|
||||
onBatteryLevelChanged(batteryLevel);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -243,6 +266,7 @@ public class CSCActivity extends BleProfileServiceReadyActivity<CSCService.CSCBi
|
||||
final IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(CSCService.BROADCAST_WHEEL_DATA);
|
||||
intentFilter.addAction(CSCService.BROADCAST_CRANK_DATA);
|
||||
intentFilter.addAction(CSCService.BROADCAST_BATTERY_LEVEL);
|
||||
return intentFilter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,10 @@ import android.support.annotation.NonNull;
|
||||
import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.ReadRequest;
|
||||
import no.nordicsemi.android.ble.Request;
|
||||
import no.nordicsemi.android.ble.callback.Data;
|
||||
import no.nordicsemi.android.ble.callback.profile.BatteryLevelCallback;
|
||||
import no.nordicsemi.android.ble.callback.profile.CyclingSpeedAndCadenceCallback;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
import no.nordicsemi.android.nrftoolbox.csc.settings.SettingsFragment;
|
||||
@@ -42,16 +45,25 @@ import no.nordicsemi.android.nrftoolbox.parser.CSCMeasurementParser;
|
||||
|
||||
public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
/**
|
||||
* Cycling Speed and Cadence service UUID
|
||||
* Cycling Speed and Cadence service UUID.
|
||||
*/
|
||||
public final static UUID CYCLING_SPEED_AND_CADENCE_SERVICE_UUID = UUID.fromString("00001816-0000-1000-8000-00805f9b34fb");
|
||||
/**
|
||||
* Cycling Speed and Cadence Measurement characteristic UUID
|
||||
* Cycling Speed and Cadence Measurement characteristic UUID.
|
||||
*/
|
||||
private static final UUID CSC_MEASUREMENT_CHARACTERISTIC_UUID = UUID.fromString("00002A5B-0000-1000-8000-00805f9b34fb");
|
||||
/**
|
||||
* Battery Service UUID.
|
||||
*/
|
||||
private final static UUID BATTERY_SERVICE_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");
|
||||
/**
|
||||
* Battery Level characteristic UUID.
|
||||
*/
|
||||
private final static UUID BATTERY_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A19-0000-1000-8000-00805f9b34fb");
|
||||
|
||||
private final SharedPreferences preferences;
|
||||
private BluetoothGattCharacteristic mCSCMeasurementCharacteristic;
|
||||
private BluetoothGattCharacteristic mBatteryLevelCharacteristic;
|
||||
|
||||
CSCManager(final Context context) {
|
||||
super(context);
|
||||
@@ -63,18 +75,62 @@ public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
return mGattCallback;
|
||||
}
|
||||
|
||||
public void readBatteryLevelCharacteristic() {
|
||||
readCharacteristic(mBatteryLevelCharacteristic)
|
||||
.with(new BatteryLevelCallback() {
|
||||
@Override
|
||||
public void onBatteryValueChanged(final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(getBluetoothDevice(), batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(final @NonNull Data data) {
|
||||
log(LogContract.Log.Level.WARNING, "Invalid Battery Level data received: " + data);
|
||||
}
|
||||
})
|
||||
.fail(status -> log(LogContract.Log.Level.WARNING, "Battery Level characteristic not found"));
|
||||
}
|
||||
|
||||
public void enableBatteryLevelCharacteristicNotifications() {
|
||||
// If the Battery Level characteristic is null, the request will be ignored
|
||||
enableNotifications(mBatteryLevelCharacteristic)
|
||||
.with(new BatteryLevelCallback() {
|
||||
@Override
|
||||
public void onBatteryValueChanged(final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(getBluetoothDevice(), batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(final @NonNull Data data) {
|
||||
log(LogContract.Log.Level.WARNING, "Invalid Battery Level data received: " + data);
|
||||
}
|
||||
})
|
||||
.done(() -> log(LogContract.Log.Level.INFO, "Battery Level notifications enabled"))
|
||||
.fail(status -> log(LogContract.Log.Level.WARNING, "Battery Level characteristic not found"));
|
||||
}
|
||||
|
||||
public void disableBatteryLevelCharacteristicNotifications() {
|
||||
disableNotifications(mBatteryLevelCharacteristic)
|
||||
.done(() -> log(LogContract.Log.Level.INFO, "Battery Level notifications disabled"));
|
||||
}
|
||||
|
||||
/**
|
||||
* BluetoothGatt callbacks for connection/disconnection, service discovery, receiving indication, etc
|
||||
*/
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected void initialize(final BluetoothDevice device) {
|
||||
protected void initialize(@NonNull final BluetoothDevice device) {
|
||||
enableBatteryLevelCharacteristicNotifications();
|
||||
|
||||
// CSC characteristic is required
|
||||
enableNotifications(mCSCMeasurementCharacteristic)
|
||||
.with(new CyclingSpeedAndCadenceCallback() {
|
||||
@Override
|
||||
public void onDataReceived(@NonNull final Data data) {
|
||||
public void onDataReceived(final @NonNull Data data) {
|
||||
log(LogContract.Log.Level.APPLICATION, "\"" + CSCMeasurementParser.parse(data) + "\" received");
|
||||
|
||||
// Pass through received data
|
||||
super.onDataReceived(data);
|
||||
}
|
||||
|
||||
@@ -94,14 +150,14 @@ public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final Data data) {
|
||||
log(LogContract.Log.Level.WARNING, "Invalid data received: " + data);
|
||||
public void onInvalidDataReceived(final @NonNull Data data) {
|
||||
log(LogContract.Log.Level.WARNING, "Invalid CSC Measurement data received: " + data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(CYCLING_SPEED_AND_CADENCE_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mCSCMeasurementCharacteristic = service.getCharacteristic(CSC_MEASUREMENT_CHARACTERISTIC_UUID);
|
||||
@@ -109,9 +165,19 @@ public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
return mCSCMeasurementCharacteristic != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(BATTERY_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mBatteryLevelCharacteristic = service.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC_UUID);
|
||||
}
|
||||
return mBatteryLevelCharacteristic != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDeviceDisconnected() {
|
||||
mCSCMeasurementCharacteristic = null;
|
||||
mBatteryLevelCharacteristic = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -29,4 +29,6 @@ public interface CSCManagerCallbacks extends BleManagerCallbacks {
|
||||
void onDistanceChanged(final BluetoothDevice device, final float totalDistance, final float distance, final float speed);
|
||||
|
||||
void onCrankDataChanged(final BluetoothDevice device, final float crankCadence, final float gearRatio);
|
||||
|
||||
void onBatteryLevelChanged(final BluetoothDevice device, final int batteryLevel);
|
||||
}
|
||||
|
||||
@@ -45,16 +45,20 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
||||
private static final String TAG = "CSCService";
|
||||
|
||||
public static final String BROADCAST_WHEEL_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_WHEEL_DATA";
|
||||
/** Speed in meters per second. */
|
||||
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";
|
||||
/** 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 BROADCAST_CRANK_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_CRANK_DATA";
|
||||
public static final String EXTRA_GEAR_RATIO = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_GEAR_RATIO";
|
||||
public static final String EXTRA_CADENCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_CADENCE";
|
||||
|
||||
public static final String BROADCAST_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.BROADCAST_BATTERY_LEVEL";
|
||||
public static final String EXTRA_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.EXTRA_BATTERY_LEVEL";
|
||||
|
||||
private static final String ACTION_DISCONNECT = "no.nordicsemi.android.nrftoolbox.csc.ACTION_DISCONNECT";
|
||||
|
||||
private final static int NOTIFICATION_ID = 200;
|
||||
@@ -62,6 +66,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
||||
private final static int DISCONNECT_REQ = 1;
|
||||
|
||||
private final LocalBinder mBinder = new CSCBinder();
|
||||
private CSCManager mManager;
|
||||
|
||||
/**
|
||||
* This local binder is an interface for the bonded activity to operate with the RSC sensor
|
||||
@@ -77,7 +82,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
||||
|
||||
@Override
|
||||
protected BleManager<CSCManagerCallbacks> initializeManager() {
|
||||
return new CSCManager(this);
|
||||
return mManager = new CSCManager(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,10 +107,21 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
||||
protected void onRebind() {
|
||||
// when the activity rebinds to the service, remove the notification
|
||||
cancelNotification();
|
||||
|
||||
if (isConnected()) {
|
||||
// This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property).
|
||||
// If the Battery Level characteristic has only the NOTIFY property, it will only try to enable notifications.
|
||||
mManager.readBatteryLevelCharacteristic();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onUnbind() {
|
||||
// When we are connected, but the application is not open, we are not really interested in battery level notifications.
|
||||
// But we will still be receiving other values, if enabled.
|
||||
if (isConnected())
|
||||
mManager.disableBatteryLevelCharacteristicNotifications();
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -129,6 +145,14 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryLevelChanged(final BluetoothDevice device, final int value) {
|
||||
final Intent broadcast = new Intent(BROADCAST_BATTERY_LEVEL);
|
||||
broadcast.putExtra(EXTRA_DEVICE, getBluetoothDevice());
|
||||
broadcast.putExtra(EXTRA_BATTERY_LEVEL, value);
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the notification
|
||||
*
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import java.util.Calendar;
|
||||
@@ -128,7 +129,7 @@ public class GlucoseManager extends BleManager<GlucoseManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newEnableNotificationsRequest(mGlucoseMeasurementCharacteristic));
|
||||
if (mGlucoseMeasurementContextCharacteristic != null) {
|
||||
@@ -153,7 +154,7 @@ public class GlucoseManager extends BleManager<GlucoseManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(GLS_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mGlucoseMeasurementCharacteristic = service.getCharacteristic(GM_CHARACTERISTIC);
|
||||
@@ -164,7 +165,7 @@ public class GlucoseManager extends BleManager<GlucoseManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(BluetoothGatt gatt) {
|
||||
protected boolean isOptionalServiceSupported(@NonNull BluetoothGatt gatt) {
|
||||
return mGlucoseMeasurementContextCharacteristic != null;
|
||||
}
|
||||
|
||||
@@ -176,12 +177,12 @@ public class GlucoseManager extends BleManager<GlucoseManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" sent");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
final UUID uuid = characteristic.getUuid();
|
||||
|
||||
if (GM_CHARACTERISTIC.equals(uuid)) {
|
||||
@@ -329,7 +330,7 @@ public class GlucoseManager extends BleManager<GlucoseManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" received");
|
||||
|
||||
// Record Access Control Point characteristic
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
@@ -75,7 +76,7 @@ public class HRSManager extends BleManager<HRSManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
if (mHRLocationCharacteristic != null)
|
||||
requests.add(Request.newReadRequest(mHRLocationCharacteristic));
|
||||
@@ -84,7 +85,7 @@ public class HRSManager extends BleManager<HRSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mHRCharacteristic = service.getCharacteristic(HR_CHARACTERISTIC_UUID);
|
||||
@@ -93,7 +94,7 @@ public class HRSManager extends BleManager<HRSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mHRLocationCharacteristic = service.getCharacteristic(HR_SENSOR_LOCATION_CHARACTERISTIC_UUID);
|
||||
@@ -102,7 +103,7 @@ public class HRSManager extends BleManager<HRSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + BodySensorLocationParser.parse(characteristic) + "\" received");
|
||||
|
||||
final String sensorPosition = getBodySensorPosition(characteristic.getValue()[0]);
|
||||
@@ -117,7 +118,7 @@ public class HRSManager extends BleManager<HRSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + HeartRateMeasurementParser.parse(characteristic) + "\" received");
|
||||
|
||||
int hrValue;
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
@@ -72,14 +73,14 @@ public class HTSManager extends BleManager<HTSManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newEnableIndicationsRequest(mHTCharacteristic));
|
||||
return requests;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(HT_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mHTCharacteristic = service.getCharacteristic(HT_MEASUREMENT_CHARACTERISTIC_UUID);
|
||||
@@ -93,7 +94,7 @@ public class HTSManager extends BleManager<HTSManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + TemperatureMeasurementParser.parse(characteristic) + "\" received");
|
||||
|
||||
try {
|
||||
|
||||
@@ -61,7 +61,6 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl
|
||||
private BleManager<? extends BleManagerCallbacks> mBleManager;
|
||||
|
||||
private TextView mDeviceNameView;
|
||||
private TextView mBatteryLevelView;
|
||||
private Button mConnectButton;
|
||||
private ILogSession mLogSession;
|
||||
|
||||
@@ -131,7 +130,6 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mConnectButton = findViewById(R.id.action_connect);
|
||||
mDeviceNameView = findViewById(R.id.device_name);
|
||||
mBatteryLevelView = findViewById(R.id.battery);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -274,17 +272,12 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl
|
||||
runOnUiThread(() -> {
|
||||
mConnectButton.setText(R.string.action_connect);
|
||||
mDeviceNameView.setText(getDefaultDeviceName());
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLinklossOccur(final BluetoothDevice device) {
|
||||
mDeviceConnected = false;
|
||||
runOnUiThread(() -> {
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -312,20 +305,6 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl
|
||||
showToast(R.string.bonding_failed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) {
|
||||
// Yes, we want battery level updates
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryValueReceived(final BluetoothDevice device, final int value) {
|
||||
runOnUiThread(() -> {
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(getString(R.string.battery, value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(final BluetoothDevice device, final String message, final int errorCode) {
|
||||
DebugLogger.e(TAG, "Error occurred: " + message + ", error code: " + errorCode);
|
||||
|
||||
@@ -61,7 +61,6 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct
|
||||
private BleManager<? extends BleManagerCallbacks> mBleManager;
|
||||
|
||||
private TextView mDeviceNameView;
|
||||
private TextView mBatteryLevelView;
|
||||
private Button mConnectButton;
|
||||
private ILogSession mLogSession;
|
||||
|
||||
@@ -131,7 +130,6 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mConnectButton = findViewById(R.id.action_connect);
|
||||
mDeviceNameView = findViewById(R.id.device_name);
|
||||
mBatteryLevelView = findViewById(R.id.battery);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -274,17 +272,12 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct
|
||||
runOnUiThread(() -> {
|
||||
mConnectButton.setText(R.string.action_connect);
|
||||
mDeviceNameView.setText(getDefaultDeviceName());
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLinklossOccur(final BluetoothDevice device) {
|
||||
mDeviceConnected = false;
|
||||
runOnUiThread(() -> {
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -318,14 +311,6 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryValueReceived(final BluetoothDevice device, final int value) {
|
||||
runOnUiThread(() -> {
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(getString(R.string.battery, value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(final BluetoothDevice device, final String message, final int errorCode) {
|
||||
DebugLogger.e(TAG, "Error occurred: " + message + ", error code: " + errorCode);
|
||||
|
||||
@@ -53,6 +53,7 @@ public abstract class BleProfileService extends Service implements BleManagerCal
|
||||
public static final String BROADCAST_SERVICES_DISCOVERED = "no.nordicsemi.android.nrftoolbox.BROADCAST_SERVICES_DISCOVERED";
|
||||
public static final String BROADCAST_DEVICE_READY = "no.nordicsemi.android.nrftoolbox.DEVICE_READY";
|
||||
public static final String BROADCAST_BOND_STATE = "no.nordicsemi.android.nrftoolbox.BROADCAST_BOND_STATE";
|
||||
@Deprecated
|
||||
public static final String BROADCAST_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.BROADCAST_BATTERY_LEVEL";
|
||||
public static final String BROADCAST_ERROR = "no.nordicsemi.android.nrftoolbox.BROADCAST_ERROR";
|
||||
|
||||
@@ -66,6 +67,7 @@ public abstract class BleProfileService extends Service implements BleManagerCal
|
||||
public static final String EXTRA_BOND_STATE = "no.nordicsemi.android.nrftoolbox.EXTRA_BOND_STATE";
|
||||
public static final String EXTRA_SERVICE_PRIMARY = "no.nordicsemi.android.nrftoolbox.EXTRA_SERVICE_PRIMARY";
|
||||
public static final String EXTRA_SERVICE_SECONDARY = "no.nordicsemi.android.nrftoolbox.EXTRA_SERVICE_SECONDARY";
|
||||
@Deprecated
|
||||
public static final String EXTRA_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.EXTRA_BATTERY_LEVEL";
|
||||
public static final String EXTRA_ERROR_MESSAGE = "no.nordicsemi.android.nrftoolbox.EXTRA_ERROR_MESSAGE";
|
||||
public static final String EXTRA_ERROR_CODE = "no.nordicsemi.android.nrftoolbox.EXTRA_ERROR_CODE";
|
||||
@@ -242,12 +244,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal
|
||||
|
||||
if (!mActivityIsChangingConfiguration)
|
||||
onRebind();
|
||||
|
||||
if (!mActivityIsChangingConfiguration && mBleManager.isConnected()) {
|
||||
// This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property).
|
||||
// If the Battery Level characteristic has only the NOTIFY property, it will only try to enable notifications.
|
||||
mBleManager.readBatteryLevel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,10 +262,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal
|
||||
if (!mActivityIsChangingConfiguration)
|
||||
onUnbind();
|
||||
|
||||
// When we are connected, but the application is not open, we are not really interested in battery level notifications. But we will still be receiving other values, if enabled.
|
||||
if (!mActivityIsChangingConfiguration && mBleManager.isConnected())
|
||||
mBleManager.disableBatteryLevelNotifications();
|
||||
|
||||
// We want the onRebind method be called if anything else binds to it again
|
||||
return true;
|
||||
}
|
||||
@@ -391,12 +383,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal
|
||||
// empty default implementation
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) {
|
||||
// By default the Battery Level notifications will be enabled only the activity is bound.
|
||||
return mBinded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceConnecting(final BluetoothDevice device) {
|
||||
final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
|
||||
|
||||
@@ -81,7 +81,6 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
private E mService;
|
||||
|
||||
private TextView mDeviceNameView;
|
||||
private TextView mBatteryLevelView;
|
||||
private Button mConnectButton;
|
||||
|
||||
private ILogSession mLogSession;
|
||||
@@ -366,7 +365,6 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
mConnectButton = findViewById(R.id.action_connect);
|
||||
mDeviceNameView = findViewById(R.id.device_name);
|
||||
mBatteryLevelView = findViewById(R.id.battery);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -504,8 +502,6 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
public void onDeviceDisconnected(final BluetoothDevice device) {
|
||||
mConnectButton.setText(R.string.action_connect);
|
||||
mDeviceNameView.setText(getDefaultDeviceName());
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
|
||||
try {
|
||||
Logger.d(mLogSession, "Unbinding from the service...");
|
||||
@@ -524,8 +520,7 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
|
||||
@Override
|
||||
public void onLinklossOccur(final BluetoothDevice device) {
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(R.string.not_available);
|
||||
// empty default implementation
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -553,19 +548,6 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
// empty default implementation
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) {
|
||||
// This method will never be called.
|
||||
// Please see BleProfileService#shouldEnableBatteryLevelNotifications(BluetoothDevice) instead.
|
||||
throw new UnsupportedOperationException("This method should not be called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryValueReceived(final BluetoothDevice device, final int value) {
|
||||
if (mBatteryLevelView != null)
|
||||
mBatteryLevelView.setText(getString(R.string.battery, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(final BluetoothDevice device, final String message, final int errorCode) {
|
||||
DebugLogger.e(TAG, "Error occurred: " + message + ", error code: " + errorCode);
|
||||
|
||||
@@ -185,6 +185,7 @@ public abstract class BleMulticonnectProfileService extends Service implements B
|
||||
* Returns the last received battery level value.
|
||||
* @param device the device of which battery level should be returned
|
||||
* @return battery value or -1 if no value was received or Battery Level characteristic was not found
|
||||
* @deprecated Keep battery value in your manager instead.
|
||||
*/
|
||||
public int getBatteryValue(final BluetoothDevice device) {
|
||||
final BleManager<BleManagerCallbacks> manager = mBleManagers.get(device);
|
||||
@@ -415,12 +416,6 @@ public abstract class BleMulticonnectProfileService extends Service implements B
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) {
|
||||
// By default the Battery Level notifications will be enabled only the activity is bound.
|
||||
return mBinded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceConnecting(final BluetoothDevice device) {
|
||||
final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
|
||||
|
||||
@@ -24,6 +24,7 @@ package no.nordicsemi.android.nrftoolbox.proximity;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -40,19 +41,20 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
private final ProximityService.ProximityBinder mService;
|
||||
private final List<BluetoothDevice> mDevices;
|
||||
|
||||
public DeviceAdapter(final ProximityService.ProximityBinder binder) {
|
||||
DeviceAdapter(final ProximityService.ProximityBinder binder) {
|
||||
mService = binder;
|
||||
mDevices = mService.getManagedDevices();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
|
||||
public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
|
||||
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_feature_proximity_item, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final ViewHolder holder, final int position) {
|
||||
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
|
||||
holder.bind(mDevices.get(position));
|
||||
}
|
||||
|
||||
@@ -88,13 +90,13 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
notifyItemChanged(position);
|
||||
}
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||
class ViewHolder extends RecyclerView.ViewHolder {
|
||||
private TextView nameView;
|
||||
private TextView addressView;
|
||||
private TextView batteryView;
|
||||
private ImageButton actionButton;
|
||||
|
||||
public ViewHolder(final View itemView) {
|
||||
ViewHolder(final View itemView) {
|
||||
super(itemView);
|
||||
|
||||
nameView = itemView.findViewById(R.id.name);
|
||||
@@ -134,7 +136,7 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
actionButton.setImageResource(on ? R.drawable.ic_stat_notify_proximity_silent : R.drawable.ic_stat_notify_proximity_find);
|
||||
actionButton.setVisibility(state == BluetoothGatt.STATE_CONNECTED ? View.VISIBLE : View.GONE);
|
||||
|
||||
final int batteryValue = mService.getBatteryValue(device);
|
||||
final int batteryValue = mService.getBatteryLevel(device);
|
||||
if (batteryValue >= 0) {
|
||||
batteryView.getCompoundDrawables()[0 /*left*/].setLevel(batteryValue);
|
||||
batteryView.setVisibility(View.VISIBLE);
|
||||
|
||||
@@ -21,10 +21,12 @@
|
||||
*/
|
||||
package no.nordicsemi.android.nrftoolbox.proximity;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
@@ -32,6 +34,7 @@ import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.Request;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
import no.nordicsemi.android.log.Logger;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.AlertLevelParser;
|
||||
import no.nordicsemi.android.nrftoolbox.utility.DebugLogger;
|
||||
@@ -52,6 +55,7 @@ public class ProximityManager extends BleManager<ProximityManagerCallbacks> {
|
||||
|
||||
private BluetoothGattCharacteristic mAlertLevelCharacteristic, mLinklossCharacteristic;
|
||||
private boolean mAlertOn;
|
||||
private int mBatteryLevel;
|
||||
|
||||
public ProximityManager(final Context context) {
|
||||
super(context);
|
||||
@@ -73,14 +77,12 @@ public class ProximityManager extends BleManager<ProximityManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newWriteRequest(mLinklossCharacteristic, HIGH_ALERT));
|
||||
return requests;
|
||||
protected void initialize(@NonNull final BluetoothDevice device) {
|
||||
writeCharacteristic(mLinklossCharacteristic, HIGH_ALERT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService llService = gatt.getService(LINKLOSS_SERVICE_UUID);
|
||||
if (llService != null) {
|
||||
mLinklossCharacteristic = llService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID);
|
||||
@@ -89,7 +91,7 @@ public class ProximityManager extends BleManager<ProximityManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService iaService = gatt.getService(IMMEDIATE_ALERT_SERVICE_UUID);
|
||||
if (iaService != null) {
|
||||
mAlertLevelCharacteristic = iaService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID);
|
||||
@@ -104,11 +106,6 @@ public class ProximityManager extends BleManager<ProximityManagerCallbacks> {
|
||||
// Reset the alert flag
|
||||
mAlertOn = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + AlertLevelParser.parse(characteristic) + "\" sent");
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -128,19 +125,27 @@ public class ProximityManager extends BleManager<ProximityManagerCallbacks> {
|
||||
if (!isConnected())
|
||||
return;
|
||||
|
||||
if (mAlertLevelCharacteristic != null) {
|
||||
mAlertLevelCharacteristic.setValue(on ? HIGH_ALERT : NO_ALERT);
|
||||
writeCharacteristic(mAlertLevelCharacteristic);
|
||||
mAlertOn = on;
|
||||
} else {
|
||||
DebugLogger.w(TAG, "Immediate Alert Level Characteristic is not found");
|
||||
}
|
||||
log(LogContract.Log.Level.VERBOSE, on ? "Setting alarm to HIGH..." : "Disabling alarm...");
|
||||
writeCharacteristic(mAlertLevelCharacteristic, on ? HIGH_ALERT : NO_ALERT)
|
||||
.done(() -> {
|
||||
mAlertOn = on;
|
||||
log(LogContract.Log.Level.APPLICATION, "\"" + AlertLevelParser.parse(mAlertLevelCharacteristic) + "\" sent");
|
||||
})
|
||||
.fail(status -> log(LogContract.Log.Level.APPLICATION, "Alert Level characteristic not found"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the alert has been enabled on the proximity tag, false otherwise.
|
||||
*/
|
||||
public boolean isAlertEnabled() {
|
||||
boolean isAlertEnabled() {
|
||||
return mAlertOn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last obtained Battery Level value in percent.
|
||||
* @return battery level value
|
||||
*/
|
||||
int getBatteryLevel() {
|
||||
return mBatteryLevel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.BleManagerCallbacks;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
import no.nordicsemi.android.nrftoolbox.FeaturesActivity;
|
||||
import no.nordicsemi.android.nrftoolbox.R;
|
||||
@@ -104,6 +105,16 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
final ProximityManager manager = (ProximityManager) getBleManager(device);
|
||||
return manager.isAlertEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last received battery level value.
|
||||
* @param device the device of which battery level should be returned
|
||||
* @return battery value or -1 if no value was received or Battery Level characteristic was not found
|
||||
*/
|
||||
public int getBatteryLevel(final BluetoothDevice device) {
|
||||
final ProximityManager manager = (ProximityManager) getBleManager(device);
|
||||
return manager.getBatteryLevel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -225,6 +236,7 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
|
||||
@Override
|
||||
public void onUnbind() {
|
||||
|
||||
createBackgroundNotification();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
@@ -63,14 +64,14 @@ public class RSCManager extends BleManager<RSCManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newEnableNotificationsRequest(mRSCMeasurementCharacteristic));
|
||||
return requests;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(RUNNING_SPEED_AND_CADENCE_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mRSCMeasurementCharacteristic = service.getCharacteristic(RSC_MEASUREMENT_CHARACTERISTIC_UUID);
|
||||
@@ -84,7 +85,7 @@ public class RSCManager extends BleManager<RSCManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
Logger.a(mLogSession, "\"" + RSCMeasurementParser.parse(characteristic) + "\" received");
|
||||
|
||||
// Decode the new data
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
@@ -64,7 +65,7 @@ public class TemplateManager extends BleManager<TemplateManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
// TODO initialize your device, enable required notifications and indications, write what needs to be written to start working
|
||||
requests.add(Request.newEnableNotificationsRequest(mCharacteristic));
|
||||
@@ -72,7 +73,7 @@ public class TemplateManager extends BleManager<TemplateManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mCharacteristic = service.getCharacteristic(MEASUREMENT_CHARACTERISTIC_UUID);
|
||||
@@ -96,7 +97,7 @@ public class TemplateManager extends BleManager<TemplateManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// TODO this method is called when a notification has been received
|
||||
// This method may be removed from this class if not required
|
||||
|
||||
@@ -114,19 +115,19 @@ public class TemplateManager extends BleManager<TemplateManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// TODO this method is called when an indication has been received
|
||||
// This method may be removed from this class if not required
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// TODO this method is called when the characteristic has been read
|
||||
// This method may be removed from this class if not required
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
protected void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// TODO this method is called when the characteristic has been written
|
||||
// This method may be removed from this class if not required
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@@ -66,14 +67,14 @@ public class UARTManager extends BleManager<UARTManagerCallbacks> {
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newEnableNotificationsRequest(mTXCharacteristic));
|
||||
return requests;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
|
||||
public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
final BluetoothGattService service = gatt.getService(UART_SERVICE_UUID);
|
||||
if (service != null) {
|
||||
mRXCharacteristic = service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID);
|
||||
@@ -103,7 +104,7 @@ public class UARTManager extends BleManager<UARTManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
// When the whole buffer has been sent
|
||||
final byte[] buffer = mOutgoingBuffer;
|
||||
if (mBufferOffset == buffer.length) {
|
||||
@@ -123,7 +124,7 @@ public class UARTManager extends BleManager<UARTManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
|
||||
public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
final String data = characteristic.getStringValue(0);
|
||||
Logger.a(mLogSession, "\"" + data + "\" received");
|
||||
mCallbacks.onDataReceived(gatt.getDevice(), data);
|
||||
|
||||
Reference in New Issue
Block a user