Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O issues with WiFi Peer Discovery

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?

like image 403
Hector Avatar asked Sep 07 '17 13:09

Hector


People also ask

What is WIFI P2P service?

However, using Wi-Fi Direct (P2P) Service Discovery allows you to discover the services of nearby devices directly, without being connected to a network. You can also advertise the services running on your device. These capabilities help you communicate between apps, even when no local network or hotspot is available.

What is P2P device?

What does P2P mean? A P2P (peer to peer) connection is a direct communication infrastructure between two peers: a client device (such as a smartphone or a laptop) and an IoT device (such as a surveillance camera, smart door lock, alarm system, heat controller, or anything else that can connect to the internet).


1 Answers

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...

like image 189
Pavel Zavyalov Avatar answered Sep 27 '22 18:09

Pavel Zavyalov