Skip to content

Commit

Permalink
Merge pull request #77 from onaio/76-Request-for-NEARBY_WIFI_DEVICES
Browse files Browse the repository at this point in the history
Implement request for NEARBY_WIFI_DEVICES permission
  • Loading branch information
pld authored Aug 15, 2024
2 parents a0a56c0 + 3a7c48b commit ae8aa97
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion p2p-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

defaultConfig {
minSdk 21
targetSdk 30
targetSdk 34
versionCode 1
versionName "1.0"

Expand Down
1 change: 1 addition & 0 deletions p2p-lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />

<application>
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
context.application.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
}
private val accessFineLocationPermissionRequestInt: Int = 12345
private val nearbyWifiDevicesPermissionRequestInt: Int = 67890
private var wifiP2pChannel: WifiP2pManager.Channel? = null
private var wifiP2pReceiver: BroadcastReceiver? = null

Expand Down Expand Up @@ -160,6 +161,9 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
requestAccessFineLocationIfNotGranted()
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestNearbyWifiDevicesNotGranted()
}
// Check if already connected and disconnect
requestDeviceInfo(onDeviceFound = onDeviceFound, onConnected = onConnected)
}
Expand All @@ -184,13 +188,21 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
}

private fun initiatePeerDiscoveryOnceAccessFineLocationGranted() {
if (ActivityCompat.checkSelfPermission(
if ((ActivityCompat.checkSelfPermission(
context,
android.Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
android.Manifest.permission.ACCESS_FINE_LOCATION)
) != PackageManager.PERMISSION_GRANTED ||
(ActivityCompat.checkSelfPermission(
context,
android.Manifest.permission.NEARBY_WIFI_DEVICES)
) != PackageManager.PERMISSION_GRANTED
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
logDebug("initiatePeerDiscoveryOnceAccessFineLocationGranted(): requesting ACCESS_FINE_LOCATION")
requestAccessFineLocationIfNotGranted()
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
logDebug("initiatePeerDiscoveryOnceAccessFineLocationGranted(): requesting NEARBY_WIFI_DEVICES")
requestNearbyWifiDevicesNotGranted()
} else {
handleMinimumSDKVersionNotMet(Build.VERSION_CODES.M)
}
Expand All @@ -217,6 +229,24 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
private fun requestNearbyWifiDevicesNotGranted() {
when (ActivityCompat.checkSelfPermission(
context,
android.Manifest.permission.NEARBY_WIFI_DEVICES
)
) {
PackageManager.PERMISSION_GRANTED -> logDebug("Wifi P2P: Nearby wifi devices granted")
else -> {
logDebug("Wifi P2P: Requesting Nearby wifi devices granted permission")
return context.requestPermissions(
arrayOf(android.Manifest.permission.NEARBY_WIFI_DEVICES),
nearbyWifiDevicesPermissionRequestInt
)
}
}
}

private fun initiatePeerDiscovery(onDeviceFound: OnDeviceFound?) {
if (ActivityCompat.checkSelfPermission(
context,
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
defaultConfig {
applicationId "org.smartregister.p2p.sample"
minSdk 21
targetSdk 31
targetSdk 34
versionCode 1
versionName "1.0"

Expand Down

0 comments on commit ae8aa97

Please sign in to comment.