Bluetooth Low Energy On Android: Top Tips For The Tricky Bits @ GDG Detroit

  Переглядів 20,383

Stuart Kent

4 роки тому

Bluetooth Low Energy (BLE) powers the Internet of Things (IoT): smart watches, smart bulbs, and smart cars all use it for short-range communication. Now that 90% of Android consumer devices and 100% of Android Things devices run software that supports BLE, there’s never been a better time for Android developers to jump into the rapidly-growing IoT ecosystem and start building their own companion apps or custom smart devices.

Unfortunately, Android’s Bluetooth stack has a well-deserved reputation for being difficult to work with. The documentation is patchy, the API abstractions are leaky, and the stack itself is unreliable. I worked through all these challenges while building a pro audio app at the start of 2017, and now I'm sharing my story to save you time.

The beginning of this talk will cover BLE basics. The remainder will showcase code samples and strategies for tackling the quirks of the Android Bluetooth stack. No prior experience with BLE is required to enjoy this talk. You’ll leave excited to tinker with BLE on Android and equipped with a roadmap and toolkit to help you navigate the nasty parts.

Favorite Resources: gist.github.com/stkent/a7f0d6...
Slides: speakerdeck.com/stkent/blueto...

КОМЕНТАРІ
David
David 2 місяці тому
I literally flinched the first two times he said "GATT_ERROR 133"
BhAvAnA PaTiL
BhAvAnA PaTiL 5 місяців тому
hey.. I am not able to get devices... location and BLE is on though
David
David 2 місяці тому
Did you check the permission? You have to even if you put it in the manifest: if((ContextCompat.checkSelfPermission(application, Manifest.permission.ACCESS_FINE_LOCATION)) ==PackageManager.PERMISSION_GRANTED) { val settings = ScanSettings .Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) .build() bluetoothScanner.startScan(null, settings, scanCallback) }
Giga Joules
Giga Joules 7 місяців тому
Wish I had watched this video BEFORE I wrote the app I'm working on right now but at least I feel confident that I've handled things properly :)
Stuart Kent
Stuart Kent 7 місяців тому
Props for getting it all figured out! Glad the talk helped you confirm your approach :)
QK Loh
QK Loh 10 місяців тому
Right on spot, clear and short.
Stuart Kent
Stuart Kent 10 місяців тому
Glad you liked it!
Ashutosh Soni
Ashutosh Soni Рік тому
Thanks a lot, your video on ble is amazing.
Stuart Kent
Stuart Kent Рік тому
Thank you for the kind words; I'm glad you found it helpful!
Ektishaf Ahwaz
Ektishaf Ahwaz Рік тому
This is so old method and cant create operation class for every activity. I have a bettet way of doing this which is so fast and works when you connect multiple BlutoothGatt objects.
Joseph Sebastian
Joseph Sebastian Рік тому
Can you give some explanation for the queuing class request() function, looking for some clarification for line below, getServer(currentOp.getServerId()).perform(currentOp);
Stuart Kent
Stuart Kent Рік тому
The request function around 36:03 is used by external classes to add a new operation to the central queue. Its logic is: - Add the newly-requested operation to the back of the queue. - IF no other operation is currently being processed (i.e., the new operation is the _only_ operation to process), then - Grab the new operation right back out of the queue, - Locate the server that the operation should be sent to `getServer(currentOp.getServerId())`, and - Tell it to perform the new operation `.perform(currentOp)`. `getServerId` would be implemented in your Operation class and would be set when you created the operation. `getServer` would be implemented in your OperationManager class and would be able to look up a connected device using the ID returned by `getServerId`. Hope that helps!
JASH GOPANI
JASH GOPANI Рік тому
while scanning / connecting or doing any other operations, do we need to do it in a background service or calling the callbacks in the activity is ok? I am working with BLE for the first time on android so I am confused about it. Please help me, Sir :)
JASH GOPANI
JASH GOPANI Рік тому
@Stuart Kent Thankyou sir✌️
Stuart Kent
Stuart Kent Рік тому
You can safely call methods from the main thread in your Activity or Fragment. However, note that when your callback methods are invoked by the framework, they will NOT be invoked on the main thread (so you'll need to explicitly return to the main thread before updating your UI).
Fahim Masud Choudhury
Fahim Masud Choudhury Рік тому
This is one of the best presentations I have ever seen on any technical topic, it's concise, informative and perfectly explained. Thank you very much!
Stuart Kent
Stuart Kent Рік тому
Thank you for the very kind words :)
Carlo
Carlo Рік тому
Finally found a good explanation for Bluetooth LE for android. Thanks!
Stuart Kent
Stuart Kent Рік тому
Glad you found it useful; thanks for letting me know!
Rudresh S P
Rudresh S P 2 роки тому
could you please share link for source code download
Stuart Kent
Stuart Kent Рік тому
Unfortunately there is no source code available with this talk (it was based on a proprietary project). I recommend checking out github.com/NordicSemiconductor/Android-nRF-Blinky and the underlying library github.com/NordicSemiconductor/Android-BLE-Library/ to see how to use these concepts in practice.
Rudresh S P
Rudresh S P 2 роки тому
where is the source code
Axa Axa
Axa Axa 2 роки тому
i put a hole in my screen pounding the thumbs up. Edit add back my initial question: So is location required only for scanning?
dirrelito
dirrelito Рік тому
@Axa Axa Also, for API 28 and above, you may use companion device pairing instead, and you won't need any location permission at all.
Axa Axa
Axa Axa 2 роки тому
@r7stuart good to know
r7stuart
r7stuart 2 роки тому
That's correct! One update since this talk was given: if you target API level 28 or newer, you _must_ request ACCESS_FINE_LOCATION; ACCESS_COARSE_LOCATION doesn't cut it any more.
Ofir Bar
Ofir Bar 2 роки тому
HERO!!!!!! YOU ARE A FUC**** HERO
John Melody Me
John Melody Me 2 роки тому
Finally !!!!!!! A saviour is here!!!!! finally someone taught this in ENGLISH !!!! I am subscribing to this channel!
Deacon Eric
Deacon Eric 3 місяці тому
@Izaiah Flynn Definitely, have been watching on flixzone for years myself :)
Izaiah Flynn
Izaiah Flynn 3 місяці тому
a tip: watch movies at flixzone. I've been using them for watching loads of movies lately.
vphoneac
vphoneac 2 роки тому
Finally, that one worthy video for BLE . Thanks!
Dave Bound
Dave Bound 2 роки тому
Wonderful information Stuart - thank you so much for this. Now I might not go mad.
Karlo Gašpar
Karlo Gašpar 2 роки тому
Hi Stuart, first of all thank you so much for this video, I have been doing hybrid mobile app development for the last 2 years(ReactNative and Ionic) most of it has been revolving around Bluetooth Low Energy. We are now producing some new solutions that require the use of Native Android developing, so I wanted to go a little bit deeper to see whats going on on Android. Incredible to see how clunky the actual Android implementation of BLE is. I have come across all of these errors when using the hybrid libraries available for these technologies and the only way to get around them is to implement dirty hacks which become an even bigger mess since you have to do it for hybrid. But on the bright side it's getting better. Right now the iOS side has quite a bigger lead in terms of BLE performance and implementation simplicity, but I hope that changes in the future. Thank you so much for your video and all the work you have poured into this, as well as sharing it with us. This is one of the best videos I saw on how to deal with the weird Bluedroid stack, and helps immensely in planing steps for anyone trying to make a BLE App.
Harshal Bagul
Harshal Bagul 2 роки тому
Hi, I am working on BLE App in Xamarin Forms. I am using BLE plugin for Xamarin. I am facing issue for Notify Property. I am not getting bytes value from Glucose Measurement characteristic. CharacterValueChanged event not called means it is not notifying value. Can you please help me as you are working on BLE App... Thanks
V N Shetty
V N Shetty 2 роки тому
Very informative...
Kevin M
Kevin M 2 роки тому
You're doing God's work good sir
Stuart Kent
Stuart Kent 2 роки тому
🙏Thanks, glad to help!
Nawfal Akadiri
Nawfal Akadiri 2 роки тому
Thanks for your presentation. About library existing, Nordic SemiConductor has a great open source library and i used it for a BLE project( interaction with Android and iPad app). Check Android nrF Blinky on Github
Thom
Thom 3 роки тому
I seriously thought I was the only one experiencing these problems. Pretty sure it took me a week just to figure out we needed ACCESS_COARSE_LOCATION permission enabled. I know there probably isn't that much motivation to do so but I think the google docs on ble need to be updated. Anyways, awesome presentation! Super helpful! Edit: That part on multiple connections is gold.
HyunQ Han
HyunQ Han 3 роки тому
Thanks for sharing this lesson and learned. You've done a great job !
EmilenL
EmilenL 3 роки тому
Nice video! Just a correction: you don't need to serialize among all peripherals. It is enough with one queue per BluetoothGatt object, since the Bluetooth stack internally can hold one queued operation per BluetoothGatt object. Also, the only benefit of not calling close directly after disconnect is to avoid getting a warning in the log about an internal NullPointerException. So I would say the workaround isn't really needed unless you want the log clean.
Piotr Majsterek
Piotr Majsterek 2 роки тому
Tho the author regarding the queueing: also, please mind that device is using one antena for connecting to all devices, how it can transfer radio data simultaniously, so the api follow more less the phisical pattern bere. I think that might be cleare and less confusing why it is not so intuitive at first. Anyway, thank you for the video, lots of info I needed.
Creating Bluetooth LE Devices with Arduino
44:20
Alasdair Allan
Переглядів 9 тис.
Ellisys Bluetooth Video 5: Generic Attribute Profile (GATT)
8:36
Слава купил проблемную BMW. Как жить дальше?
51:49
ИЛЬДАР АВТО-ПОДБОР
Переглядів 2,7 млн
Яхты, взятки и любовница. Что скрывает министр Лавров
36:21
СЕРГЕЙ ОРЛОВ и ГАРИК ОГАНИСЯН в DZK
54:23
ЛЬВЫ НА ДЖИПЕ
Переглядів 236 тис.
Comedy Club: премьерный выпуск 17 сезона
37:49
Телеканал ТНТ
Переглядів 2,3 млн
MVVM  / MVI Repository Pattern with Hilt
1:00:24
CodingWithMitch
Переглядів 41 тис.
Ellisys Bluetooth Video 7: Security Part 1
9:25
Ellisys
Переглядів 17 тис.
Bound Services on Android
43:00
CodingWithMitch
Переглядів 38 тис.
Raspberry Pi & Bluetooth LE part 1 with Tony D! @adafruit #LIVE
1:27:33
Adafruit Industries
Переглядів 54 тис.
#173 ESP32 Bluetooth BLE with  Arduino IDE (Tutorial) and Polar H7
15:55
Andreas Spiess
Переглядів 129 тис.
SmartBond™ Bluetooth® low energy for IoT was never this easy and fast!
40:51
Dialog Semiconductor
Переглядів 1,7 тис.
Слава купил проблемную BMW. Как жить дальше?
51:49
ИЛЬДАР АВТО-ПОДБОР
Переглядів 2,7 млн
Яхты, взятки и любовница. Что скрывает министр Лавров
36:21
СЕРГЕЙ ОРЛОВ и ГАРИК ОГАНИСЯН в DZK
54:23
ЛЬВЫ НА ДЖИПЕ
Переглядів 236 тис.
Comedy Club: премьерный выпуск 17 сезона
37:49
Телеканал ТНТ
Переглядів 2,3 млн
Холостячка - Сезон 2. Выпуск 1 - СМОТРИТЕ ПРЯМО СЕЙЧАС НА TELEPORTAL.UA
10:59
МастерШеф 11 сезон. Выпуск 2 от 04.09.2021
3:40:30
МастерШеф
Переглядів 1,1 млн