1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 06:22:55 +01:00

Fixes from Code Review

This commit is contained in:
Andreas Böhler 2018-12-29 12:25:45 +01:00 committed by Carsten Pfeiffer
parent 76aebd4e20
commit 0009badd71
3 changed files with 127 additions and 89 deletions

View File

@ -29,8 +29,6 @@ import android.content.Context;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
import nodomain.freeyourgadget.gadgetbridge.devices.casiogb6900.CasioGB6900Constants; import nodomain.freeyourgadget.gadgetbridge.devices.casiogb6900.CasioGB6900Constants;
@ -47,12 +45,15 @@ class CasioGATTServer extends BluetoothGattServerCallback {
mDeviceSupport = deviceSupport; mDeviceSupport = deviceSupport;
} }
public void setContext(Context ctx) public void setContext(Context ctx) {
{
mContext = ctx; mContext = ctx;
} }
boolean initialize() { boolean initialize() {
if(mContext == null) {
return false;
}
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager == null) { if (bluetoothManager == null) {
return false; return false;
@ -81,6 +82,7 @@ class CasioGATTServer extends BluetoothGattServerCallback {
return true; return true;
} }
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) { public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
if (!characteristic.getUuid().equals(CasioGB6900Constants.NAME_OF_APP_CHARACTERISTIC_UUID)) { if (!characteristic.getUuid().equals(CasioGB6900Constants.NAME_OF_APP_CHARACTERISTIC_UUID)) {
@ -95,7 +97,7 @@ class CasioGATTServer extends BluetoothGattServerCallback {
} }
} }
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
@ -103,12 +105,16 @@ class CasioGATTServer extends BluetoothGattServerCallback {
LOG.warn("unexpected write request"); LOG.warn("unexpected write request");
return; return;
} }
if((value[0] & 0x03) == 0)
{ if(mDeviceSupport == null) {
LOG.warn("mDeviceSupport is null, did initialization complete?");
return;
}
if((value[0] & 0x03) == 0) {
int button = value[1] & 0x0f; int button = value[1] & 0x0f;
LOG.info("Button pressed: " + button); LOG.info("Button pressed: " + button);
switch(button) switch(button) {
{
case 3: case 3:
musicCmd.event = GBDeviceEventMusicControl.Event.NEXT; musicCmd.event = GBDeviceEventMusicControl.Event.NEXT;
break; break;
@ -118,18 +124,18 @@ class CasioGATTServer extends BluetoothGattServerCallback {
case 1: case 1:
musicCmd.event = GBDeviceEventMusicControl.Event.PLAYPAUSE; musicCmd.event = GBDeviceEventMusicControl.Event.PLAYPAUSE;
break; break;
case 0: default:
LOG.warn("Unhandled button received: " + button);
break; return;
} }
mDeviceSupport.evaluateGBDeviceEvent(musicCmd); mDeviceSupport.evaluateGBDeviceEvent(musicCmd);
} }
else else {
{
LOG.info("received from device: " + value.toString()); LOG.info("received from device: " + value.toString());
} }
} }
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
LOG.info("Connection state change for device: " + device.getAddress() + " status = " + status + " newState = " + newState); LOG.info("Connection state change for device: " + device.getAddress() + " status = " + status + " newState = " + newState);
@ -138,6 +144,7 @@ class CasioGATTServer extends BluetoothGattServerCallback {
} }
} }
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
@ -147,11 +154,12 @@ class CasioGATTServer extends BluetoothGattServerCallback {
} }
} }
@Override
public void onServiceAdded(int status, BluetoothGattService service) { public void onServiceAdded(int status, BluetoothGattService service) {
LOG.info("onServiceAdded() status = " + status + " service = " + service.getUuid()); LOG.info("onServiceAdded() status = " + status + " service = " + service.getUuid());
} }
@Override
public void onNotificationSent(BluetoothDevice bluetoothDevice, int status) { public void onNotificationSent(BluetoothDevice bluetoothDevice, int status) {
LOG.info("onNotificationSent() status = " + status + " to device " + bluetoothDevice.getAddress()); LOG.info("onNotificationSent() status = " + status + " to device " + bluetoothDevice.getAddress());
} }
@ -160,6 +168,7 @@ class CasioGATTServer extends BluetoothGattServerCallback {
if (mBluetoothGattServer != null) { if (mBluetoothGattServer != null) {
mBluetoothGattServer.clearServices(); mBluetoothGattServer.clearServices();
mBluetoothGattServer.close(); mBluetoothGattServer.close();
mBluetoothGattServer = null;
} }
} }

