Utilisation d’UUID aléatoire (v4)

This commit is contained in:
Louis-Guillaume DUBOIS 2017-06-06 17:21:21 +02:00
parent 14c67e3abb
commit 41b63780d7
5 changed files with 17 additions and 21 deletions

View file

@ -36,9 +36,6 @@ import android.util.Log;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.UUID;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
@ -132,7 +129,7 @@ public class BluetoothLeService extends Service {
Log.d(TAG, "broadcastUpdate(String, BluetoothGattChar.) appelé."); Log.d(TAG, "broadcastUpdate(String, BluetoothGattChar.) appelé.");
final byte[] data = characteristic.getValue(); final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) { 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 = final long value =
(data.length == 2) ? (data[0] << 8) & 0x0000ff00 | (data[1] << 0) & 0x000000ff (data.length == 2) ? (data[0] << 8) & 0x0000ff00 | (data[1] << 0) & 0x000000ff
: (data[0] << 0) & 0x000000ff; : (data[0] << 0) & 0x000000ff;
@ -295,8 +292,8 @@ public class BluetoothLeService extends Service {
return; return;
} }
Log.d(TAG, "setChar.Notification() appelé"); Log.d(TAG, "setChar.Notification() appelé");
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) { if (GattConstants.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(SampleGattAttributes.CHARACTERISTIC_CONFIG_UUID); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(GattConstants.CHARACTERISTIC_CONFIG_UUID);
descriptor.setValue( descriptor.setValue(
enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
@ -324,6 +321,6 @@ public class BluetoothLeService extends Service {
public BluetoothGattService getPrivateService() { public BluetoothGattService getPrivateService() {
if (mBluetoothGatt == null) return null; if (mBluetoothGatt == null) return null;
return mBluetoothGatt.getService(SampleGattAttributes.PRIVATE_SERVICE_UUID); return mBluetoothGatt.getService(GattConstants.PRIVATE_SERVICE_UUID);
} }
} }

View file

@ -260,7 +260,7 @@ public class DeviceControlActivity extends Activity {
HashMap<String, String> currentServiceData = new HashMap<String, String>(); HashMap<String, String> currentServiceData = new HashMap<String, String>();
uuid = gattService.getUuid().toString(); uuid = gattService.getUuid().toString();
currentServiceData.put( currentServiceData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString)); LIST_NAME, GattConstants.lookup(uuid, unknownServiceString));
currentServiceData.put(LIST_UUID, uuid); currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData); gattServiceData.add(currentServiceData);
@ -277,7 +277,7 @@ public class DeviceControlActivity extends Activity {
HashMap<String, String> currentCharaData = new HashMap<String, String>(); HashMap<String, String> currentCharaData = new HashMap<String, String>();
uuid = gattCharacteristic.getUuid().toString(); uuid = gattCharacteristic.getUuid().toString();
currentCharaData.put( currentCharaData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString)); LIST_NAME, GattConstants.lookup(uuid, unknownCharaString));
currentCharaData.put(LIST_UUID, uuid); currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData); gattCharacteristicGroupData.add(currentCharaData);
} }

View file

@ -26,7 +26,6 @@ import android.bluetooth.BluetoothManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
@ -210,10 +209,11 @@ public class DeviceScanActivity extends ListActivity {
mBluetoothAdapter.stopLeScan(mLeScanCallback); mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false; mScanning = false;
} }
startActivity(intent);
} }
private void scanLeDevice(final boolean enable) { private void scanLeDevice(final boolean enable) {
UUID [] uuids = {SampleGattAttributes.PRIVATE_SERVICE_UUID}; UUID [] uuids = {GattConstants.PRIVATE_SERVICE_UUID};
if (enable) { if (enable) {
// Stops scanning after a pre-defined scan period. // Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {

View file

@ -23,14 +23,17 @@ import java.util.UUID;
/** /**
* This class includes a small subset of standard GATT attributes for demonstration purposes. * This class includes a small subset of standard GATT attributes for demonstration purposes.
*/ */
public class SampleGattAttributes { public class GattConstants {
private static HashMap<String, String> attributes = new HashMap(); private static HashMap<String, String> attributes = new HashMap();
// UUID de notre service privé
// Version 4 (random) UUID https://www.uuidgenerator.net/
public static final String PRIVATE_SERVICE_UUID_STRING = 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 = public static final UUID PRIVATE_SERVICE_UUID =
UUID.fromString(PRIVATE_SERVICE_UUID_STRING); UUID.fromString(PRIVATE_SERVICE_UUID_STRING);
public static final String SENSOR_CHARACTERISTIC_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 = public static final UUID SENSOR_CHARACTERISTIC_UUID =
UUID.fromString(SENSOR_CHARACTERISTIC_UUID_STRING); UUID.fromString(SENSOR_CHARACTERISTIC_UUID_STRING);
public static final String CHARACTERISTIC_CONFIG_UUID_STRING = public static final String CHARACTERISTIC_CONFIG_UUID_STRING =
@ -38,6 +41,7 @@ public class SampleGattAttributes {
public static final UUID CHARACTERISTIC_CONFIG_UUID = public static final UUID CHARACTERISTIC_CONFIG_UUID =
UUID.fromString(CHARACTERISTIC_CONFIG_UUID_STRING); UUID.fromString(CHARACTERISTIC_CONFIG_UUID_STRING);
// UUID de services connus utilisés dans DeviceControleActivity
static { static {
// Sample Services. // Sample Services.
attributes.put("0000180a-0000-1000-8000-00805f9b34fb", "Device Information"); 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("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name");
attributes.put(SENSOR_CHARACTERISTIC_UUID_STRING, "Sensor value"); 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) { public static String lookup(String uuid, String defaultName) {

View file

@ -1,9 +1,7 @@
package fr.centralesupelec.students.clientble; package fr.centralesupelec.students.clientble;
import android.app.Activity; import android.app.Activity;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
@ -14,12 +12,9 @@ import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.TextView; import android.widget.TextView;
import java.util.UUID;
public class SimpleDetailActivity extends Activity { public class SimpleDetailActivity extends Activity {
private final static String TAG = SimpleDetailActivity.class.getSimpleName(); private final static String TAG = SimpleDetailActivity.class.getSimpleName();
@ -200,7 +195,7 @@ public class SimpleDetailActivity extends Activity {
return; return;
} }
mSensorValueCharac = mSensorValueCharac =
privateService.getCharacteristic(SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID); privateService.getCharacteristic(GattConstants.SENSOR_CHARACTERISTIC_UUID);
final int charaProp = mSensorValueCharac.getProperties(); final int charaProp = mSensorValueCharac.getProperties();
mBluetoothLeService.readCharacteristic(mSensorValueCharac); mBluetoothLeService.readCharacteristic(mSensorValueCharac);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {