I am in the process of writing an app that will use WiFi Aware to publish a service yet I cannot get passed the initial step of validating whether or not the device supports the service. The API (from Google here: https://developer.android.com/guide/topics/connectivity/wifi-aware) specifies to use this line of code:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);
However, I cannot just put this into my MainActivity class since I have not instantiated an object called "context". I have tried looking around for answers and have not found any that work. Any form of help would be greatly appreciated. Thank you.
UPDATE
It seems to be that using the method by itself works however it will cause the app to crash unless it is called within a method (such as my Publish() method). I have attached my code below for reference. 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);
// Check if WiFi Aware is available
boolean awareAvailable = wifiAwareManager.isAvailable();
// Messages for whether or not WiFi Aware is available
Toast isAvailable = Toast.makeText(this, "WiFi Aware Supported", Toast.LENGTH_SHORT);
Toast notAvailable = Toast.makeText(this, "WiFi Aware Unsupported", Toast.LENGTH_SHORT);
if (awareAvailable) {
isAvailable.show();
} else {
notAvailable.show();
}
// AttachCallback attachCallback = new AttachCallback();
// Handler handler = new Handler();
//
// wifiAwareManager.attach(attachCallback, handler);
// private static final String AWARE_FILE_SHARE_SERVICE_NAME = "Test Publish";
// PublishConfig config = new PublishConfig.Builder()
// .setServiceName(AWARE_FILE_SHARE_SERVICE_NAME)
// .build();
// 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));
}
}