Fin de la première passe de documentation.
This commit is contained in:
parent
3e78c1e9b0
commit
7828a157a6
2 changed files with 124 additions and 42 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2013 The Android Open Source Project
|
* Copyright (C) 2013 The Android Open Source Project
|
||||||
* Copyright (C) 2017 Louis-Guillaume Dubois
|
* Copyright (C) 2017 CentraleSupélec
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2017 CentraleSupélec
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package fr.centralesupelec.students.clientble;
|
package fr.centralesupelec.students.clientble;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
@ -11,18 +26,24 @@ import android.content.IntentFilter;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.SyncStateContract;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Activité qui se connecte à un appareil BLE proposant notre service privé et
|
||||||
|
* affiche les caractéristiques de ce dernier.
|
||||||
|
* Permet la modification de la caractéristique longue éditable, et écoute
|
||||||
|
* les notifications de la valeur du potentiomètre.
|
||||||
|
*/
|
||||||
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();
|
||||||
|
|
||||||
public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
|
public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
|
||||||
public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
|
public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
|
||||||
|
|
||||||
|
/* Référence à des objets de l’interface. */
|
||||||
private TextView mDeviceAddressView;
|
private TextView mDeviceAddressView;
|
||||||
private TextView mConnectionStateView;
|
private TextView mConnectionStateView;
|
||||||
private TextView mSensorValueView;
|
private TextView mSensorValueView;
|
||||||
|
@ -32,7 +53,6 @@ public class SimpleDetailActivity extends Activity {
|
||||||
private String mDeviceName;
|
private String mDeviceName;
|
||||||
private String mDeviceAddress;
|
private String mDeviceAddress;
|
||||||
|
|
||||||
|
|
||||||
private BluetoothLeService mBluetoothLeService;
|
private BluetoothLeService mBluetoothLeService;
|
||||||
private boolean mConnected = false;
|
private boolean mConnected = false;
|
||||||
private BluetoothGattCharacteristic mSensorValueCharac;
|
private BluetoothGattCharacteristic mSensorValueCharac;
|
||||||
|
@ -63,7 +83,9 @@ public class SimpleDetailActivity extends Activity {
|
||||||
// ACTION_GATT_CONNECTED: connected to a GATT server.
|
// ACTION_GATT_CONNECTED: connected to a GATT server.
|
||||||
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
|
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
|
||||||
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
|
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
|
||||||
// ACTION_SENSOR_VALUE_AVAILABLE: received data from the device. This can be a result of read
|
// ACTION_SENSOR_VALUE_AVAILABLE: received data (sensor value) from the device. This can be a result of read
|
||||||
|
// or notification operations.
|
||||||
|
// ACTION_WRITABLE_VALUE_AVAILABLE: received data (long characteristic value) from the device. This can be a result of read
|
||||||
// or notification operations.
|
// or notification operations.
|
||||||
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -81,37 +103,47 @@ public class SimpleDetailActivity extends Activity {
|
||||||
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
|
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
|
||||||
// Show all the supported services and characteristics on the user interface.
|
// Show all the supported services and characteristics on the user interface.
|
||||||
Log.d(TAG, "ACTION_GATT_SERVICES_DISCOVERED reçu.");
|
Log.d(TAG, "ACTION_GATT_SERVICES_DISCOVERED reçu.");
|
||||||
requestValues();
|
requestValues(); // Demande des valeurs des caractéristiques.
|
||||||
} else if (BluetoothLeService.ACTION_SENSOR_VALUE_AVAILABLE.equals(action)) {
|
} else if (BluetoothLeService.ACTION_SENSOR_VALUE_AVAILABLE.equals(action)) {
|
||||||
Log.d(TAG, "ACTION_SENSOR_VALUE_AVAILABLE reçu.");
|
Log.d(TAG, "ACTION_SENSOR_VALUE_AVAILABLE reçu.");
|
||||||
final String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
final String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
||||||
Log.d(TAG, data);
|
Log.d(TAG, data);
|
||||||
displaySensorValue(data);
|
displaySensorValue(data); // Affichage de la nouvelle valeur.
|
||||||
} else if (BluetoothLeService.ACTION_WRITABLE_VALUE_AVAILABLE.equals(action)) {
|
} else if (BluetoothLeService.ACTION_WRITABLE_VALUE_AVAILABLE.equals(action)) {
|
||||||
Log.d(TAG, "ACTION_WRITABLE_VALUE_AVAILABLE reçu.");
|
Log.d(TAG, "ACTION_WRITABLE_VALUE_AVAILABLE reçu.");
|
||||||
final String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
final String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
||||||
Log.d(TAG, data);
|
Log.d(TAG, data);
|
||||||
displayWritableValue(data);
|
displayWritableValue(data); // Affichage de la nouvelle valeur.
|
||||||
} else {
|
} else {
|
||||||
Log.w(TAG, "Action non reconnue.");
|
Log.w(TAG, "Action non reconnue.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nettoyage de l’interface.
|
||||||
|
*/
|
||||||
private void clearUI() {
|
private void clearUI() {
|
||||||
mSensorValueView.setText(R.string.no_data);
|
mSensorValueView.setText(R.string.no_data);
|
||||||
mWritableValueView.setText(R.string.no_data);
|
mWritableValueView.setText(R.string.no_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée lors de la création de l’activité ou de la modification de l’orientation
|
||||||
|
* de l’écran (paysage/portrait.)
|
||||||
|
* @param savedInstanceState
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.simple_detail_layout);
|
setContentView(R.layout.simple_detail_layout);
|
||||||
|
|
||||||
|
// Réception des données de l’Intent qui a lancée l’activité.
|
||||||
final Intent intent = getIntent();
|
final Intent intent = getIntent();
|
||||||
mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
|
mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
|
||||||
mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
|
mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
|
||||||
|
|
||||||
|
// Configuration des références vers les objets de l’interface
|
||||||
mDeviceAddressView = (TextView) findViewById(R.id.device_address);
|
mDeviceAddressView = (TextView) findViewById(R.id.device_address);
|
||||||
mDeviceAddressView.setText(mDeviceAddress);
|
mDeviceAddressView.setText(mDeviceAddress);
|
||||||
|
|
||||||
|
@ -120,16 +152,23 @@ public class SimpleDetailActivity extends Activity {
|
||||||
mSensorValueView = (TextView) findViewById(R.id.sensor_value);
|
mSensorValueView = (TextView) findViewById(R.id.sensor_value);
|
||||||
mWritableValueView = (TextView) findViewById(R.id.writable_value);
|
mWritableValueView = (TextView) findViewById(R.id.writable_value);
|
||||||
|
|
||||||
|
// Configuration de la barre supérieure
|
||||||
getActionBar().setTitle(mDeviceName);
|
getActionBar().setTitle(mDeviceName);
|
||||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
|
// Liaison au service BluetoothLeService
|
||||||
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
|
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
|
||||||
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
|
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée après onCreate, ou quand l’application repasse au premier plan.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
// Recevoir à nouveau les notifications de la valeur du potentiomètre.
|
||||||
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
|
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
|
||||||
if (mBluetoothLeService != null) {
|
if (mBluetoothLeService != null) {
|
||||||
final boolean result = mBluetoothLeService.connect(mDeviceAddress);
|
final boolean result = mBluetoothLeService.connect(mDeviceAddress);
|
||||||
|
@ -138,47 +177,41 @@ public class SimpleDetailActivity extends Activity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée avant onDestroy, ou quand l’application passe à l’arrière-plan.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
// Cesser de recevoir les notifications de la valeur du potentiomètre.
|
||||||
mBluetoothLeService.setCharacteristicNotification(mSensorValueCharac, false);
|
mBluetoothLeService.setCharacteristicNotification(mSensorValueCharac, false);
|
||||||
unregisterReceiver(mGattUpdateReceiver);
|
unregisterReceiver(mGattUpdateReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quand l’activité se termine.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
// Fin de la liaison avec le service
|
||||||
unbindService(mServiceConnection);
|
unbindService(mServiceConnection);
|
||||||
mBluetoothLeService = null;
|
mBluetoothLeService = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
@Override
|
* Gestion de l’appui sur un bouton de la barre supérieure.
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
* @param item
|
||||||
getMenuInflater().inflate(R.menu.gatt_services, menu);
|
* @return
|
||||||
if (mConnected) {
|
*/
|
||||||
menu.findItem(R.id.menu_connect).setVisible(false);
|
|
||||||
menu.findItem(R.id.menu_disconnect).setVisible(true);
|
|
||||||
} else {
|
|
||||||
menu.findItem(R.id.menu_connect).setVisible(true);
|
|
||||||
menu.findItem(R.id.menu_disconnect).setVisible(false);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
switch(item.getItemId()) {
|
switch(item.getItemId()) {
|
||||||
/*
|
// Bouton à gauche
|
||||||
case R.id.menu_connect:
|
|
||||||
mBluetoothLeService.connect(mDeviceAddress);
|
|
||||||
return true;
|
|
||||||
case R.id.menu_disconnect:
|
|
||||||
mBluetoothLeService.disconnect();
|
|
||||||
return true;
|
|
||||||
*/
|
|
||||||
case android.R.id.home:
|
case android.R.id.home:
|
||||||
|
// Identique à un appel au bouton "retour" en forme de triangle en bas à gauche
|
||||||
|
// de la barre inférieure d’Android.
|
||||||
onBackPressed();
|
onBackPressed();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -186,6 +219,10 @@ public class SimpleDetailActivity extends Activity {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Affichage de l’était de la connexion.
|
||||||
|
* @param resourceId
|
||||||
|
*/
|
||||||
private void updateConnectionState(final int resourceId) {
|
private void updateConnectionState(final int resourceId) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -195,26 +232,55 @@ public class SimpleDetailActivity extends Activity {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displaySensorValue(String data) {
|
/**
|
||||||
if (data != null) {
|
* Affichage de la valeur du potentiomètre.
|
||||||
mSensorValueView.setText(data);
|
* @param data
|
||||||
}
|
*/
|
||||||
|
private void displaySensorValue(final String data) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (data != null) {
|
||||||
|
mSensorValueView.setText(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayWritableValue(String data) {
|
/**
|
||||||
if (data != null) {
|
* Affichage de la valeur de la caractéristique longue éditable.
|
||||||
mWritableValueView.setText(data);
|
* @param data
|
||||||
}
|
*/
|
||||||
|
private void displayWritableValue(final String data) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (data != null) {
|
||||||
|
mWritableValueView.setText(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lors d’un appui sur le bouton "Read" pour lire de nouveau la valeur de la caractéristique longue.
|
||||||
|
* @param view
|
||||||
|
*/
|
||||||
public void onRefreshClick(View view) {
|
public void onRefreshClick(View view) {
|
||||||
Log.d(TAG, "onRefreshClick()");
|
Log.d(TAG, "onRefreshClick()");
|
||||||
requestWritableValue();
|
requestWritableValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lors d’un appui sur le bouton "Write" pour envoyer à l’appareil BLE dans la caractéristique
|
||||||
|
* longue éditable la valeur (ASCII) du champ textuel au-dessus.
|
||||||
|
* @param view
|
||||||
|
*/
|
||||||
public void onSendClick(View view) {
|
public void onSendClick(View view) {
|
||||||
Log.d(TAG, "onSendClick()");
|
Log.d(TAG, "onSendClick()");
|
||||||
|
// Récupération du contenu du champ textuel éditable par l’utilisateur.
|
||||||
byte [] text = mFormView.getText().toString().getBytes();
|
byte [] text = mFormView.getText().toString().getBytes();
|
||||||
|
// Longueur maximale de la valeur de la caractéristique: 20 octets.
|
||||||
byte [] data = new byte[20];
|
byte [] data = new byte[20];
|
||||||
int max = Math.min(text.length, GattConstants.WRITABLE_CHARACTERISTIC_MAX_LENGTH);
|
int max = Math.min(text.length, GattConstants.WRITABLE_CHARACTERISTIC_MAX_LENGTH);
|
||||||
for (int i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
|
@ -224,6 +290,9 @@ public class SimpleDetailActivity extends Activity {
|
||||||
mBluetoothLeService.writeCharacterisitic(mWritableValueCharac, data);
|
mBluetoothLeService.writeCharacterisitic(mWritableValueCharac, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Demande de la valeur de la caractéristique longue éditable.
|
||||||
|
*/
|
||||||
private void requestWritableValue() {
|
private void requestWritableValue() {
|
||||||
BluetoothGattService privateService = mBluetoothLeService.getPrivateService();
|
BluetoothGattService privateService = mBluetoothLeService.getPrivateService();
|
||||||
if (privateService == null) {
|
if (privateService == null) {
|
||||||
|
@ -239,7 +308,10 @@ public class SimpleDetailActivity extends Activity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestAndSubsribeSensorValue() {
|
/**
|
||||||
|
* Demande la valeur du potentiomètre, et souscrit à ses notifications.
|
||||||
|
*/
|
||||||
|
private void requestAndSubscribeSensorValue() {
|
||||||
BluetoothGattService privateService = mBluetoothLeService.getPrivateService();
|
BluetoothGattService privateService = mBluetoothLeService.getPrivateService();
|
||||||
if (privateService == null) {
|
if (privateService == null) {
|
||||||
Log.w(TAG, "Service Gatt privé non détecté.");
|
Log.w(TAG, "Service Gatt privé non détecté.");
|
||||||
|
@ -248,9 +320,11 @@ public class SimpleDetailActivity extends Activity {
|
||||||
mSensorValueCharac =
|
mSensorValueCharac =
|
||||||
privateService.getCharacteristic(GattConstants.SENSOR_CHARACTERISTIC_UUID);
|
privateService.getCharacteristic(GattConstants.SENSOR_CHARACTERISTIC_UUID);
|
||||||
if (mSensorValueCharac != null) {
|
if (mSensorValueCharac != null) {
|
||||||
|
// Lecture.
|
||||||
mBluetoothLeService.readCharacteristic(mSensorValueCharac);
|
mBluetoothLeService.readCharacteristic(mSensorValueCharac);
|
||||||
|
|
||||||
|
// Souscription aux notifications.
|
||||||
final int charaProp = mSensorValueCharac.getProperties();
|
final int charaProp = mSensorValueCharac.getProperties();
|
||||||
mBluetoothLeService.readCharacteristic(mSensorValueCharac);
|
|
||||||
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
|
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
|
||||||
Log.d(TAG, "Demande de notification.");
|
Log.d(TAG, "Demande de notification.");
|
||||||
mBluetoothLeService.setCharacteristicNotification(mSensorValueCharac, true);
|
mBluetoothLeService.setCharacteristicNotification(mSensorValueCharac, true);
|
||||||
|
@ -258,14 +332,22 @@ public class SimpleDetailActivity extends Activity {
|
||||||
} else {
|
} else {
|
||||||
Log.w(TAG, "SENSOR_CHARACTERISTIC_UUID non trouvé");
|
Log.w(TAG, "SENSOR_CHARACTERISTIC_UUID non trouvé");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Demande les valeurs de deux caractéristiques du service,
|
||||||
|
* et souscrit aux notifications de la valeur du potentiomètre.
|
||||||
|
*/
|
||||||
private void requestValues() {
|
private void requestValues() {
|
||||||
requestWritableValue();
|
requestWritableValue();
|
||||||
requestAndSubsribeSensorValue();
|
requestAndSubscribeSensorValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filtre les types d’Intents broadcastés à recevoir, suivant les noms des actions.
|
||||||
|
* Ne recevoir que les actions envoyées par BluetoothLeService.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private static IntentFilter makeGattUpdateIntentFilter() {
|
private static IntentFilter makeGattUpdateIntentFilter() {
|
||||||
final IntentFilter intentFilter = new IntentFilter();
|
final IntentFilter intentFilter = new IntentFilter();
|
||||||
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
|
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
|
||||||
|
|
Loading…
Reference in a new issue