Hugo Domenech-Juarez discusses about the possibilities that Android provides by using BLE to connect to sensors and actuators around you. He shares some tips and tricks learned from building a SDK for interacting with the WunderBar, a BLE enabled starter kit for the Internet of Things, and his experiences on how to make your connections secure and fast.
25. BLE Scanning
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi,
byte[] scanRecord) {
// You are not in the UI thread
// or in the thread you subscribed for what matters!
}
};
25
26. BLE Scanning
/**
* Decodes the services uuid from the advertisement packet since
* {@link android.bluetooth.BluetoothDevice#getUuids()} returns null most of
* the times.
*/
public static List<String> decodeServicesUuid(byte[] data);
/**
* Decodes the device name from the Complete Local Name or Shortened
* Local Name field in the
* Advertisement packet. {@link
* android.bluetooth.BluetoothDevice#getName()} does it already,
* although some phones skip it. i.e. Sony Xperia Z1 (C6903) with Android 4.3
* where getName() always returns <code>null</code>.
*/
public static String decodeDeviceName(byte[] data);
26
27. BLE Scanning: Lollipop improvements
mBluetoothAdapter.startScan(filters, scanSettings, scanCallback)
name
deviceAddress
uuid
serviceDataUuid
serviceData
manufacturerId
manufacturerData
Low Power
Balanced
Low Latency
27
28. interface ScanCallback {
public void onScanResult(int callbackType, ScanResult result);
public void onBatchScanResults(List<ScanResult> results);
public void onScanFailed(int errorCode)
}
BLE Scanning: Lollipop improvements
28
29. Advertisement
GATT server
Hardware feature
- chipset needs to support it
- nexus 9 vs nexus 5 & 7
BluetoothAdapter.isMultipleAdvertisementSupported()
Peripheral mode: Lollipop features
29
31. The GATT Protocol
Peripheral Central
Read - Write - Subscribe
Respond - Push
GATT Peripheral
GATT protocol
Service
Service
Characteristic
Characteristic
Value
Indications
Notifications
Descriptor
Descriptor Descriptor
31
Server Client
Generic Attribute Profile
32. The GATT Protocol
class BluetoothAdapterLeScanCallback {
void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
device.connectGatt(context, false, mBluetoothGattReceiver);
}
}
class BluetoothGattReceiver {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {}
}
32