Ça marche avec le potentiomètre
This commit is contained in:
parent
baa15ceda3
commit
b5d22d7b4d
4 changed files with 42 additions and 32 deletions
|
@ -144,23 +144,26 @@ public class BluetoothLeService extends Service {
|
||||||
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
|
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
|
||||||
} else {
|
} else {
|
||||||
*/
|
*/
|
||||||
// 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;
|
||||||
} else {
|
long max = 65535; // 2^16 - 1
|
||||||
final StringBuilder stringBuilder = new StringBuilder(data.length);
|
double percent = ((double) (100 * value)) / ((double) max);
|
||||||
//stringBuilder.append(String.format("%d", data));/
|
intent.putExtra(EXTRA_DATA, String.format("%.3f %%", percent));
|
||||||
//stringBuilder.append(" --- ");
|
} else {
|
||||||
for (byte byteChar : data)
|
final StringBuilder stringBuilder = new StringBuilder(data.length);
|
||||||
stringBuilder.append(String.format("%02X ", byteChar));
|
//stringBuilder.append(String.format("%d", data));/
|
||||||
Log.d(TAG, String.format(stringBuilder.toString()));
|
//stringBuilder.append(" --- ");
|
||||||
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
|
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
|
/* TODO
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
@ -218,11 +221,10 @@ 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)}
|
||||||
* callback.
|
* callback.
|
||||||
*/
|
*/
|
||||||
public boolean connect(final String address) {
|
public boolean connect(final String address) {
|
||||||
if (mBluetoothAdapter == null || address == null) {
|
if (mBluetoothAdapter == null || address == null) {
|
||||||
|
@ -301,7 +303,7 @@ public class BluetoothLeService extends Service {
|
||||||
* Enables or disables notification on a give characteristic.
|
* Enables or disables notification on a give characteristic.
|
||||||
*
|
*
|
||||||
* @param characteristic Characteristic to act on.
|
* @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,
|
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
|
||||||
boolean enabled) {
|
boolean enabled) {
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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());
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue