Android Alliance Philadelphia - November 11, 2015
Don Coleman - Chariot Solutions
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
BluetoothManager manager =
activity.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter adapter = manager.getAdapter();
bluetoothAdapter.startLeScan(callback);
bluetoothAdapter.startLeScan(serviceUuids, callback);
public void onLeScan(BluetoothDevice device,
int rssi,
byte[] scanRecord) {
// do something
}
ScanCallback scanCallback = new ScanCallback() {
public void onScanResult(int callbackType, ScanResult result) {
}
public void onScanFailed(int errorCode) {
}
};
BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
List<ScanFilter> scanFilters = new ArrayList<>();
scanFilters.add(
new ScanFilter.Builder().setServiceUuid(
new ParcelUuid(BLEThermometer.SERVICE_UUID)).build()
);
ScanSettings scanSettings = new ScanSettings.Builder().build();
scanner.startScan(scanFilters, scanSettings, scanCallback);
device.connectGatt(context, autoConnect, bluetoothGattCallback);
public void onConnectionStateChange(BluetoothGatt gatt,
int status, int state) {
if (state == BluetoothGatt.STATE_CONNECTED) {
this.gatt = gatt;
gatt.discoverServices();
}
}
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
BluetoothGattService service = gatt.getService(SERVICE_UUID);
switchCharacteristic = service.getCharacteristic(SWITCH_UUID);
brightnessCharacteristic = service.getCharacteristic(DIMMER_UUID);
}
gatt.readCharacteristic(switchCharacteristic);
public void onCharacteristicRead(gatt, characteristic, status) {
UUID uuid = characteristic.getUuid();
if (uuid.equals(SWITCH_UUID)) {
int switchState =
characteristic.getIntValue(FORMAT_UINT8, 0);
// update UI
}
}
byte data[] = { 0x01 };
characteristic.setValue(data);
gatt.writeCharacteristic(characteristic);
switchCharacteristic.setValue(1, FORMAT_UINT8, 0);
gatt.writeCharacteristic(switchCharacteristic);
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "Wrote " + characteristic);
}
if (gatt.setCharacteristicNotification(characteristic, true)) {
// 0x2902 client characteristic configuration
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805F9B34FB");
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION);
gatt.writeDescriptor(descriptor);
}
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
if (characteristic.getUuid().equals(TEMPERATURE_UUID)) {
float temperature = getFloat(characteristic, 0);
Log.d(TAG, String.valueOf(temperature));
float fahrenheit = 1.8f * temperature + 32;
Log.d(TAG, String.valueOf(fahrenheit));
// update the UI
}
}
Requires Lollipop (21) & compatible hardware
BluetoothGattService service =
new BluetoothGattService(SERVICE_UUID, SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic switchCharacteristic =
new BluetoothGattCharacteristic(
SWITCH_UUID,
PROPERTY_READ | PROPERTY_WRITE,
PERMISSION_READ | PERMISSION_WRITE);
BluetoothGattCharacteristic dimmerCharacteristic =
new BluetoothGattCharacteristic(
DIMMER_UUID,
PROPERTY_READ | PROPERTY_WRITE,
PERMISSION_READ | PERMISSION_WRITE);
service.addCharacteristic(switchCharacteristic);
service.addCharacteristic(dimmerCharacteristic);
BluetoothGattServerCallback callback = new BluetoothGattServerCallback(){
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
}
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
}
};
if (SWITCH_UUID.equals(characteristic.getUuid())) {
gattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
0,
getSwitchValue());
}
if (SWITCH_UUID.equals(characteristic.getUuid())) {
setSwitchValue(value);
} else if (DIMMER_UUID.equals(characteristic.getUuid())) {
setDimmerValue(value);
}
if (responseNeeded) {
gattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
0,
value);
}
gattServer = bluetoothManager.openGattServer(
context, bluetoothGattServerCallback);
gattServer.addService(service);
private AdvertiseSettings getAdvertiseSettings() {
AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
return builder.build();
}
private AdvertiseData getAdvertisementData() {
AdvertiseData.Builder builder = new AdvertiseData.Builder();
builder.setIncludeDeviceName(true);
builder.addServiceUuid(new ParcelUuid(SERVICE_UUID));
return builder.build();
}
BluetoothLeAdvertiser bluetoothLeAdvertiser =
bluetoothAdapter.getBluetoothLeAdvertiser();
AdvertiseData advertisementData = getAdvertisementData();
AdvertiseSettings advertiseSettings = getAdvertiseSettings();
bluetoothLeAdvertiser.startAdvertising(advertiseSettings,
advertisementData,
advertiseCallback);
if (!bluetoothAdapter.isEnabled()) {
Intent intent =
new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
this.startActivityForResult(intent, ENABLE_BLUETOOTH_REQUEST);
// process the result in onActivityResult
}
Don Coleman
Slides http://don.github.io/slides/
Bluetooth Low Energy for Android by Don Coleman
is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at https://github.com/don/.../2015-11-11-phillyandroid.