everything about bluetooth (淺談藍牙 4.0) - central 篇

64
EVERYTHING ABOUT BLUETOOTH Johnny Sung

Upload: johnny-sung

Post on 23-Jan-2018

4.248 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

EVERYTHING ABOUT BLUETOOTHJohnny Sung

Page 2: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

https://fb.com/j796160836

Johnny SungMobile devices Developer

https://plus.google.com/+JohnnySung

http://about.me/j796160836

Page 3: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 4: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

MessageHorn

https://play.google.com/store/apps/details?id=com.JohnnyWorks.messagehorn

https://itunes.apple.com/tw/app/messagehorn/id994783068

iOS Server Android Client

iOS Server

Android Client

Page 5: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 6: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 7: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

http://www.redicecreations.com/article.php?id=31630http://www.ettoday.net/dalemon/post/3250

Page 8: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 9: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

2.0 2.0 + 4.0 Dual Mode 4.0Bluetooth SMART READYBluetooth Bluetooth SMART

Page 10: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 11: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Bluetooth low energy Protocol Stack

Page 12: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• Peripheral

• Central

• Broadcaster

• Observer

GAP Roles

Page 13: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

PeripheralCentral

Page 14: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

PeripheralCentral

Page 15: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

PeripheralCentral

Page 16: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

GATT

Page 17: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• Service

• Characteristic

• Data

• Descriptor

GATT

Page 18: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• Heart Rate Service

• Heart Rate Measurement

• Data

• Descriptor

00002A37-0000-1000-8000-00805F9B34FB

GATT

Heart Rate Monitor

0000180D-0000-1000-8000-00805F9B34FB

Property: Notify

Page 19: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Property: Indicate

• Health Thermometer

• Temperature Measurement

• Data

• Descriptor

GATT

Health Thermometer

00001809-0000-1000-8000-00805F9B34FB

00002A1C-0000-1000-8000-00805F9B34FB

Page 20: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• Read

• Write

• Notify

• Indicate

Characteristic Properties

Page 21: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

UUID

00002A37-0000-1000-8000-00805F9B34FB

0x2A37Heart Rate Measurement

Page 22: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• 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/

Page 23: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

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

Page 24: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 25: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 26: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 27: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 28: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Property: Indicate

• Health Thermometer

• Temperature Measurement

• Data

• Descriptor

GATT

Health Thermometer

00001809-0000-1000-8000-00805F9B34FB

00002A1C-0000-1000-8000-00805F9B34FB

Page 29: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 30: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• Central

• Android 4.3 (API Level 18)

• Peripheral

• Android 5.0 (API Level 21)

• Specific BLE chip

Requirement in Android

Page 31: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

CENTRAL

Page 32: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• Discover

• Connect

• Explore

• Interact

Central 的四個階段

Page 33: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/><uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

Bluetooth 權限

在 AndroidManifest.xml 加上

Page 34: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Bluetooth 權限確定是否支援 BLEgetPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

@Overridepublic 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);

要求打開藍牙

Page 35: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Discover (搜尋裝置)

BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter adapter = manager.getAdapter();

adapter.startLeScan(scanCallback);

adapter.stopLeScan(scanCallback);

拿到 BluetoothAdapter

開始搜尋

結束搜尋

This method was deprecated in API level 21

API level 18

Page 36: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Discover (搜尋裝置)

BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device , final int rssi , byte[] scanRecord) { addDevice(device, rssi); // Add device to list } };

搜尋的 Callback

This method was deprecated in API level 21

API level 18

Page 37: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

scanner.stopScan(scanCallback);

scanner.startScan(filters, settings, scanCallback); 開始搜尋

結束搜尋

Discover (搜尋裝置)改用 BluetoothLeScanner

API level 21

BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter adapter = manager.getAdapter();

BluetoothLeScanner scanner = adapter.getBluetoothLeScanner(); // Setting ScanFilter and ScanSettings

Page 38: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Discover (搜尋裝置)

ArrayList<ScanFilter> filters = new ArrayList<>(); ScanFilter.Builder fb = new ScanFilter.Builder(); fb.setServiceUuid(new ParcelUuid(UUID.fromString("..."))); filters.add(fb.build()); ScanSettings.Builder sb = new ScanSettings.Builder(); ScanSettings settings = sb.build();

搜尋選項

API level 21

Page 39: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Discover (搜尋裝置)搜尋選項

• ScanFilter

• ServiceUuid

• ServiceData

• DeviceAddress

• DeviceName

• ManufacturerData

API level 21

Page 40: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Discover (搜尋裝置)搜尋選項

• ScanSettings

• CallbackType (All matches / First match / Match lost)

• MatchMode

• NumOfMatches

• ScanMode (Low latency / Balanced / Low power)

API level 21

Page 41: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

ScanCallback scanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType , ScanResult result) { if (callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES) { // Add device to list } } @Override public void onScanFailed(int errorCode) { Log.e("Scan Failed", "Error Code: " + errorCode); }

@Override public void onBatchScanResults(List<ScanResult> results) { }};

Discover (搜尋裝置)搜尋 Callback

API level 21

Page 42: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Discover (搜尋裝置)列出搜尋到的裝置

Page 43: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Connect (連接裝置)

連接裝置String deviceAddress = "0A:0B:0C:0D:0F:10"; BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);

