1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 14:32:54 +01:00

Added device information and reworked notifications

This commit is contained in:
dakhnod 2019-07-25 10:30:18 +02:00
parent 8775255d69
commit 8f996e04cd

View File

@ -4,6 +4,8 @@ import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -25,6 +27,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.NoSuchElementException;
import java.util.Queue; import java.util.Queue;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
@ -58,6 +61,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Pla
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.ReleaseHandsControlRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.ReleaseHandsControlRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Request; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.Request;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.RequestHandControlRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.RequestHandControlRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetCountdownSettings;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetCurrentTimeServiceRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetCurrentTimeServiceRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetStepGoalRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetStepGoalRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetVibrationStrengthRequest; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.SetVibrationStrengthRequest;
@ -111,6 +115,9 @@ public class QHybridSupport extends QHybridBaseSupport {
Queue<Request> requestQueue = new ArrayDeque<>(); Queue<Request> requestQueue = new ArrayDeque<>();
private String modelNumber;
public QHybridSupport() { public QHybridSupport() {
super(logger); super(logger);
addSupportedService(UUID.fromString("3dda0001-957f-7d4a-34a6-74696673696d")); addSupportedService(UUID.fromString("3dda0001-957f-7d4a-34a6-74696673696d"));
@ -127,7 +134,6 @@ public class QHybridSupport extends QHybridBaseSupport {
} }
private void fillResponseList() { private void fillResponseList() {
Class<? extends Request>[] classes = new Class[]{ Class<? extends Request>[] classes = new Class[]{
BatteryLevelRequest.class, BatteryLevelRequest.class,
GetStepGoalRequest.class, GetStepGoalRequest.class,
@ -194,9 +200,6 @@ public class QHybridSupport extends QHybridBaseSupport {
protected TransactionBuilder initializeDevice(TransactionBuilder builder) { protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext())); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
for (int i = 2; i <= 7; i++)
builder.notify(getCharacteristic(UUID.fromString("3dda000" + i + "-957f-7d4a-34a6-74696673696d")), true);
helper = new PackageConfigHelper(getContext()); helper = new PackageConfigHelper(getContext());
@ -212,6 +215,7 @@ public class QHybridSupport extends QHybridBaseSupport {
getTimeOffset(); getTimeOffset();
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext())); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
return builder; return builder;
} }
@ -219,20 +223,19 @@ public class QHybridSupport extends QHybridBaseSupport {
public void onServicesDiscovered(BluetoothGatt gatt) { public void onServicesDiscovered(BluetoothGatt gatt) {
super.onServicesDiscovered(gatt); super.onServicesDiscovered(gatt);
playAnimation();
requestQueue.add(new GetStepGoalRequest());
requestQueue.add(new GetCurrentStepCountRequest()); for (int i = 2; i <= 7; i++)
requestQueue.add(new GetVibrationStrengthRequest()); gatt.setCharacteristicNotification(getCharacteristic(UUID.fromString("3dda000" + i + "-957f-7d4a-34a6-74696673696d")), true);
requestQueue.add(new ActivityPointGetRequest());
queueWrite(new BatteryLevelRequest()); BluetoothGattService deviceInfo = gatt.getService(UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb"));
BluetoothGattCharacteristic modelNumber = deviceInfo.getCharacteristic(UUID.fromString("00002a24-0000-1000-8000-00805f9b34fb"));
gatt.readCharacteristic(modelNumber);
logger.debug("onServicesDiscovered"); logger.debug("onServicesDiscovered");
} }
private void playAnimation() {
queueWrite(new AnimationRequest());
}
@Override @Override
public void onNotification(NotificationSpec notificationSpec) { public void onNotification(NotificationSpec notificationSpec) {
Log.d("Service", "notif from " + notificationSpec.sourceAppId + " " + notificationSpec.sender + " " + notificationSpec.phoneNumber); Log.d("Service", "notif from " + notificationSpec.sourceAppId + " " + notificationSpec.sender + " " + notificationSpec.phoneNumber);
@ -305,6 +308,9 @@ public class QHybridSupport extends QHybridBaseSupport {
// queueWrite(new OTAResetRequest()); // queueWrite(new OTAResetRequest());
// new UploadFileRequest((short)00, new byte[]{0x01, 0x00, 0x08, 0x01, 0x01, 0x0C, 0x00, (byte)0xBD, 0x01, 0x30, 0x71, (byte)0xFF, 0x05, 0x00, 0x01, 0x00}); // new UploadFileRequest((short)00, new byte[]{0x01, 0x00, 0x08, 0x01, 0x01, 0x0C, 0x00, (byte)0xBD, 0x01, 0x30, 0x71, (byte)0xFF, 0x05, 0x00, 0x01, 0x00});
// queueWrite(new ActivityPointGetRequest()); // queueWrite(new ActivityPointGetRequest());
long millis = System.currentTimeMillis();
int secs = (int)(millis / 1000 * 60);
queueWrite(new SetCountdownSettings(secs, secs + 10, (short)120));
queueWrite(new GetCountdownSettingsRequest()); queueWrite(new GetCountdownSettingsRequest());
} }
@ -352,6 +358,48 @@ public class QHybridSupport extends QHybridBaseSupport {
} }
} }
@Override
public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
switch (characteristic.getUuid().toString()){
case "00002a24-0000-1000-8000-00805f9b34fb":{
modelNumber = characteristic.getStringValue(0);
gbDevice.setModel(modelNumber);
BluetoothGattService genericAccess = gatt.getService(UUID.fromString("00001800-0000-1000-8000-00805f9b34fb"));
BluetoothGattCharacteristic deviceName = genericAccess.getCharacteristic(UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb"));
gatt.readCharacteristic(deviceName);
break;
}
case "00002a00-0000-1000-8000-00805f9b34fb":{
String deviceName = characteristic.getStringValue(0);
gbDevice.setName(deviceName);
BluetoothGattService genericAccess = gatt.getService(UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb"));
BluetoothGattCharacteristic firmwareVersion = genericAccess.getCharacteristic(UUID.fromString("00002a26-0000-1000-8000-00805f9b34fb"));
gatt.readCharacteristic(firmwareVersion);
break;
}
case "00002a26-0000-1000-8000-00805f9b34fb":{
String firmwareVersion = characteristic.getStringValue(0);
gbDevice.setFirmwareVersion(firmwareVersion);
requestQueue.add(new GetStepGoalRequest());
requestQueue.add(new GetCurrentStepCountRequest());
requestQueue.add(new GetVibrationStrengthRequest());
requestQueue.add(new ActivityPointGetRequest());
requestQueue.add(new AnimationRequest());
queueWrite(new BatteryLevelRequest());
break;
}
}
return true;
}
@Override @Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
switch (characteristic.getUuid().toString()) { switch (characteristic.getUuid().toString()) {
@ -464,6 +512,7 @@ public class QHybridSupport extends QHybridBaseSupport {
if (request instanceof BatteryLevelRequest) { if (request instanceof BatteryLevelRequest) {
gbDevice.setBatteryLevel(((BatteryLevelRequest) request).level); gbDevice.setBatteryLevel(((BatteryLevelRequest) request).level);
gbDevice.setBatteryThresholdPercent((short)25);
} else if (request instanceof GetStepGoalRequest) { } else if (request instanceof GetStepGoalRequest) {
gbDevice.addDeviceInfo(new GenericItem(ITEM_STEP_GOAL, String.valueOf(((GetStepGoalRequest) request).stepGoal))); gbDevice.addDeviceInfo(new GenericItem(ITEM_STEP_GOAL, String.valueOf(((GetStepGoalRequest) request).stepGoal)));
} else if (request instanceof GetVibrationStrengthRequest) { } else if (request instanceof GetVibrationStrengthRequest) {
@ -507,10 +556,9 @@ public class QHybridSupport extends QHybridBaseSupport {
} else if (request instanceof ActivityPointGetRequest) { } else if (request instanceof ActivityPointGetRequest) {
gbDevice.addDeviceInfo(new GenericItem(ITEM_ACTIVITY_POINT, String.valueOf(((ActivityPointGetRequest) request).activityPoint))); gbDevice.addDeviceInfo(new GenericItem(ITEM_ACTIVITY_POINT, String.valueOf(((ActivityPointGetRequest) request).activityPoint)));
} }
Request nextRequest; try {
if((nextRequest = requestQueue.remove()) != null){ queueWrite(requestQueue.remove());
queueWrite(nextRequest); }catch (NoSuchElementException e){}
}
return true; return true;
} }