Search code examples
androidbluetooth-lowenergytarget-sdkandroid-ble

Which targetSDK to use for BLE?


I’m building an Android app which should run on every device with Bluetooth Low Energy, which means a minSDK of 18. I’m not sure as to which targetSDK I should use however. I read online that it is good practice to always use the latest version for this (API 22). Is this the case, or should I build my application with every targetSDK that I support, i.e. build with the SDK 18 for applications that run API 18, build with SDK 19 for devices with API 19, …?

I’m confused since to start a BLE discovery I can use either startScan() or startLeScan(). The Android documentation tells me: "startLeScan() was deprecated in API level 21, use startScan() instead". I’m unsure what impact this has on which targetSDK I should use to compile my app with. Will devices running API 18 will be able to run my app if I compile with SDK 22 and use startScan(), and will devices running API 22 be able to run my app if I compile with API 18 and use startLeScan()? Or should I really just build my application with every targetSDK that I support like mentioned above?


Solution

  • Let me answer this for you.

    1) you already know minSDK should be 18(cool). Because LE supports is there on or after that.

    2) If you build your app using target sdk 18, it will work on devices supporting Android 5.0/5.1 also.

    3) If you build your app using target >sdk 21, and use startLeScan instead of startScan(startScan is introduced in 5.0/sdk21), it will work on all devices running on >18 api level.

    4) Now the tricky part, there are also other apis which are available on 5.0+, which were missing on lower versions. I will suggest build the app using target of the latest sdk (currently 23). i) Use different apis to achieve same results in different versions. Like if the phone running on lower than Android 5.0, use startLeScan, else startScan. ii) There are differences on Android 5.0 and 5.1 also, so use those methods accordingly.

    5) Some apis like startLeScan are deprecated, but I know, they are still working as they have tied them up with new apis. so until they remove the old apis, they will work on all platforms. This is precisely what deprecation means :)

    So answer of your question "Which target sdk should be use", answer is latest sdk :), and call respective apis based on SDK version. You can get SDK version on run time via android.os.Build.VERSION.SDK_INT