Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optionally using new API on Android

I am pretty sure this question has been asked before, but I could not find it anywhere, so please don't bite.

I am writing an android app, that needs to communicate with nearby devices and I want to used WifiDirect API if two devices that have it happen to be nearby.

But if that's not the case application can still work and it will use other less effective ways to communicate between participating devices (like Wifi Access Point sharing).

I also want this app to be possible to run on older devices like android 2.2 which does not have WifiDirect API at all (>=4.0 I think).

So is there anyway to make my app optionally use new API, but not necessarily in case of older phones?

like image 857
siemanko Avatar asked Jan 15 '23 17:01

siemanko


1 Answers

so please don't bite

Can we at least gnaw a little? Just a nibble?

:-)

So is there anyway to make my app optionally use new API, but not necessarily in case of older phones?

You can wrap your references to new classes/methods in Java version guard blocks:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  // do stuff with WifiP2pManager
}
// optional else block for workaround for older devices

There may be other particulars for WiFiDirect, which I haven't used yet, but that's the basic step.

like image 151
CommonsWare Avatar answered Jan 17 '23 14:01

CommonsWare