Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
-
Upload
johnny-sung -
Category
Technology
-
view
4.248 -
download
1
Transcript of Everything About Bluetooth (淺談藍牙 4.0) - Central 篇
EVERYTHING ABOUT BLUETOOTHJohnny Sung
https://fb.com/j796160836
Johnny SungMobile devices Developer
https://plus.google.com/+JohnnySung
http://about.me/j796160836
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
http://www.redicecreations.com/article.php?id=31630http://www.ettoday.net/dalemon/post/3250
2.0 2.0 + 4.0 Dual Mode 4.0Bluetooth SMART READYBluetooth Bluetooth SMART
Bluetooth low energy Protocol Stack
• Peripheral
• Central
• Broadcaster
• Observer
GAP Roles
PeripheralCentral
PeripheralCentral
PeripheralCentral
GATT
• Service
• Characteristic
• Data
• Descriptor
GATT
• Heart Rate Service
• Heart Rate Measurement
• Data
• Descriptor
00002A37-0000-1000-8000-00805F9B34FB
GATT
Heart Rate Monitor
0000180D-0000-1000-8000-00805F9B34FB
Property: Notify
Property: Indicate
• Health Thermometer
• Temperature Measurement
• Data
• Descriptor
GATT
Health Thermometer
00001809-0000-1000-8000-00805F9B34FB
00002A1C-0000-1000-8000-00805F9B34FB
• Read
• Write
• Notify
• Indicate
Characteristic Properties
UUID
00002A37-0000-1000-8000-00805F9B34FB
0x2A37Heart Rate Measurement
• 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/
https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.health_thermometer.xml
Property: Indicate
• Health Thermometer
• Temperature Measurement
• Data
• Descriptor
GATT
Health Thermometer
00001809-0000-1000-8000-00805F9B34FB
00002A1C-0000-1000-8000-00805F9B34FB
• Central
• Android 4.3 (API Level 18)
• Peripheral
• Android 5.0 (API Level 21)
• Specific BLE chip
Requirement in Android
CENTRAL
• Discover
• Connect
• Explore
• Interact
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 加上
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);
要求打開藍牙
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
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
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
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
Discover (搜尋裝置)搜尋選項
• ScanFilter
• ServiceUuid
• ServiceData
• DeviceAddress
• DeviceName
• ManufacturerData
API level 21
Discover (搜尋裝置)搜尋選項
• ScanSettings
• CallbackType (All matches / First match / Match lost)
• MatchMode
• NumOfMatches
• ScanMode (Low latency / Balanced / Low power)
API level 21
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
Discover (搜尋裝置)列出搜尋到的裝置
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
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
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 (續)
Explore (探索資料結構)
取得 Services
API level 18
BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("..."));
BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID.fromString("..."));
取得 Characteristic
Explore (探索結構)列出所有的 Services
Explore (探索結構)列出所有的 Characteristics
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 (續)
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);
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 (續)
Interact (互動)存取 Characteristics
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
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
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
MessageHorn
MessageHorn for iOS
https://itunes.apple.com/tw/app/messagehorn/id994783068
MessageHorn for Android WearWhen messaging
Connected to device
Device selectionMain
https://play.google.com/store/apps/details?id=com.JohnnyWorks.messagehorn
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
AndroidWear
Q&A
• 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
https://www.facebook.com/groups/353660298163446/
穿戴式裝置開發者社群
歡迎您的加⼊入