Hi. I need some advice with bluetooth low energy. ...
# android
m
Hi. I need some advice with bluetooth low energy. I need do some communication with device in background mode. I was wondering if exist better solution than background task where periodically start ble scan and if found device, connect to them and check if data exist on it. I found this article https://developer.android.com/reference/android/bluetooth/le/AdvertiseData but never use it and wanted to ask you some details. Do you know its possible to check that device has some new data during scan process or somehow got information when new data is coming on device?. Becuase i think background ble scanning is heavy solution and maybe is something better than scanning all the time, trying to establish connection and keeping subscription on characteristics.
k
Do you know its possible to check that device has some new data during scan process
that can’t be possible, since BLE devices has defined advertise data up-front, so in order to know if there’s something new, you’ll have to connect
👍 1
m
So with advertising data i can easily check that device has some new data and then i can device if i need subscribe on characteristic or not, correct? Im asking because i have never heard about advertising data.
k
Not exactly - advertising data is something defined by the creator of the device - mostly this contains the name and services that device supports. This data is not changeable, so it won’t tell you if there’s something new or not
So advertising data is used, for example, to know what kind of device you’re trying to connect to (hence android system displays different icon per device when scanning)
in order to get data from that device (the stuff you’re after) you have to establish a connection
What you could do, for example, before scanning in background, check for bonded devices - if your device phone connected to said device before, it’s bonded
so, once in background, check for bonded device, try to connect to it and get the data. If it’s not connectable, try scanning to see if it’s nearby
m
So technically if i do some background task which start scanning for example every 10 seconds it will be correct? Beucase i have to fetch any new data from device immedietaly, but device can automatically turn off after few seconds so keeeping connection all the time is not possible.
k
You don’t have to scan once you’re bounded with the device - scanning is there to search for new devices that you want to connect to
So your option is to connect again to said device, once there’s a time to sync
👍 1
m
Thanks for your all answers.
👍 1