From 41b63780d714a9bed1fbec6a77a0874f136948f0 Mon Sep 17 00:00:00 2001 From: Louis-Guillaume DUBOIS Date: Tue, 6 Jun 2017 17:21:21 +0200 Subject: [PATCH] =?UTF-8?q?Utilisation=20d=E2=80=99UUID=C2=A0al=C3=A9atoir?= =?UTF-8?q?e=20(v4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../students/clientble/BluetoothLeService.java | 11 ++++------- .../students/clientble/DeviceControlActivity.java | 4 ++-- .../students/clientble/DeviceScanActivity.java | 4 ++-- ...{SampleGattAttributes.java => GattConstants.java} | 12 ++++++++---- .../students/clientble/SimpleDetailActivity.java | 7 +------ 5 files changed, 17 insertions(+), 21 deletions(-) rename Application/src/main/java/fr/centralesupelec/students/clientble/{SampleGattAttributes.java => GattConstants.java} (88%) diff --git a/Application/src/main/java/fr/centralesupelec/students/clientble/BluetoothLeService.java b/Application/src/main/java/fr/centralesupelec/students/clientble/BluetoothLeService.java index e06d8a6..e5e7e30 100644 --- a/Application/src/main/java/fr/centralesupelec/students/clientble/BluetoothLeService.java +++ b/Application/src/main/java/fr/centralesupelec/students/clientble/BluetoothLeService.java @@ -36,9 +36,6 @@ import android.util.Log; import java.text.DateFormat; import java.util.Date; import java.util.List; -import java.util.Locale; -import java.util.StringTokenizer; -import java.util.UUID; import static java.lang.Thread.sleep; @@ -132,7 +129,7 @@ public class BluetoothLeService extends Service { Log.d(TAG, "broadcastUpdate(String, BluetoothGattChar.) appelé."); final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { - if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { + if (GattConstants.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { final long value = (data.length == 2) ? (data[0] << 8) & 0x0000ff00 | (data[1] << 0) & 0x000000ff : (data[0] << 0) & 0x000000ff; @@ -295,8 +292,8 @@ public class BluetoothLeService extends Service { return; } Log.d(TAG, "setChar.Notification() appelé"); - if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(SampleGattAttributes.CHARACTERISTIC_CONFIG_UUID); + if (GattConstants.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { + BluetoothGattDescriptor descriptor = characteristic.getDescriptor(GattConstants.CHARACTERISTIC_CONFIG_UUID); descriptor.setValue( enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE @@ -324,6 +321,6 @@ public class BluetoothLeService extends Service { public BluetoothGattService getPrivateService() { if (mBluetoothGatt == null) return null; - return mBluetoothGatt.getService(SampleGattAttributes.PRIVATE_SERVICE_UUID); + return mBluetoothGatt.getService(GattConstants.PRIVATE_SERVICE_UUID); } } diff --git a/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceControlActivity.java b/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceControlActivity.java index a9d0daf..9f1230b 100644 --- a/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceControlActivity.java +++ b/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceControlActivity.java @@ -260,7 +260,7 @@ public class DeviceControlActivity extends Activity { HashMap currentServiceData = new HashMap(); uuid = gattService.getUuid().toString(); currentServiceData.put( - LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString)); + LIST_NAME, GattConstants.lookup(uuid, unknownServiceString)); currentServiceData.put(LIST_UUID, uuid); gattServiceData.add(currentServiceData); @@ -277,7 +277,7 @@ public class DeviceControlActivity extends Activity { HashMap currentCharaData = new HashMap(); uuid = gattCharacteristic.getUuid().toString(); currentCharaData.put( - LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString)); + LIST_NAME, GattConstants.lookup(uuid, unknownCharaString)); currentCharaData.put(LIST_UUID, uuid); gattCharacteristicGroupData.add(currentCharaData); } diff --git a/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceScanActivity.java b/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceScanActivity.java index 6af05ea..23568f5 100644 --- a/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceScanActivity.java +++ b/Application/src/main/java/fr/centralesupelec/students/clientble/DeviceScanActivity.java @@ -26,7 +26,6 @@ import android.bluetooth.BluetoothManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.provider.Settings; @@ -210,10 +209,11 @@ public class DeviceScanActivity extends ListActivity { mBluetoothAdapter.stopLeScan(mLeScanCallback); mScanning = false; } + startActivity(intent); } private void scanLeDevice(final boolean enable) { - UUID [] uuids = {SampleGattAttributes.PRIVATE_SERVICE_UUID}; + UUID [] uuids = {GattConstants.PRIVATE_SERVICE_UUID}; if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { diff --git a/Application/src/main/java/fr/centralesupelec/students/clientble/SampleGattAttributes.java b/Application/src/main/java/fr/centralesupelec/students/clientble/GattConstants.java similarity index 88% rename from Application/src/main/java/fr/centralesupelec/students/clientble/SampleGattAttributes.java rename to Application/src/main/java/fr/centralesupelec/students/clientble/GattConstants.java index 9b3b83b..fd4eabc 100644 --- a/Application/src/main/java/fr/centralesupelec/students/clientble/SampleGattAttributes.java +++ b/Application/src/main/java/fr/centralesupelec/students/clientble/GattConstants.java @@ -23,14 +23,17 @@ import java.util.UUID; /** * This class includes a small subset of standard GATT attributes for demonstration purposes. */ -public class SampleGattAttributes { +public class GattConstants { private static HashMap attributes = new HashMap(); + + // UUID de notre service privé + // Version 4 (random) UUID — https://www.uuidgenerator.net/ public static final String PRIVATE_SERVICE_UUID_STRING = - "11223344-5566-7788-9900-aabbccddeeff"; + "622df401-85ed-4666-a4fe-9efaa3ab47aa"; public static final UUID PRIVATE_SERVICE_UUID = UUID.fromString(PRIVATE_SERVICE_UUID_STRING); public static final String SENSOR_CHARACTERISTIC_UUID_STRING = - "01020304-0506-0708-0900-0a0b0c0d0e0f"; + "7817a8eb-f6cb-4be3-8143-52086719754d"; public static final UUID SENSOR_CHARACTERISTIC_UUID = UUID.fromString(SENSOR_CHARACTERISTIC_UUID_STRING); public static final String CHARACTERISTIC_CONFIG_UUID_STRING = @@ -38,6 +41,7 @@ public class SampleGattAttributes { public static final UUID CHARACTERISTIC_CONFIG_UUID = UUID.fromString(CHARACTERISTIC_CONFIG_UUID_STRING); + // UUID de services connus — utilisés dans DeviceControleActivity static { // Sample Services. attributes.put("0000180a-0000-1000-8000-00805f9b34fb", "Device Information"); @@ -55,7 +59,7 @@ public class SampleGattAttributes { attributes.put("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name"); attributes.put(SENSOR_CHARACTERISTIC_UUID_STRING, "Sensor value"); - attributes.put("ff020304-0506-0708-0900-0a0b0c0d0e0f", "3-byte rw notif. char."); + attributes.put("c093685d-005f-4d3c-8240-6d3020a2c608", "3-byte rw notif. char."); } public static String lookup(String uuid, String defaultName) { diff --git a/Application/src/main/java/fr/centralesupelec/students/clientble/SimpleDetailActivity.java b/Application/src/main/java/fr/centralesupelec/students/clientble/SimpleDetailActivity.java index 58c4528..b2b6803 100644 --- a/Application/src/main/java/fr/centralesupelec/students/clientble/SimpleDetailActivity.java +++ b/Application/src/main/java/fr/centralesupelec/students/clientble/SimpleDetailActivity.java @@ -1,9 +1,7 @@ package fr.centralesupelec.students.clientble; import android.app.Activity; -import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; import android.content.BroadcastReceiver; import android.content.ComponentName; @@ -14,12 +12,9 @@ import android.content.ServiceConnection; import android.os.IBinder; import android.os.Bundle; import android.util.Log; -import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; -import java.util.UUID; - public class SimpleDetailActivity extends Activity { private final static String TAG = SimpleDetailActivity.class.getSimpleName(); @@ -200,7 +195,7 @@ public class SimpleDetailActivity extends Activity { return; } mSensorValueCharac = - privateService.getCharacteristic(SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID); + privateService.getCharacteristic(GattConstants.SENSOR_CHARACTERISTIC_UUID); final int charaProp = mSensorValueCharac.getProperties(); mBluetoothLeService.readCharacteristic(mSensorValueCharac); if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {