Slide 1

Slide 1 text

EVERYTHING ABOUT BLUETOOTH Johnny Sung 02. Peripheral mode in Android

Slide 2

Slide 2 text

https://fb.com/j796160836 Johnny Sung Mobile devices Developer https://plus.google.com/+JohnnySung http://about.me/j796160836

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

(Samsung S6 Edge) Peripheral Central (LG Nexus 5)

Slide 6

Slide 6 text

8)"5*4#-&

Slide 7

Slide 7 text

2.0 2.0 + 4.0 Dual Mode 4.0 Bluetooth SMART READY Bluetooth Bluetooth SMART

Slide 8

Slide 8 text

Bluetooth low energy Protocol Stack

Slide 9

Slide 9 text

• Peripheral ヰ鼹酤縨 • Central • Broadcaster • Observer GAP Roles

Slide 10

Slide 10 text

Peripheral Central

Slide 11

Slide 11 text

Peripheral Central

Slide 12

Slide 12 text

Peripheral Central

Slide 13

Slide 13 text

GATT

Slide 14

Slide 14 text

• Service • Characteristic • Data • Descriptor GATT

Slide 15

Slide 15 text

• Heart Rate Service • Heart Rate Measurement • Data • Descriptor 00002A37-0000-1000-8000-00805F9B34FB GATT Heart Rate Monitor 0000180D-0000-1000-8000-00805F9B34FB Property: Notify

Slide 16

Slide 16 text

Property: Indicate • Health Thermometer • Temperature Measurement • Data • Descriptor GATT Health Thermometer 00001809-0000-1000-8000-00805F9B34FB 00002A1C-0000-1000-8000-00805F9B34FB

Slide 17

Slide 17 text

• Read • Write • Notify • Indicate Characteristic Properties

Slide 18

Slide 18 text

UUID 00002A37-0000-1000-8000-00805F9B34FB 0x2A37 Heart Rate Measurement

Slide 19

Slide 19 text

• an indicate operation is identical to a notify operation except that indications are acknowledged, while notifications are not. Notify vs Indicate http://mbientlab.com/blog/bluetooth-low-energy-introduction/

Slide 20

Slide 20 text

https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx? u=org.bluetooth.service.health_thermometer.xml

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Property: Indicate • Health Thermometer • Temperature Measurement • Data • Descriptor GATT Health Thermometer 00001809-0000-1000-8000-00805F9B34FB 00002A1C-0000-1000-8000-00805F9B34FB

Slide 26

Slide 26 text

*.1-&.&/5*/ "/%30*%

Slide 27

Slide 27 text

• Central • Android 4.3 (API Level 18) • Peripheral • Android 5.0 (API Level 21) • Specific BLE chip Requirement in Android

Slide 28

Slide 28 text

What? Specific BLE chip ?

Slide 29

Slide 29 text

http://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html

Slide 30

Slide 30 text

BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 BluetoothAdapter adapter = manager.getAdapter();
 
 adapter.isMultipleAdvertisementSupported(); Check if devices support Peripheral mode

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

PERIPHERAL

Slide 33

Slide 33 text


 
 
 BluetoothQFSNJTTJPOT "OESPJE.BOJGFTUYNM

Slide 34

Slide 34 text

BluetoothQFSNJTTJPOT $IFDLTZTUFNGFBUVSF getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) @Override
 public void onActivityResult(int requestCode , int resultCode, Intent data) {
 if (requestCode == REQUEST_ENABLE_BT) {
 if (resultCode == Activity.RESULT_OK) {
 // Bluetooth has turned on
 } else {
 // User did not enable Bluetooth or an error occurred
 }
 }
 } Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 3FRVFTUUPFOBCMF#MVFUPPUI

Slide 35

Slide 35 text

BluetoothGattService hrmService = new BluetoothGattService(SERVICE_HEALTH_THERMOMETER_UUID,
 BluetoothGattService.SERVICE_TYPE_PRIMARY); BluetoothGattCharacteristic tempChar =
 new BluetoothGattCharacteristic(CHAR_TEMP_UUID, BluetoothGattCharacteristic.PROPERTY_INDICATE, BluetoothGattCharacteristic.PERMISSION_READ); tempChar.addDescriptor(new BluetoothGattDescriptor(
 UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),
 (BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE))); 
 hrmService.addCharacteristic(tempChar); 1SFQBSFTFSWJDFTUSVDUVSF Health Thermometer 2A1C 1809

