0

I am in the process of creating an app that will utilize WiFi Aware to publish a service yet I am unable to properly call the attach method on the WiFiAwareManager object that I instantiated. Everything compiles yet when when I press the button that triggers the method, the app crashes. I have added my code and the debug log down below. Thank you.

My code:

package com.patrickutz.wifiawarepublish;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.aware.AttachCallback;
import android.net.wifi.aware.WifiAwareManager;
import android.net.wifi.aware.WifiAwareSession;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.wifi.aware.PublishConfig;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

// Constant values of WifiAwareManager
String state_change = WifiAwareManager.ACTION_WIFI_AWARE_STATE_CHANGED;
int data_init = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR;
int data_resp = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;

public void publish(View view) {

    // Check whether or not device supports WiFi Aware
    boolean hasWiFiAware = getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);

    // Toast myToast = Toast.makeText(this, message, duration);
    // Messages for whether or not device has WiFi Aware
    Toast hasAware = Toast.makeText(this, "WiFi Aware Supported", Toast.LENGTH_SHORT);
    Toast noAware = Toast.makeText(this, "WiFi Aware Unsupported", Toast.LENGTH_SHORT);

    if (hasWiFiAware) {
        hasAware.show();
    } else {
        noAware.show();
    }

    System.out.println(hasWiFiAware);

    // Create WiFiAwareManager object
    WifiAwareManager wifiAwareManager = (WifiAwareManager)getSystemService(Context.WIFI_AWARE_SERVICE);

    AttachCallback attachCallback = new AttachCallback();
    Handler handler = new Handler();

    wifiAwareManager.attach(attachCallback, handler);





    // Get the text views
    TextView showStateChangeTextView = (TextView) findViewById(R.id.stateChangeTextView);
    TextView showDataPathRoleInitTextView = (TextView) findViewById(R.id.dataPathRoleInitTextView);
    TextView showDataPathRoleRespTextView = (TextView) findViewById(R.id.dataPathRoleRespTextView);

    // Display the new values of current state in the text view.
    showStateChangeTextView.setText("State: " + state_change);
    showDataPathRoleInitTextView.setText("Data Initiator: " + Integer.toString(data_init));
    showDataPathRoleRespTextView.setText("Data Responder: " + Integer.toString(data_resp));

    }
}

The log:

06-26 15:18:20.070 7365-7365/com.patrickutz.wifiawarepublish E/AndroidRuntime: FATAL EXCEPTION: main Process: com.patrickutz.wifiawarepublish, PID: 7365 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:6294)  at android.view.View$PerformClick.run(View.java:24770)  at android.os.Handler.handleCallback(Handler.java:790)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.wifi.aware.WifiAwareManager.attach(android.net.wifi.aware.AttachCallback, android.os.Handler)' on a null object reference at com.patrickutz.wifiawarepublish.MainActivity.publish(MainActivity.java:67) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)  at android.view.View.performClick(View.java:6294)  at android.view.View$PerformClick.run(View.java:24770)  at android.os.Handler.handleCallback(Handler.java:790)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

2 Answers2

2

WiFiAwareManager is supported from API level 26. So the services will not be available in below devices. To avoid Null pointer exception in this case, consider checking it's availability via following line of code:

if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE)) {
            Log.e("ERROR_TAG", "WiFiAwareManager service is not available."); 
            return;
}
//if available then initialize.
WifiAwareManager was = (WifiAwareManager)getSystemService(WifiAwareManager.class);

Refrence: https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
0

The issue all along was that I was running this code on a Pixel (running Android 8.1.0) which does not support WiFi Aware. When I ran the code on a Pixel 2 XL (running Android 8.1.0) the code works fine and does not cause the app to crash. It is worthy to also note that the isAvailable() method causes the app to crash on unsupported devices instead of returning false. Wow! (will contact Google about that)

  • Strange! From the documentation the WiFiAwareManager is added in API level 26 while you're testing on 8.1.0 which is 27, It should work ideally. – TheLittleNaruto Jul 23 '18 at 09:47