Reputation: 1409
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?
Upvotes: 4
Views: 1458
Reputation: 1006594
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.
Upvotes: 9
Reputation: 31996
You can check the current API level with Build.VERSION.SDK_INT
. Use in if/case statement to select different code for different APIs. Also, if you have a minSdk that is set less, you might get errors or warnings about code specifically for later APIs. You can address these by annotating these later API classes with @TargetApi(APILEVEL)
Upvotes: 0