diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2014-12-06 19:25:51 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2014-12-06 19:25:51 +0100 |
commit | 76b17543bee2c38995fca07e21d5b271eddb7369 (patch) | |
tree | 7e3e8067a8327ffc5ab6ddca09da0989fd0db0f2 /app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java | |
parent | bb59449fbcbb46ba018313c51150924e7aec3be0 (diff) | |
download | fiken-display-android-master.tar.gz fiken-display-android-master.tar.bz2 fiken-display-android-master.tar.xz fiken-display-android-master.zip |
o Adding seek bars to each gauge. Sending updates to the device.
Diffstat (limited to 'app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java')
-rw-r--r-- | app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java | 265 |
1 files changed, 81 insertions, 184 deletions
diff --git a/app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java b/app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java index 53b00fc..9a4cbf0 100644 --- a/app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java +++ b/app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java @@ -16,14 +16,11 @@ import android.os.IBinder; import android.util.Log; import android.widget.Toast; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.List; -import java.util.Queue; +import java.util.concurrent.atomic.AtomicInteger; public class DefaultDisplayService extends Service implements DisplayService { - private final Context context = DefaultDisplayService.this; private final static String TAG = DefaultDisplayService.class.getSimpleName(); + private final Context context = DefaultDisplayService.this; private final IBinder binder = new LocalBinder(this); @@ -37,6 +34,8 @@ public class DefaultDisplayService extends Service implements DisplayService { BluetoothGattService displayService; BluetoothGattCharacteristic gaugeCtrl; BluetoothGattCharacteristic gaugeData; + + FikenStatusPanel fikenStatusPanel = new FikenStatusPanel(); } private Handler handler; @@ -113,9 +112,14 @@ public class DefaultDisplayService extends Service implements DisplayService { state = new State(); BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); - BtActionExecutor executor = new BtActionExecutor(); + final BtActionExecutor executor = new BtActionExecutor(new Runnable() { + @Override + public void run() { + disconnect(); + } + }); - executor.onSuccess(new BtCallback() { + executor.onSuccess(new BtCallback("Connect") { @Override public boolean onConnectionStateChange(BluetoothGatt gatt, int newState) { boolean ok = gatt.discoverServices(); @@ -131,12 +135,24 @@ public class DefaultDisplayService extends Service implements DisplayService { return true; } - }).onSuccess(new BtCallback() { + }).onSuccess(new BtCallback("Service discovered") { @Override public boolean onServicesDiscovered(BluetoothGatt gatt) { Log.i(TAG, "Services discovered"); state.displayService = gatt.getService(Constants.TRYGVIS_IO_FIKEN_STATUS_PANEL_UUID); + + if (state.displayService == null) { + Log.e(TAG, "Could not find display service on device."); + + Intent intent = IntentAction.DEVICE_UPDATE.intent(); + intent.putExtra(IntentExtra.DEVICE_ADDRESS.name(), address); + intent.putExtra(IntentExtra.DEVICE_IS_DISPLAY.name(), state.displayService != null); + sendBroadcast(intent); + + return false; + } + state.gaugeCtrl = state.displayService.getCharacteristic(Constants.TRYGVIS_IO_GAUGE_CTRL_UUID); state.gaugeData = state.displayService.getCharacteristic(Constants.TRYGVIS_IO_GAUGE_DATA_UUID); @@ -152,42 +168,55 @@ public class DefaultDisplayService extends Service implements DisplayService { gatt.setCharacteristicNotification(state.gaugeCtrl, true); return gatt.writeDescriptor(ccg); } - }).onSuccess(new BtCallback() { + }).onSuccess(new BtCallback("Get gauge count") { @Override public boolean onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) { - state.gaugeCtrl.setValue(new byte[]{0x01}); - gatt.writeCharacteristic(state.gaugeCtrl); - - return true; + state.gaugeCtrl.setValue(FikenStatusPanel.getGaugeCountReq()); + return gatt.writeCharacteristic(state.gaugeCtrl); } - }).onSuccess(new BtCallback() { + }).onSuccess(new BtCallback("data written") { @Override public boolean onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.w(TAG, "wait for it...!"); return true; } - }).onSuccess(new BtCallback() { + }).onSuccess(new BtCallback("Got gauge count") { @Override public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { byte[] value = state.gaugeCtrl.getValue(); - Log.w(TAG, "gauge bytes: " + toHexString(value)); - - if (value.length != 2) { - Log.w(TAG, "Bad response, expected value.length to be 2, was " + value.length); - return false; - } - - int gaugeCount = (int) value[1]; - - Log.d(TAG, "Gauge count=" + gaugeCount); + state.fikenStatusPanel.update(value); + int gaugeCount = state.fikenStatusPanel.gaugeCount(); Intent intent = IntentAction.DEVICE_UPDATE.intent(); intent.putExtra(IntentExtra.DEVICE_ADDRESS.name(), address); intent.putExtra(IntentExtra.GAUGE_COUNT.name(), gaugeCount); sendBroadcast(intent); - return true; + final AtomicInteger i = new AtomicInteger(0); + + executor.addFallback(updateGaugeValue); + executor.onSuccess(BtActionExecutor.waitForIt); + executor.onSuccess(new BtCallback("Get gauge value") { + @Override + public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { + updateGaugeValue.onCharacteristicChanged(gatt, characteristic); + + int next = i.incrementAndGet(); + + if (next == state.fikenStatusPanel.gaugeCount()) { + return true; + } + + executor.onSuccess(BtActionExecutor.waitForIt); + executor.onSuccess(this); + state.gaugeCtrl.setValue(FikenStatusPanel.getGaugeValueReq((byte) next)); + return gatt.writeCharacteristic(state.gaugeCtrl); + } + }); + + state.gaugeCtrl.setValue(FikenStatusPanel.getGaugeValueReq((byte) 0)); + return gatt.writeCharacteristic(state.gaugeCtrl); } }); @@ -211,164 +240,6 @@ public class DefaultDisplayService extends Service implements DisplayService { } } - private static class BtCallback { - public boolean onConnectionStateChange(BluetoothGatt gatt, int newState) { - throw new NotOverriddenException(); - } - - public boolean onServicesDiscovered(BluetoothGatt gatt) { - throw new NotOverriddenException(); - } - - public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - throw new NotOverriddenException(); - } - - public boolean onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - throw new NotOverriddenException(); - } - - public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - throw new NotOverriddenException(); - } - - public boolean onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) { - throw new NotOverriddenException(); - } - - public boolean onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) { - throw new NotOverriddenException(); - } - - public boolean onReliableWriteCompleted(BluetoothGatt gatt) { - throw new NotOverriddenException(); - } - - public boolean onReadRemoteRssi(BluetoothGatt gatt, int rssi) { - throw new NotOverriddenException(); - } - } - - private static class NotOverriddenException extends RuntimeException { - } - - private class BtActionExecutor { - Queue<BtCallback> actions = new ArrayDeque<BtCallback>(); - List<BluetoothGattCallback> remoteRssi = new ArrayList<BluetoothGattCallback>(); - - - public BtActionExecutor onSuccess(BtCallback btCallback) { - actions.add(btCallback); - return this; - } - - public BtActionExecutor onRemoteRssi(BluetoothGattCallback callback) { - remoteRssi.add(callback); - return this; - } - - public void onEvent(String key, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor, int status, int newState) { - if (status != BluetoothGatt.GATT_SUCCESS) { - Log.w(TAG, "Operation failed: " + key); - } - - Log.i(TAG, "Bt action completed successfully: callback=" + key); - - if (!actions.isEmpty()) { - BtCallback btCallback = actions.remove(); - Log.i(TAG, "Executing bt action"); - - boolean ok; - - try { - if (key.equals("onConnectionStateChange")) { - ok = btCallback.onConnectionStateChange(gatt, newState); - } else if (key.equals("onServicesDiscovered")) { - ok = btCallback.onServicesDiscovered(gatt); - } else if (key.equals("onCharacteristicRead")) { - ok = btCallback.onCharacteristicRead(gatt, characteristic); - } else if (key.equals("onCharacteristicWrite")) { - ok = btCallback.onCharacteristicWrite(gatt, characteristic); - } else if (key.equals("onCharacteristicChanged")) { - ok = btCallback.onCharacteristicChanged(gatt, characteristic); - } else if (key.equals("onDescriptorRead")) { - ok = btCallback.onDescriptorRead(gatt, descriptor); - } else if (key.equals("onDescriptorWrite")) { - ok = btCallback.onDescriptorWrite(gatt, descriptor); - } else if (key.equals("onReliableWriteCompleted")) { - ok = btCallback.onReliableWriteCompleted(gatt); - } else { - Log.w(TAG, "Unknown callback: " + key); - return; - } - } catch (NotOverriddenException e) { - Log.w(TAG, "Unexpected callback by listener: " + key); - disconnect(); - return; - } - - if (!ok) { - Log.w(TAG, "Error performing Bluetooth action."); - disconnect(); - } - } else { - Log.d(TAG, "All Bluetooth actions are done"); - } - } - - public BluetoothGattCallback asCallback() { - return new BluetoothGattCallback() { - - @Override - public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { - onEvent("onConnectionStateChange", gatt, null, null, status, newState); - } - - @Override - public void onServicesDiscovered(BluetoothGatt gatt, int status) { - onEvent("onServicesDiscovered", gatt, null, null, status, 9); - } - - @Override - public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - onEvent("onCharacteristicRead", gatt, characteristic, null, status, 0); - } - - @Override - public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - onEvent("onCharacteristicWrite", gatt, characteristic, null, status, 0); - } - - @Override - public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - onEvent("onCharacteristicChanged", gatt, characteristic, null, BluetoothGatt.GATT_SUCCESS, 0); - } - - @Override - public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - onEvent("onDescriptorRead", gatt, null, descriptor, status, 0); - } - - @Override - public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - onEvent("onDescriptorWrite", gatt, null, descriptor, status, 0); - } - - @Override - public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { - onEvent("onReliableWriteCompleted", gatt, null, null, status, 0); - } - - @Override - public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { - for (BluetoothGattCallback callback : remoteRssi) { - callback.onReadRemoteRssi(gatt, rssi, status); - } - } - }; - } - } - @Override public void disconnect() { if (serviceState != ServiceState.CONNECTED) { @@ -421,6 +292,17 @@ public class DefaultDisplayService extends Service implements DisplayService { serviceState = ServiceState.IDLE; } + @Override + public FikenStatusPanel getStatusPanel() { + return state != null ? state.fikenStatusPanel : null; + } + + @Override + public void setGauge(int gauge, int value) { + state.gaugeCtrl.setValue(FikenStatusPanel.setGaugeValueReq((byte) gauge, (byte) value)); + state.gatt.writeCharacteristic(state.gaugeCtrl); + } + private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { @@ -444,4 +326,19 @@ public class DefaultDisplayService extends Service implements DisplayService { return s.toString(); } + + private final BtCallback updateGaugeValue = new BtCallback("Update gauge value") { + @Override + public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { + if (state != null) { + state.fikenStatusPanel.update(state.gaugeCtrl.getValue()); + + Intent intent = IntentAction.DEVICE_UPDATE.intent(); + intent.putExtra(IntentExtra.DEVICE_ADDRESS.name(), gatt.getDevice().getAddress()); + intent.putExtra(IntentExtra.GAUGE_VALUE_CHANGED.name(), 0); + sendBroadcast(intent); + } + return true; + } + }; } |