1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-02-21 06:46:48 +01:00

Fix reconnect and initialization

This commit is contained in:
Andreas Böhler 2020-11-16 21:59:31 +01:00 committed by Gitea
parent 92986724ee
commit ae02c825b4
2 changed files with 112 additions and 86 deletions

View File

@ -35,6 +35,7 @@ import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
@ -74,16 +75,14 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
addSupportedService(CasioConstants.WATCH_FEATURES_SERVICE_UUID); addSupportedService(CasioConstants.WATCH_FEATURES_SERVICE_UUID);
} }
/*
@Override @Override
public boolean connectFirstTime() { public boolean connectFirstTime() {
GB.toast(getContext(), "After first connect, disable and enable bluetooth on your Casio watch to really connect", Toast.LENGTH_SHORT, GB.INFO);
mFirstConnect = true; mFirstConnect = true;
return super.connect(); return connect();
} }
*/
public void setInitialized(TransactionBuilder builder) { public void setInitialized(TransactionBuilder builder) {
mFirstConnect = false;
builder.add(new SetDeviceStateAction(gbDevice, GBDevice.State.INITIALIZED, getContext())); builder.add(new SetDeviceStateAction(gbDevice, GBDevice.State.INITIALIZED, getContext()));
} }
@ -95,10 +94,9 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) { protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
LOG.info("Initializing");
try { try {
new InitOperationGBX100(this, builder).perform(); new InitOperationGBX100(this, builder, mFirstConnect).perform();
} catch (IOException e) { } catch (IOException e) {
GB.toast(getContext(), "Initializing Casio watch failed", Toast.LENGTH_SHORT, GB.ERROR, e); GB.toast(getContext(), "Initializing Casio watch failed", Toast.LENGTH_SHORT, GB.ERROR, e);
} }
@ -106,8 +104,6 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
getDevice().setFirmwareVersion("N/A"); getDevice().setFirmwareVersion("N/A");
getDevice().setFirmwareVersion2("N/A"); getDevice().setFirmwareVersion2("N/A");
LOG.info("Initialization Done");
return builder; return builder;
} }
@ -143,19 +139,29 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
return true; return true;
} }
private void showNotification(byte icon, String title, String message, int id) { private void showNotification(byte icon, String sender, String title, String message, int id, boolean delete) {
byte[] titleBytes = title.getBytes(StandardCharsets.UTF_8); byte[] titleBytes = new byte[0];
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); if(title != null)
titleBytes = title.getBytes(StandardCharsets.UTF_8);
byte[] messageBytes = new byte[0];
if(message != null)
messageBytes = message.getBytes(StandardCharsets.UTF_8);
byte[] senderBytes = new byte[0];
if(sender != null)
senderBytes = sender.getBytes(StandardCharsets.UTF_8);
byte[] arr = new byte[22]; byte[] arr = new byte[22];
arr[0] = (byte) 0x03; // (byte)(id & 0xff); arr[0] = (byte)(id & 0xff);
arr[1] = (byte) 0x00; //((id >> 8) & 0xff); arr[1] = (byte) ((id >> 8) & 0xff);
arr[2] = (byte) 0x00; // ((id >> 16) & 0xff); arr[2] = (byte) ((id >> 16) & 0xff);
arr[3] = (byte) 0x00; // ((id >> 24) & 0xff); arr[3] = (byte) ((id >> 24) & 0xff);
arr[4] = 0x00; arr[4] = delete ? (byte) 0x02 : (byte) 0x00;
arr[5] = (byte) 0x01; arr[5] = (byte) 0x01; // Set to 0x00 to not vibrate/ring for this notification
arr[6] = icon; arr[6] = icon;
arr[7] = (byte) 0x32; // These bytes contain a timestamp, not yet decoded / implemented
/*arr[7] = (byte) 0x32;
arr[8] = (byte) 0x30; arr[8] = (byte) 0x30;
arr[9] = (byte) 0x32; arr[9] = (byte) 0x32;
arr[10] = (byte) 0x30; arr[10] = (byte) 0x30;
@ -169,11 +175,16 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
arr[18] = (byte) 0x33; arr[18] = (byte) 0x33;
arr[19] = (byte) 0x31; arr[19] = (byte) 0x31;
arr[20] = (byte) 0x35; arr[20] = (byte) 0x35;
arr[21] = (byte) 0x33; arr[21] = (byte) 0x33;*/
byte[] copy = Arrays.copyOf(arr, arr.length + 2); byte[] copy = Arrays.copyOf(arr, arr.length + 2);
copy[copy.length-2] = 0; copy[copy.length-2] = 0;
copy[copy.length-1] = 0; copy[copy.length-1] = 0;
// appName is currently not supported if(senderBytes.length > 0) {
copy = Arrays.copyOf(copy, copy.length + senderBytes.length);
copy[copy.length-2-senderBytes.length] = (byte)(senderBytes.length & 0xff);
copy[copy.length-1-senderBytes.length] = (byte)((senderBytes.length >> 8) & 0xff);
System.arraycopy(senderBytes, 0, copy, copy.length - senderBytes.length, senderBytes.length);
}
copy = Arrays.copyOf(copy, copy.length + 2); copy = Arrays.copyOf(copy, copy.length + 2);
copy[copy.length-2] = 0; copy[copy.length-2] = 0;
copy[copy.length-1] = 0; copy[copy.length-1] = 0;
@ -209,30 +220,60 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
LOG.warn("showNotification failed: " + e.getMessage()); LOG.warn("showNotification failed: " + e.getMessage());
} }
} }
@Override @Override
public void onNotification(NotificationSpec notificationSpec) { public void onNotification(NotificationSpec notificationSpec) {
String notificationTitle = StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title);
byte icon; byte icon;
switch (notificationSpec.type) { switch (notificationSpec.type.getGenericType()) {
case GENERIC_SMS: case "generic_calendar":
icon = CasioConstants.CATEGORY_EMAIL;
break;
case GENERIC_CALENDAR:
icon = CasioConstants.CATEGORY_SCHEDULE_AND_ALARM; icon = CasioConstants.CATEGORY_SCHEDULE_AND_ALARM;
break; break;
case GENERIC_EMAIL: case "generic_email":
icon = CasioConstants.CATEGORY_EMAIL; icon = CasioConstants.CATEGORY_EMAIL;
break; break;
default: default:
icon = CasioConstants.CATEGORY_SNS; icon = CasioConstants.CATEGORY_SNS;
break; break;
} }
showNotification(icon, notificationTitle, notificationSpec.body, notificationSpec.getId()); LOG.info("onNotification id=" + notificationSpec.getId());
showNotification(icon, notificationSpec.sender, notificationSpec.title, notificationSpec.body, notificationSpec.getId(), false);
} }
@Override @Override
public void onDeleteNotification(int id) { public void onDeleteNotification(int id) {
LOG.info("onDeleteNofication id=" + id);
showNotification(CasioConstants.CATEGORY_OTHER, null, null, null, id, true);
}
public void writeCurrentTime(TransactionBuilder builder) {
byte[] arr = new byte[11];
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
arr[0] = CasioConstants.characteristicToByte.get("CASIO_CURRENT_TIME");
arr[1] = (byte)((year >>> 0) & 0xff);
arr[2] = (byte)((year >>> 8) & 0xff);
arr[3] = (byte)(1 + cal.get(Calendar.MONTH));
arr[4] = (byte)cal.get(Calendar.DAY_OF_MONTH);
arr[5] = (byte)cal.get(Calendar.HOUR_OF_DAY);
arr[6] = (byte)cal.get(Calendar.MINUTE);
arr[7] = (byte)(1 + cal.get(Calendar.SECOND));
byte dayOfWk = (byte)(cal.get(Calendar.DAY_OF_WEEK) - 1);
if(dayOfWk == 0)
dayOfWk = 7;
arr[8] = dayOfWk;
arr[9] = (byte)(int) TimeUnit.MILLISECONDS.toSeconds(256 * cal.get(Calendar.MILLISECOND));
arr[10] = 1; // or 0?
writeAllFeatures(builder, arr);
}
public void writeAllFeatures(TransactionBuilder builder, byte[] arr) {
builder.write(getCharacteristic(CasioConstants.CASIO_ALL_FEATURES_CHARACTERISTIC_UUID), arr);
}
public void writeAllFeaturesRequest(TransactionBuilder builder, byte[] arr) {
builder.write(getCharacteristic(CasioConstants.CASIO_READ_REQUEST_FOR_ALL_FEATURES_CHARACTERISTIC_UUID), arr);
} }
@Override @Override
@ -242,14 +283,22 @@ public class CasioGBX100DeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetTime() { public void onSetTime() {
LOG.debug("onSetTime called");
try {
TransactionBuilder builder = performInitialized("onSetTime");
writeCurrentTime(builder);
builder.queue(getQueue());
} catch (IOException e) {
LOG.warn("onSetTime failed: " + e.getMessage());
}
} }
@Override @Override
public void onSetCallState(CallSpec callSpec) { public void onSetCallState(CallSpec callSpec) {
final AtomicInteger c = new AtomicInteger((int) (System.currentTimeMillis()/1000));
switch (callSpec.command) { switch (callSpec.command) {
case CallSpec.CALL_INCOMING: case CallSpec.CALL_INCOMING:
showNotification(CasioConstants.CATEGORY_INCOMING_CALL, callSpec.name, callSpec.number, 0x9876); showNotification(CasioConstants.CATEGORY_INCOMING_CALL, "Phone", callSpec.name, callSpec.number, c.incrementAndGet(), false);
break; break;
default: default:
LOG.info("not sending CallSpec since only CALL_INCOMING is handled"); LOG.info("not sending CallSpec since only CALL_INCOMING is handled");

View File

@ -39,38 +39,34 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
private final TransactionBuilder builder; private final TransactionBuilder builder;
private final CasioGBX100DeviceSupport support; private final CasioGBX100DeviceSupport support;
private final boolean mFirstConnect;
private boolean mWriteAllFeaturesInitPending = false;
public InitOperationGBX100(CasioGBX100DeviceSupport support, TransactionBuilder builder) { public InitOperationGBX100(CasioGBX100DeviceSupport support, TransactionBuilder builder, boolean firstConnect) {
super(support); super(support);
this.builder = builder; this.builder = builder;
this.support = support; this.support = support;
this.mFirstConnect = firstConnect;
LOG.info("mFirstConnect: " + mFirstConnect);
builder.setGattCallback(this); builder.setGattCallback(this);
} }
private void writeAllFeaturesRequest(TransactionBuilder builder, byte[] arr) {
builder.write(getCharacteristic(CasioConstants.CASIO_READ_REQUEST_FOR_ALL_FEATURES_CHARACTERISTIC_UUID), arr);
}
private void writeAllFeaturesRequest(byte[] arr) { private void writeAllFeaturesRequest(byte[] arr) {
try { try {
TransactionBuilder builder = createTransactionBuilder("writeAllFeaturesRequest"); TransactionBuilder builder = createTransactionBuilder("writeAllFeaturesRequest");
builder.setGattCallback(this); builder.setGattCallback(this);
writeAllFeaturesRequest(builder, arr); support.writeAllFeaturesRequest(builder, arr);
support.performImmediately(builder); support.performImmediately(builder);
} catch(IOException e) { } catch(IOException e) {
LOG.error("Error writing all features: " + e.getMessage()); LOG.error("Error writing all features: " + e.getMessage());
} }
} }
private void writeAllFeatures(TransactionBuilder builder, byte[] arr) {
builder.write(getCharacteristic(CasioConstants.CASIO_ALL_FEATURES_CHARACTERISTIC_UUID), arr);
}
private void writeAllFeatures(byte[] arr) { private void writeAllFeatures(byte[] arr) {
try { try {
TransactionBuilder builder = createTransactionBuilder("writeAllFeatures"); TransactionBuilder builder = createTransactionBuilder("writeAllFeatures");
builder.setGattCallback(this); builder.setGattCallback(this);
writeAllFeatures(builder, arr); support.writeAllFeatures(builder, arr);
support.performImmediately(builder); support.performImmediately(builder);
} catch(IOException e) { } catch(IOException e) {
LOG.error("Error writing all features: " + e.getMessage()); LOG.error("Error writing all features: " + e.getMessage());
@ -82,8 +78,10 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
arr[0] = 0x00; arr[0] = 0x00;
arr[1] = 0x01; arr[1] = 0x01;
mWriteAllFeaturesInitPending = true;
if(builder != null) if(builder != null)
writeAllFeatures(builder, arr); support.writeAllFeatures(builder, arr);
else else
writeAllFeatures(arr); writeAllFeatures(arr);
} }
@ -93,7 +91,7 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
} }
private void requestWatchName(TransactionBuilder builder) { private void requestWatchName(TransactionBuilder builder) {
writeAllFeaturesRequest(builder, new byte[]{CasioConstants.characteristicToByte.get("CASIO_WATCH_NAME")}); support.writeAllFeaturesRequest(builder, new byte[]{CasioConstants.characteristicToByte.get("CASIO_WATCH_NAME")});
} }
private void requestWatchName() { private void requestWatchName() {
@ -251,32 +249,6 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
writeAllFeatures(arr); writeAllFeatures(arr);
} }
private void writeCurrentTime(TransactionBuilder builder) {
byte[] arr = new byte[11];
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
arr[0] = CasioConstants.characteristicToByte.get("CASIO_CURRENT_TIME");
arr[1] = (byte)((year >>> 0) & 0xff);
arr[2] = (byte)((year >>> 8) & 0xff);
arr[3] = (byte)(1 + cal.get(Calendar.MONTH));
arr[4] = (byte)cal.get(Calendar.DAY_OF_MONTH);
arr[5] = (byte)cal.get(Calendar.HOUR_OF_DAY);
arr[6] = (byte)cal.get(Calendar.MINUTE);
arr[7] = (byte)(1 + cal.get(Calendar.SECOND));
byte dayOfWk = (byte)(cal.get(Calendar.DAY_OF_WEEK) - 1);
if(dayOfWk == 0)
dayOfWk = 7;
arr[8] = dayOfWk;
arr[9] = (byte)(int) TimeUnit.MILLISECONDS.toSeconds(256 * cal.get(Calendar.MILLISECOND));
arr[10] = 1; // or 0?
if(builder == null)
writeAllFeatures(arr);
else
writeAllFeatures(builder, arr);
}
private void enableAllFeatures(boolean enable) { private void enableAllFeatures(boolean enable) {
try { try {
TransactionBuilder builder = createTransactionBuilder("notifyAllFeatures"); TransactionBuilder builder = createTransactionBuilder("notifyAllFeatures");
@ -294,7 +266,6 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
@Override @Override
protected void doPerform() throws IOException { protected void doPerform() throws IOException {
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext())); builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
enableAllFeatures(builder, true); enableAllFeatures(builder, true);
requestWatchName(builder); requestWatchName(builder);
@ -314,51 +285,58 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
if(data.length == 0) if(data.length == 0)
return true; return true;
// Abort a stalled allFeaturesInit write
if(mWriteAllFeaturesInitPending) {
getQueue().abortCurrentTransaction();
mWriteAllFeaturesInitPending = false;
}
if(characteristicUUID.equals(CasioConstants.CASIO_ALL_FEATURES_CHARACTERISTIC_UUID)) { if(characteristicUUID.equals(CasioConstants.CASIO_ALL_FEATURES_CHARACTERISTIC_UUID)) {
if(data[0] == CasioConstants.characteristicToByte.get("CASIO_WATCH_NAME")) { if(data[0] == CasioConstants.characteristicToByte.get("CASIO_WATCH_NAME")) {
LOG.info("Got watch name, requesting BLE features; should write CASIO_APP_INFORMATION"); LOG.debug("Got watch name, requesting BLE features; should write CASIO_APP_INFORMATION");
writeAppInformation(); writeAppInformation();
// The rest of the init sequence is not strictly needed; we keep // The rest of the init sequence is not strictly needed; we keep
// it here for future reference. // it here for future reference.
//requestBleConfiguration(); if(mFirstConnect)
requestBleConfiguration();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_BLE_FEATURES")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_BLE_FEATURES")) {
LOG.info("Got BLE features, requesting BLE settings"); LOG.debug("Got BLE features, requesting BLE settings");
requestBleSettings(); requestBleSettings();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_SETTING_FOR_BLE")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_SETTING_FOR_BLE")) {
LOG.info("Got BLE settings, requesting advertising parameters; should write BLE settings"); LOG.debug("Got BLE settings, requesting advertising parameters; should write BLE settings");
writeBleSettings(data); writeBleSettings(data);
requestAdvertisingParameters(); requestAdvertisingParameters();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_ADVERTISE_PARAMETER_MANAGER")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_ADVERTISE_PARAMETER_MANAGER")) {
LOG.info("Got advertising parameters, requesting connection parameters; should write advertising parameters"); LOG.debug("Got advertising parameters, requesting connection parameters; should write advertising parameters");
writeAdvertisingParameters(); writeAdvertisingParameters();
requestConnectionParameters(); requestConnectionParameters();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_CONNECTION_PARAMETER_MANAGER")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_CONNECTION_PARAMETER_MANAGER")) {
LOG.info("Got connection parameters, requesting module ID; should write connection parameters"); LOG.debug("Got connection parameters, requesting module ID; should write connection parameters");
writeConnectionParameters(); writeConnectionParameters();
requestModuleId(); requestModuleId();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_MODULE_ID")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_MODULE_ID")) {
LOG.info("Got module ID, requesting watch condition"); LOG.debug("Got module ID, requesting watch condition");
requestWatchCondition(); requestWatchCondition();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_WATCH_CONDITION")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_WATCH_CONDITION")) {
LOG.info("Got watch condition, requesting version information"); LOG.debug("Got watch condition, requesting version information");
requestVersionInformation(); requestVersionInformation();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_VERSION_INFORMATION")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_VERSION_INFORMATION")) {
LOG.info("Got version information, requesting DST watch state"); LOG.debug("Got version information, requesting DST watch state");
requestDstWatchState(); requestDstWatchState();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_DST_WATCH_STATE")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_DST_WATCH_STATE")) {
LOG.info("Got DST watch state, requesting DST setting; should write DST watch state"); LOG.debug("Got DST watch state, requesting DST setting; should write DST watch state");
requestDstSetting(); requestDstSetting();
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_DST_SETTING")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_DST_SETTING")) {
LOG.info("Got DST setting, waiting...; should write DST setting and location and radio information"); LOG.debug("Got DST setting, waiting...; should write DST setting and location and radio information");
} else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_SERVICE_DISCOVERY_MANAGER")) { } else if(data[0] == CasioConstants.characteristicToByte.get("CASIO_SERVICE_DISCOVERY_MANAGER")) {
if(data[1] == 0x02) { if(data[1] == 0x02) {
// The writeAllFeaturesInit request triggers bonding. However, the transaction // The writeAllFeaturesInit request triggers bonding. However, the transaction
// never completes. Instead, the watch sends 0x4701 notification and we abort // never completes. Instead, the watch sends 0x4701 notification and we abort
// the current transaction. // the current transaction.
LOG.info("We need to bond here. This is actually the request for the current time."); LOG.debug("We need to bond here. This is actually the request for the current time.");
try { try {
TransactionBuilder builder = createTransactionBuilder("writeCurrentTime"); TransactionBuilder builder = createTransactionBuilder("writeCurrentTime");
writeCurrentTime(builder); support.writeCurrentTime(builder);
writeAllFeaturesInit(builder); writeAllFeaturesInit(builder);
support.performImmediately(builder); support.performImmediately(builder);
} catch(IOException e) { } catch(IOException e) {
@ -368,13 +346,12 @@ public class InitOperationGBX100 extends AbstractBTLEOperation<CasioGBX100Device
// The writeAllFeaturesInit request triggers encryption (again). However, the transaction // The writeAllFeaturesInit request triggers encryption (again). However, the transaction
// never completes. Instead, the watch reports with 0x3d and we abort the current // never completes. Instead, the watch reports with 0x3d and we abort the current
// transaction. // transaction.
getQueue().abortCurrentTransaction(); if(mFirstConnect)
writeAllFeaturesInit(); writeAllFeaturesInit();
} }
} else if(data[0] == 0x3d) { } else if(data[0] == 0x3d) {
LOG.info("Init operation done."); LOG.info("Init operation done.");
// Finally, we set the state to initialized here! // Finally, we set the state to initialized here!
getQueue().abortCurrentTransaction();
try { try {
TransactionBuilder builder = createTransactionBuilder("setInitialized"); TransactionBuilder builder = createTransactionBuilder("setInitialized");
support.setInitialized(builder); support.setInitialized(builder);