BluetoothGatt mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

API level 18

Page 44: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt , int status, int newState) { switch (newState) { case BluetoothProfile.STATE_CONNECTED: // do discoverServices break; case BluetoothProfile.STATE_DISCONNECTED: break; case BluetoothProfile.STATE_CONNECTING: case BluetoothProfile.STATE_DISCONNECTING: break; } } // More ...};

Connect (連接裝置)GattCallback

API level 18

Page 45: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Explore (探索資料結構)搜尋 Services

API level 18

mBluetoothGatt.discoverServices();

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { // ... Continued @Override public void onServicesDiscovered(BluetoothGatt gatt , int status) { if (status == BluetoothGatt.GATT_SUCCESS) { // Do Something } }

// More ...};

GattCallback (續)

Page 46: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Explore (探索資料結構)

取得 Services

API level 18

BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("..."));

BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID.fromString("..."));

取得 Characteristic

Page 47: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Explore (探索結構)列出所有的 Services

Page 48: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Explore (探索結構)列出所有的 Characteristics

Page 49: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Interact (互動)讀取 Characteristic 的值

API level 18

byte[] bytes = characteristic.getValue();

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { // ... Continued @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { // Do Something } }

// More ...};

GattCallback (續)

Page 50: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Interact (互動)讀取 Characteristic 的值

API level 18

byte[] bytes = characteristic.getValue();

float floatValue = characteristic.getFloatValue( BluetoothGattCharacteristic.FORMAT_FLOAT, 0); int intValue = characteristic.getIntValue( BluetoothGattCharacteristic.FORMAT_SINT8, 0); int uintValue = characteristic.getIntValue( BluetoothGattCharacteristic.FORMAT_UINT8, 0); String stringValue = characteristic.getStringValue(0);

還可以這樣讀取

mBluetoothGatt.setCharacteristicNotification(characteristic, true); UUID CCCD = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCCD); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor);

Page 51: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Interact (互動)註冊 Characteristic 的 Notfication 更新

API level 18

mBluetoothGatt.setCharacteristicNotification(characteristic, true); UUID CCCD = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCCD); descriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor);

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { // ... Continued @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { // Do Something } };

GattCallback (續)

Page 52: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Interact (互動)存取 Characteristics

Page 53: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Samples

https://github.com/j796160836/nRFSample_uCXpresso-Android

nRFSample-Android

https://github.com/j796160836/nRFSample_uCXpresso-iOSnRFSample-iOS

https://github.com/googlesamples/android-BluetoothLeGattandroid-BluetoothLeGatt

Page 54: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Develop tools

https://play.google.com/store/apps/details?id=no.nordicsemi.android.nrftoolbox

nRF Toolbox

https://itunes.apple.com/tw/app/id557428110

LightBlue Explorer (iOS)https://itunes.apple.com/us/app/id820906058nRF Toolbox (iOS)

https://play.google.com/store/apps/details?id=com.kyriakosalexandrou.bluetoothsupportcheck

BLE Central,Peripheral Check

https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner

BLE Scanner: Read,Write,Notify

Andorid

Page 55: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Develop tools

https://itunes.apple.com/tw/app/id557428110

LightBlue Explorerhttps://itunes.apple.com/us/app/id820906058nRF Toolbox

iOS

https://itunes.apple.com/tw/app/lightblue/id639944780?mt=12

MacLightBlue

Page 56: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

MessageHorn

Page 57: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

MessageHorn for iOS

https://itunes.apple.com/tw/app/messagehorn/id994783068

Page 58: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

MessageHorn for Android WearWhen messaging

Connected to device

Device selectionMain

https://play.google.com/store/apps/details?id=com.JohnnyWorks.messagehorn

Page 59: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
Page 60: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

MessageHorn

https://play.google.com/store/apps/details?id=com.JohnnyWorks.messagehorn

https://itunes.apple.com/tw/app/messagehorn/id994783068

iOS Server Android Client

iOS Server

Android Client

Page 61: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

AndroidWear

Page 62: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

Q&A

Page 63: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

• https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx

• https://www.safaribooksonline.com/library/view/getting-started-with/9781491900550/ch04.html

• http://gogoprivateryan.blogspot.tw/2014/09/cc2540-bluetooth-low-energy-3.html

• http://www.redicecreations.com/article.php?id=31630

• http://mbientlab.com/blog/bluetooth-low-energy-introduction/

References

Page 64: Everything About Bluetooth (淺談藍牙 4.0) - Central 篇

https://www.facebook.com/groups/353660298163446/

穿戴式裝置開發者社群

歡迎您的加⼊入