View File

@ -18,25 +18,31 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.casiogb6900; package nodomain.freeyourgadget.gadgetbridge.service.devices.casiogb6900;
import android.content.Context; import android.content.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CasioGATTThread extends Thread { public class CasioGATTThread extends Thread {
CasioGATTServer mServer = null; CasioGATTServer mServer = null;
private static final Logger LOG = LoggerFactory.getLogger(CasioGATTThread.class);
private boolean mStopFlag = false;
public CasioGATTThread(Context context, CasioGB6900DeviceSupport deviceSupport) public CasioGATTThread(Context context, CasioGB6900DeviceSupport deviceSupport)
{ {
mServer = new CasioGATTServer(context, deviceSupport); mServer = new CasioGATTServer(context, deviceSupport);
} }
public void setContext(Context ctx) public void setContext(Context ctx) {
{
mServer.setContext(ctx); mServer.setContext(ctx);
} }
@Override @Override
public void run() public void run()
{ {
mServer.initialize(); if(!mServer.initialize()) {
while(true) LOG.error("Error initializing CasioGATTServer. Has the context been set?");
{ return;
}
while(!mStopFlag) {
try { try {
wait(100); wait(100);
} catch(Exception e) } catch(Exception e)
@ -44,5 +50,12 @@ public class CasioGATTThread extends Thread {
} }
} }
mServer.close();
} }
public void quit() {
mStopFlag = true;
}
} }

View File

