Ç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

@ -144,23 +144,26 @@ public class BluetoothLeService extends Service {
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
} else {
*/
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())
&& data.length >= 1) {
int value = (data[0]<<8)&0x0000ff00 | (data[1]<<0)&0x000000ff;
intent.putExtra(EXTRA_DATA, String.format("décimal: %d", value));
} else {
final StringBuilder stringBuilder = new StringBuilder(data.length);
//stringBuilder.append(String.format("%d", data));/
//stringBuilder.append(" --- ");
for (byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
Log.d(TAG, String.format(stringBuilder.toString()));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
}
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
long value =
(data.length == 2) ? (data[0] << 8) & 0x0000ff00 | (data[1] << 0) & 0x000000ff
: (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 {
final StringBuilder stringBuilder = new StringBuilder(data.length);
//stringBuilder.append(String.format("%d", data));/
//stringBuilder.append(" --- ");
for (byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
Log.d(TAG, String.format(stringBuilder.toString()));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
}
}
/* TODO
}
*/
@ -218,11 +221,10 @@ public class BluetoothLeService extends Service {
* Connects to the GATT server hosted on the Bluetooth LE device.
*
* @param address The device address of the destination device.
*
* @return Return true if the connection is initiated successfully. The connection result
* is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
* is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
*/
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
@ -301,7 +303,7 @@ public class BluetoothLeService extends Service {
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
@ -310,17 +312,15 @@ public class BluetoothLeService extends Service {
return;
}
Log.d(TAG, "setChar.Notification() appelé");
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
/* TODO
// This is specific to Heart Rate Measurement.
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
if (SampleGattAttributes.SENSOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(SampleGattAttributes.CHARACTERISTIC_CONFIG_UUID);
descriptor.setValue(
enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
);
while (!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) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
if (true) {
if (false) {
final Intent intent = new Intent(this, DeviceControlActivity.class);
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());

View file

@ -33,6 +33,10 @@ public class SampleGattAttributes {
"01020304-0506-0708-0900-0a0b0c0d0e0f";
public static final UUID SENSOR_CHARACTERISTIC_UUID =
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 {
// Sample Services.

View file

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