Slide 36

Slide 36 text

AdvertiseData.Builder datas = new AdvertiseData.Builder();
 AdvertiseSettings.Builder settings = new AdvertiseSettings.Builder();
 
 datas.addServiceUuid(new ParcelUuid(hrmService.getUuid()));
 BluetoothLeAdvertiser advertiser = adapter.getBluetoothLeAdvertiser();
 advertiser.startAdvertising(settings.build(), datas.build(), advertiseCallback); BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 BluetoothAdapter adapter = bluetoothManager.getAdapter(); BluetoothGattServer gattServer = manager.openGattServer(context, gattServerCallback); gattServer.addService(hrmService); 0QFOTFSWFS "EWFSUJTFUPPUIFST

Slide 37

Slide 37 text

AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
 @Override
 public void onStartSuccess(AdvertiseSettings settingsInEffect) {
 // ...
 }
 
 @Override
 public void onStartFailure(int errorCode) {
 // ...
 }
 }; "EWFSUJTFDBMMCBDL

Slide 38

Slide 38 text

private HashSet bleDevices = new HashSet<>(); 
 private final BluetoothGattServerCallback gattServerCallback = new BluetoothGattServerCallback() {
 @Override
 public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
 super.onConnectionStateChange(device, status, newState);
 if (status == BluetoothGatt.GATT_SUCCESS) {
 if (newState == BluetoothGatt.STATE_CONNECTED) {
 // Connect
 bleDevices.add(device);
 } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
 // Disconnect
 bleDevices.remove(device);
 }
 } else {
 // Disconnect with error
 bleDevices.remove(device);
 }
 }
 // ... (略)
 }; )BOEMFEFWJDFDPOOFDU

Slide 39

Slide 39 text

private final BluetoothGattServerCallback gattServerCallback = new BluetoothGattServerCallback() { 
 // ... (略)
 
 @Override
 public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
 // ... gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,
 offset, characteristic.getValue());
 }
 
 // ... (略)
 }; 3FBEJOH$IBSBDUFSJTUJD

Slide 40

Slide 40 text

8SJUJOH$IBSBDUFSJTUJD http://developer.android.com/reference/android/bluetooth/ BluetoothGattServerCallback.html#onCharacteristicWriteRequest(android.bluetooth.BluetoothDevice, int, android.bluetooth.BluetoothGattCharacteristic, boolean, boolean, int, byte[]) private final BluetoothGattServerCallback gattServerCallback = new BluetoothGattServerCallback() {
 // ... (略)
 @Override
 public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded,
 int offset, byte[] value) {
 // ...
 ByteBuffer buffer = ByteBuffer.wrap(value);
 buffer.order(ByteOrder.LITTLE_ENDIAN); 
 characteristic.setValue(buffer.getInt(),
 BluetoothGattCharacteristic.FORMAT_UINT16, 0);
 
 if (responseNeeded) {
 gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);
 }
 } // ... (略)
 };

Slide 41

Slide 41 text

private final BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
 // ... (略)
 @Override
 public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded,
 int offset, byte[] value) { 
 super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
 if (responseNeeded) {
 gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);
 }
 } // ... (略)
 }; 8SJUJOH%FTDSJQUPS

Slide 42

Slide 42 text

public void sendNotificationToDevices( BluetoothGattCharacteristic characteristic) {
 boolean indicate = (characteristic.getProperties()
 & BluetoothGattCharacteristic.PROPERTY_INDICATE)
 == BluetoothGattCharacteristic.PROPERTY_INDICATE;
 for (BluetoothDevice device : bleDevices) {
 gattServer.notifyCharacteristicChanged(device, characteristic, indicate);
 }
 } 4FOE$IBSBDUFSJTUJD/PUJGZ

Slide 43

Slide 43 text

Q&A

Slide 44

Slide 44 text

ADDITIONAL SLIDES

Slide 45

Slide 45 text

The story About Nexus 5

Slide 46

Slide 46 text

http://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

https://code.google.com/p/android-developer-preview/issues/detail?id=1570

Slide 49

Slide 49 text

We introduced BLE peripheral mode in Android 5.0 Lollipop. Nexus 6 and Nexus 9 are the first two production Nexus devices that support BLE peripheral mode. Due to hardware chipset dependency, older Nexus devices (4/5/7) will not have access to the feature on Lollipop. #52 Won’t Fix