From 1784a0ee9bfc3937aef6f8cb1f9404c4817ee946 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 2 Dec 2014 20:04:28 +0100 Subject: o Fixing small bugs, enabling notification reception. --- .../main/java/no/topi/fiken/display/Constants.java | 5 +- .../topi/fiken/display/DefaultDisplayService.java | 56 +++++++++++++++++++--- .../java/no/topi/fiken/display/MainActivity.java | 10 +++- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/no/topi/fiken/display/Constants.java b/app/src/main/java/no/topi/fiken/display/Constants.java index f568e1c..de93938 100644 --- a/app/src/main/java/no/topi/fiken/display/Constants.java +++ b/app/src/main/java/no/topi/fiken/display/Constants.java @@ -5,6 +5,9 @@ import java.util.UUID; public interface Constants { String TRYGVIS_IO_BASE_UUID = "32D0xxxx-035D-59C5-70D3-BC8E4A1FD83F"; UUID TRYGVIS_IO_FIKEN_STATUS_PANEL_UUID = UUID.fromString(TRYGVIS_IO_BASE_UUID.replace("xxxx", "0001")); - UUID TRYGVIS_IO_GAUGE_UUID = UUID.fromString(TRYGVIS_IO_BASE_UUID.replace("xxxx", "0002")); + UUID TRYGVIS_IO_GAUGE_DATA_UUID = UUID.fromString(TRYGVIS_IO_BASE_UUID.replace("xxxx", "0002")); + UUID TRYGVIS_IO_GAUGE_CTRL_UUID = UUID.fromString(TRYGVIS_IO_BASE_UUID.replace("xxxx", "0004")); UUID TRYGVIS_IO_LED_UUID = UUID.fromString(TRYGVIS_IO_BASE_UUID.replace("xxxx", "0003")); + + UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); } 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 b1aa2e7..3bd2dd0 100644 --- a/app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java +++ b/app/src/main/java/no/topi/fiken/display/DefaultDisplayService.java @@ -16,6 +16,9 @@ import android.os.IBinder; import android.util.Log; import android.widget.Toast; +import java.lang.reflect.Array; +import java.util.Arrays; + public class DefaultDisplayService extends Service implements DisplayService { private final Context context = DefaultDisplayService.this; private final static String TAG = DefaultDisplayService.class.getSimpleName(); @@ -24,16 +27,18 @@ public class DefaultDisplayService extends Service implements DisplayService { private BluetoothManager mBluetoothManager; private BluetoothAdapter mBluetoothAdapter; - private BluetoothGattService displayService; private BluetoothGatt gatt; + private BluetoothGattService displayService; + private BluetoothGattCharacteristic gaugeCtrl; + private BluetoothGattCharacteristic gaugeData; private Handler handler; - private int UPDATE_RSSI_DELAY = 1000; + private int UPDATE_RSSI_DELAY = 100 * 1000; private Runnable updateRssi = new Runnable() { @Override public void run() { - if(gatt != null) { + if (gatt != null) { gatt.readRemoteRssi(); } @@ -132,28 +137,65 @@ public class DefaultDisplayService extends Service implements DisplayService { } displayService = gatt.getService(Constants.TRYGVIS_IO_FIKEN_STATUS_PANEL_UUID); + gaugeCtrl = displayService.getCharacteristic(Constants.TRYGVIS_IO_GAUGE_CTRL_UUID); + gaugeData = displayService.getCharacteristic(Constants.TRYGVIS_IO_GAUGE_DATA_UUID); - Log.i(TAG, "service=" + displayService); + Log.i(TAG, "service=" + displayService + ", gaugeCtrl=" + gaugeCtrl + ", gaugeData=" + gaugeData); Intent intent = IntentAction.DEVICE_UPDATE.intent(); intent.putExtra(IntentExtra.DEVICE_ADDRESS.name(), address); intent.putExtra(IntentExtra.DEVICE_IS_DISPLAY.name(), displayService != null); sendBroadcast(intent); + + BluetoothGattDescriptor ccg = gaugeCtrl.getDescriptor(Constants.CLIENT_CHARACTERISTIC_CONFIG); + ccg.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); + gatt.writeDescriptor(ccg); + Log.i(TAG, "ccg=" + ccg); + + // Send a request for gauge count. + +// gaugeCtrl.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); +// gaugeCtrl.setValue(new byte[]{0x01}); +// gaugeCtrl.getDe +// gatt.setCharacteristicNotification(gaugeCtrl, true); +// gatt.writeCharacteristic(gaugeCtrl); +// gatt.readCharacteristic(gaugeCtrl); } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.i(TAG, "onCharacteristicRead"); + + String s = ""; + for (byte b : characteristic.getValue()) { + s += Integer.toHexString(b); + } + + Log.i(TAG, "uuid=" + characteristic.getUuid() + ", value=" + s); } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.i(TAG, "onCharacteristicWrite"); + + String s = ""; + for (byte b : characteristic.getValue()) { + s += Integer.toHexString(b); + } + + Log.i(TAG, "uuid=" + characteristic.getUuid() + ", value=0x" + s + ", status=" + status); } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.i(TAG, "onCharacteristicChanged"); + + String s = ""; + for (byte b : characteristic.getValue()) { + s += Integer.toHexString(b); + } + + Log.i(TAG, "uuid=" + characteristic.getUuid() + ", value=0x" + s); } @Override @@ -163,17 +205,17 @@ public class DefaultDisplayService extends Service implements DisplayService { @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - Log.i(TAG, "onDescriptorWrite"); + Log.i(TAG, "onDescriptorWrite, status=" + status + ", descriptor=" + descriptor.getUuid()); } @Override public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { - Log.i(TAG, "onReliableWriteCompleted"); + Log.i(TAG, "onReliableWriteCompleted, status=" + status); } @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { - Log.i(TAG, "onReadRemoteRssi, status=" + status + ", rssi=" + rssi); +// Log.i(TAG, "onReadRemoteRssi, status=" + status + ", rssi=" + rssi); if (status == BluetoothGatt.GATT_SUCCESS) { Intent intent = IntentAction.DEVICE_UPDATE.intent(); intent.putExtra(IntentExtra.DEVICE_ADDRESS.name(), gatt.getDevice().getAddress()); diff --git a/app/src/main/java/no/topi/fiken/display/MainActivity.java b/app/src/main/java/no/topi/fiken/display/MainActivity.java index d9dc073..6c21afe 100644 --- a/app/src/main/java/no/topi/fiken/display/MainActivity.java +++ b/app/src/main/java/no/topi/fiken/display/MainActivity.java @@ -48,6 +48,8 @@ public class MainActivity extends ListActivity { private DisplayService displayService; private String deviceToShow; + private ServiceConnection serviceConnection; + @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate"); @@ -78,7 +80,7 @@ public class MainActivity extends ListActivity { finish(); } - ServiceConnection serviceConnection = new ServiceConnection() { + serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder service) { @@ -101,6 +103,11 @@ public class MainActivity extends ListActivity { bindService(displayServiceIntent, serviceConnection, BIND_AUTO_CREATE); } + @Override + protected void onDestroy() { + unbindService(serviceConnection); + } + @Override protected void onResume() { Log.i(TAG, "onResume"); @@ -123,6 +130,7 @@ public class MainActivity extends ListActivity { super.onPause(); stopScan(); + unregisterReceiver(displayServiceBroadcastReceiver); } @Override -- cgit v1.2.3