Android O issues with WiFi Peer Discovery

2.3k Views Asked by At

I am developing an Android Application that employs WiFi (Direct) for service discovery and P2P peer discovery/connection.

My development enviromment is as follows:-

Android Studio 3.0 Beta 4
Build #AI-171.4304935, built on August 29, 2017
JRE: 1.8.0_152-release-915-b01 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.11.6

My Gradle build:-

apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.research.wifi_direct"

        minSdkVersion 19
        targetSdkVersion 26

        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    buildTypes {

        debug {
            buildConfigField "String", "INSTANCE_NAME", "\"_vascodagama\""
            buildConfigField "String", "SERVICE_TYPE", "\"_presence._tcp\""
        }

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support:recyclerview-v7:26.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    implementation 'com.yarolegovich:lovely-dialog:1.0.7'

    implementation 'io.realm:android-adapters:2.1.0'


    testImplementation 'junit:junit:4.12'

    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
}

The devices I am testing on include:-

Nexus 7 - Android 6.0.1
Nexus 9 - Android 7.1.1
Samsung Tablet - Android 4.4.1
Pixel - Android 8.0.0 

The service discovery works fine on all four devices.

However the P2P Peers List is always empty on the Pixel XL running Android 8.0.0. The other three devices consistently discover each other as well as other miscellaneous devices around my office e.g. such as a Samsung TV and my colleagues personal phones etc..

The only thing I can think of is that theres something about Android O that inhibits P2P peers being discovered. I cannot see anything in the "Whats New In Android O" to identify the culprit

Has anyone seen this behaviour on Android O? What am I doing wrong? What am I missing?

The code I am basing my application on is to be found here

https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html https://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html

Something odd that I noticed was that when I tried to keep the WiFi "stack" awake using an android.net.wifi.WifiManager.WifiLock my application stopped working altogether. Further more I had to power off all my devices and restart them once I had removed the associated WiFiLock code. And yes... I did add uses-permission android:name="android.permission.WAKE_LOCK".

UPDATE - 0001

Whats odd is that when I log out the extras available in my Broadcast Receiver for PEER_CHANGED I can see the available peers listed as shown here when the Android O Pixel XL has discovered my Nexus

WiFiDirectBroadcastRcvr: android.net.wifi.p2p.PEERS_CHANGED<<<<<EXTRA DATA :: ------------------------START
WiFiDirectBroadcastRcvr: EXTRA DATA :: wifiP2pDeviceList 
                         Device: Nexus 7
                          deviceAddress: da:50:e6:7c:56:4e
                          primary type: 000A0050F2040005
                          secondary type: null
                          wps: 392
                          grpcapab: 0
                          devcapab: 37
                          status: 3
                          wfdInfo: WFD enabled: trueWFD DeviceInfo: 0
                          WFD CtrlPort: 0
                          WFD MaxThroughput: 0 (android.net.wifi.p2p.WifiP2pDeviceList)
WiFiDirectBroadcastRcvr: EXTRA DATA :: ------------------------FINISH

However when I then make my subsequent call to requestPeers I always receive an empty WifiP2pDeviceList?

case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION:
                Log.d(TAG, "onReceive: WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION");
                mWiFiPeer2PeerCommander.requestPeers(mWifiP2pManager, mWifiP2pManagerChannel);
                break;

Am I supposed to use the WifiP2pDeviceList from the passed Extra an not bother to call requestPeers?

3

There are 3 best solutions below

0
On BEST ANSWER

I had the same problem. On my nexus 5x now also Android O. And I also always got an empty WifiP2pDeviceList. I think it's assign (?) with similar problem on Android 6 SCAN_RESULTS_AVAILABLE_ACTION return empty list in Android 6.0

It works for me...

0
On

I faced the similar issue. I fixed it by adding

Step 1 : Add Location permission in manifest.

Step 2: Request permission dynamically in your activity.

Step 3: Now scan for WIFI peers. It will appear.

Kudos!!!

0
On

Set the targetSdkVersion <= 25 can solve this issue,but I don't know why.