Ça marche avec le potentiomètre

This commit is contained in:
Louis-Guillaume DUBOIS 2017-06-02 17:58:03 +02:00
parent baa15ceda3
commit b5d22d7b4d
No known key found for this signature in database
GPG key ID: 96472D986598B31E
4 changed files with 42 additions and 32 deletions

View file

@ -147,10 +147,13 @@ public class BluetoothLeService extends Service {
// For all other profiles, writes the data formatted in HEX. // For all other profiles, writes the data formatted in HEX.
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 (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
&& data.length >= 1) { long value =
int value = (data[0]<<8)&0x0000ff00 | (data[1]<<0)&0x000000ff; (data.length == 2) ? (data[0] << 8) & 0x0000ff00 | (data[1] << 0) & 0x000000ff
intent.putExtra(EXTRA_DATA, String.format("décimal: %d", value)); : (data[0] << 0) & 0x000000ff;
long max = 65535; // 2^16 - 1
double percent = ((double) (100 * value)) / ((double) max);
intent.putExtra(EXTRA_DATA, String.format("%.3f %%", percent));
} else { } else {
final StringBuilder stringBuilder = new StringBuilder(data.length); final StringBuilder stringBuilder = new StringBuilder(data.length);
//stringBuilder.append(String.format("%d", data));/ //stringBuilder.append(String.format("%d", data));/
@ -218,7 +221,6 @@ public class BluetoothLeService extends Service {
* Connects to the GATT server hosted on the Bluetooth LE device. * Connects to the GATT server hosted on the Bluetooth LE device.
* *
* @param address The device address of the destination device. * @param address The device address of the destination device.
*
* @return Return true if the connection is initiated successfully. The connection result * @return Return true if the connection is initiated successfully. The connection result
* is reported asynchronously through the * is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
@ -310,17 +312,15 @@ public class BluetoothLeService extends Service {
return; return;
} }
Log.d(TAG, "setChar.Notification() appelé"); Log.d(TAG, "setChar.Notification() appelé");
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(SampleGattAttributes.CHARACTERISTIC_CONFIG_UUID);
/* TODO descriptor.setValue(
// This is specific to Heart Rate Measurement. enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
BluetoothGattDescriptor descriptor = characteristic.getDescriptor( );
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); while (!mBluetoothGatt.writeDescriptor(descriptor)) ;
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
} }
*/ mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
} }
/** /**

View file

@ -180,7 +180,7 @@ public class DeviceScanActivity extends ListActivity {
protected void onListItemClick(ListView l, View v, int position, long id) { protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return; if (device == null) return;
if (true) { if (false) {
final Intent intent = new Intent(this, DeviceControlActivity.class); final Intent intent = new Intent(this, DeviceControlActivity.class);
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName()); intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress()); intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());

View file

@ -33,6 +33,10 @@ public class SampleGattAttributes {
"01020304-0506-0708-0900-0a0b0c0d0e0f"; "01020304-0506-0708-0900-0a0b0c0d0e0f";
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 =
"00002902-0000-1000-8000-00805f9b34fb";
public static final UUID CHARACTERISTIC_CONFIG_UUID =
UUID.fromString(CHARACTERISTIC_CONFIG_UUID_STRING);
static { static {
// Sample Services. // Sample Services.

View file

@ -1,7 +1,9 @@
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;
@ -16,6 +18,8 @@ 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();
@ -122,12 +126,14 @@ public class SimpleDetailActivity extends Activity {
if (mBluetoothLeService != null) { if (mBluetoothLeService != null) {
final boolean result = mBluetoothLeService.connect(mDeviceAddress); final boolean result = mBluetoothLeService.connect(mDeviceAddress);
Log.d(TAG, "Connect request result=" + result); Log.d(TAG, "Connect request result=" + result);
mBluetoothLeService.setCharacteristicNotification(mSensorValueCharac, true);
} }
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
mBluetoothLeService.setCharacteristicNotification(mSensorValueCharac, false);
unregisterReceiver(mGattUpdateReceiver); unregisterReceiver(mGattUpdateReceiver);
} }