@ -105,8 +105,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
return builder; return builder;
} }
private void writeCasioCurrentTime(TransactionBuilder builder) private void writeCasioCurrentTime(TransactionBuilder builder) {
{
byte[] arr = new byte[10]; byte[] arr = new byte[10];
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
@ -135,8 +134,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
} }
} }
private void writeCasioLocalTimeInformation(TransactionBuilder builder) private void writeCasioLocalTimeInformation(TransactionBuilder builder) {
{
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
int zoneOffset = (int)TimeUnit.MILLISECONDS.toMinutes(cal.get(Calendar.ZONE_OFFSET)); int zoneOffset = (int)TimeUnit.MILLISECONDS.toMinutes(cal.get(Calendar.ZONE_OFFSET));
int dstOffset = (int)TimeUnit.MILLISECONDS.toMinutes(cal.get(Calendar.DST_OFFSET)); int dstOffset = (int)TimeUnit.MILLISECONDS.toMinutes(cal.get(Calendar.DST_OFFSET));
@ -152,8 +150,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
} }
private void writeCasioVirtualServerFeature(TransactionBuilder builder) private void writeCasioVirtualServerFeature(TransactionBuilder builder) {
{
byte byte0 = (byte)0; byte byte0 = (byte)0;
byte0 |= 1; // Casio Current Time Service byte0 |= 1; // Casio Current Time Service
byte0 |= 2; // Casio Alert Notification Service byte0 |= 2; // Casio Alert Notification Service
@ -169,59 +166,87 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
} }
} }
private boolean handleCasioCom(byte[] data) private boolean handleInitResponse(byte data) {
{
boolean handled = false; boolean handled = false;
switch(data[0]) // ServiceID - actually an int switch(data)
{ {
case 0: case (byte) 1:
switch(data[2]) LOG.info("Initialization done, setting state to INITIALIZED");
{ gbDevice.setState(GBDevice.State.INITIALIZED);
case (byte) 1: gbDevice.sendDeviceUpdateIntent(getContext());
LOG.info("Initialization done, setting state to INITIALIZED"); handled = true;
gbDevice.setState(GBDevice.State.INITIALIZED);
gbDevice.sendDeviceUpdateIntent(getContext());
break;
}
break; break;
case 2: default:
switch(data[2]) // Request Type LOG.warn("handleInitResponse: Error initializing device, received unexpected value: " + data);
{ gbDevice.setState(GBDevice.State.NOT_CONNECTED);
case (byte) 1: gbDevice.sendDeviceUpdateIntent(getContext());
try handled = true;
{
TransactionBuilder builder = createTransactionBuilder("writeCasioCurrentTime");
writeCasioCurrentTime(builder);
performImmediately(builder);
handled = true;
} catch (IOException e) {
LOG.warn(e.getMessage());
}
break;
case (byte) 2:
try
{
TransactionBuilder builder = createTransactionBuilder("writeCasioLocalTimeInformation");
writeCasioLocalTimeInformation(builder);
performImmediately(builder);
handled = true;
} catch (IOException e) {
LOG.warn(e.getMessage());
}
break;
}
break; break;
case 7: }
return handled;
}
private boolean handleTimeRequests(byte data) {
boolean handled = false;
switch(data) // Request Type
{
case (byte) 1:
try try
{ {
TransactionBuilder builder = createTransactionBuilder("writeCasioVirtualServerFeature"); TransactionBuilder builder = createTransactionBuilder("writeCasioCurrentTime");
writeCasioVirtualServerFeature(builder); writeCasioCurrentTime(builder);
performImmediately(builder); performImmediately(builder);
handled = true; handled = true;
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage()); LOG.warn("handleTimeRequests::writeCasioCurrentTime failed: " + e.getMessage());
} }
break; break;
case (byte) 2:
try
{
TransactionBuilder builder = createTransactionBuilder("writeCasioLocalTimeInformation");
writeCasioLocalTimeInformation(builder);
performImmediately(builder);
handled = true;
} catch (IOException e) {
LOG.warn("handleTimeRequests::writeCasioLocalTimeInformation failed: " + e.getMessage());
}
break;
}
return handled;
}
private boolean handleServerFeatureRequests(byte data) {
try
{
TransactionBuilder builder = createTransactionBuilder("writeCasioVirtualServerFeature");
writeCasioVirtualServerFeature(builder);
performImmediately(builder);
} catch (IOException e) {
LOG.warn("handleServerFeatureRequests failed: " + e.getMessage());
}
return true;
}
private boolean handleCasioCom(byte[] data) {
boolean handled = false;
if(data.length < 3) {
LOG.warn("handleCasioCom failed: Received unexpected request (too short)");
return false;
}
switch(data[0]) // ServiceID
{
case 0:
handled = handleInitResponse(data[2]);
break;
case 2:
handled = handleTimeRequests(data[2]);
break;
case 7:
handled = handleServerFeatureRequests(data[2]);
break;
} }
return handled; return handled;
} }
@ -240,32 +265,27 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
if (data.length == 0) if (data.length == 0)
return true; return true;
if(characteristicUUID.equals(CasioGB6900Constants.CASIO_A_NOT_W_REQ_NOT)) if(characteristicUUID.equals(CasioGB6900Constants.CASIO_A_NOT_W_REQ_NOT)) {
{
handled = handleCasioCom(data); handled = handleCasioCom(data);
} }
if(characteristicUUID.equals(CasioGB6900Constants.CASIO_A_NOT_COM_SET_NOT)) if(characteristicUUID.equals(CasioGB6900Constants.CASIO_A_NOT_COM_SET_NOT)) {
{
handled = handleCasioCom(data); handled = handleCasioCom(data);
} }
if(characteristicUUID.equals(CasioGB6900Constants.ALERT_LEVEL_CHARACTERISTIC_UUID)) if(characteristicUUID.equals(CasioGB6900Constants.ALERT_LEVEL_CHARACTERISTIC_UUID)) {
{
GBDeviceEventFindPhone findPhoneEvent = new GBDeviceEventFindPhone(); GBDeviceEventFindPhone findPhoneEvent = new GBDeviceEventFindPhone();
if(data[0] == 0x02) { if(data[0] == 0x02) {
findPhoneEvent.event = GBDeviceEventFindPhone.Event.START; findPhoneEvent.event = GBDeviceEventFindPhone.Event.START;
} }
else else {
{
findPhoneEvent.event = GBDeviceEventFindPhone.Event.STOP; findPhoneEvent.event = GBDeviceEventFindPhone.Event.STOP;
} }
evaluateGBDeviceEvent(findPhoneEvent); evaluateGBDeviceEvent(findPhoneEvent);
handled = true; handled = true;
} }
if(characteristicUUID.equals(CasioGB6900Constants.RINGER_CONTROL_POINT)) if(characteristicUUID.equals(CasioGB6900Constants.RINGER_CONTROL_POINT)) {
{
if(data[0] == 0x02) if(data[0] == 0x02)
{ {
LOG.info("Mute/ignore call event not yet supported by GB"); LOG.info("Mute/ignore call event not yet supported by GB");
@ -273,8 +293,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
handled = true; handled = true;
} }
if(!handled) if(!handled) {
{
LOG.info("Unhandled characteristic change: " + characteristicUUID + " code: " + String.format("0x%1x ...", data[0])); LOG.info("Unhandled characteristic change: " + characteristicUUID + " code: " + String.format("0x%1x ...", data[0]));
} }
return true; return true;
@ -299,7 +318,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
LOG.info("Showing notification, title: " + title + " message (not sent): " + message); LOG.info("Showing notification, title: " + title + " message (not sent): " + message);
performConnected(builder.getTransaction()); performConnected(builder.getTransaction());
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage()); LOG.warn("showNotification failed: " + e.getMessage());
} }
} }
@ -347,7 +366,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
writeCasioCurrentTime(builder); writeCasioCurrentTime(builder);
performConnected(builder.getTransaction()); performConnected(builder.getTransaction());
} catch(IOException e) { } catch(IOException e) {
LOG.warn(e.getMessage()); LOG.warn("onSetTime failed: " + e.getMessage());
} }
} }
@ -357,6 +376,9 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
case CallSpec.CALL_INCOMING: case CallSpec.CALL_INCOMING:
showNotification(CasioGB6900Constants.CALL_NOTIFICATION_ID, callSpec.name, callSpec.number); showNotification(CasioGB6900Constants.CALL_NOTIFICATION_ID, callSpec.name, callSpec.number);
break; break;
default:
LOG.info("not sending CallSpec since only CALL_INCOMING is handled");
break;
} }
} }
@ -367,7 +389,6 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetMusicState(MusicStateSpec stateSpec) { public void onSetMusicState(MusicStateSpec stateSpec) {
LOG.info("onSetMusicState");
if(stateSpec != mBufferMusicStateSpec) if(stateSpec != mBufferMusicStateSpec)
{ {
mBufferMusicStateSpec = stateSpec; mBufferMusicStateSpec = stateSpec;
@ -402,13 +423,12 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
builder.write(getCharacteristic(CasioGB6900Constants.MORE_ALERT_FOR_LONG_UUID), arr); builder.write(getCharacteristic(CasioGB6900Constants.MORE_ALERT_FOR_LONG_UUID), arr);
performConnected(builder.getTransaction()); performConnected(builder.getTransaction());
} catch (IOException e) { } catch (IOException e) {
LOG.warn(e.getMessage()); LOG.warn("sendMusicInfo failed: " + e.getMessage());
} }
} }
@Override @Override
public void onSetMusicInfo(MusicSpec musicSpec) { public void onSetMusicInfo(MusicSpec musicSpec) {
LOG.info("onSetMusicInfo");
if(musicSpec != mBufferMusicSpec) if(musicSpec != mBufferMusicSpec)
{ {
mBufferMusicSpec = musicSpec; mBufferMusicSpec = musicSpec;
@ -457,11 +477,7 @@ public class CasioGB6900DeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onReset(int flags) { public void onReset(int flags) {
try {
} catch(Exception e) {
LOG.warn(e.getMessage());
}
} }
@Override @Override