package in.sam;
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.util.YailList;
import java.util.ArrayList;
import java.util.List;
@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "android.permission.ACCESS_NETWORK_STATE,android.permission.ACCESS_WIFI_STATE,android.permission.CHANGE_WIFI_STATE")
public class CrystalWiFi extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
private WifiManager wifiManager;
private BroadcastReceiver wifiScanReceiver;
public CrystalWiFi(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
List<String> availableNetworks = new ArrayList<>();
for (ScanResult result : wifiManager.getScanResults()) {
availableNetworks.add(result.SSID);
}
YailList networkList = YailList.makeList(availableNetworks);
onNetworksListReceived(networkList);
}
}
};
context.registerReceiver(wifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@SimpleFunction(description = "Scan for available WiFi networks.")
public void ScanNetwork() {
wifiManager.startScan();
}
@SimpleEvent(description = "Fired when the Wi-Fi scan is completed.")
public void onNetworksListReceived(YailList networkList) {
EventDispatcher.dispatchEvent(this, "onNetworksListReceived", networkList);
}
@SimpleFunction(description = "Connect to a WiFi network with SSID and password.")
public void ConnectToNetwork(String ssid, String password) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + password + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}
@SimpleFunction(description = "Disconnect from the currently connected WiFi network.")
public void Disconnect() {
wifiManager.disconnect();
}
}
To scan for available networks you need location permission and GPS must be enabled
Also the startScan method has been deprecated in SDK28, see here WifiManager | Android Developers
You can find a working wifi extension here
Google changed a lot in the last SDK updates and everything around wifi is not so easy anymore, especially connecting to a ssid...
Taifun
package in.sam;
import android.app.Activity;
import android.content.Context;
import android.util.Log; // Add this import statement
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.util.YailList;
import java.util.ArrayList;
import java.util.List;
@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
// Libraries
@UsesLibraries(libraries = "")
// Permissions
@UsesPermissions(permissionNames = "android.permission.ACCESS_NETWORK_STATE,android.permission.ACCESS_WIFI_STATE,android.permission.CHANGE_WIFI_STATE,android.permission.ACCESS_FINE_LOCATION,android.permission.ACCESS_COARSE_LOCATION")
public class CrystalWiFi extends AndroidNonvisibleComponent {
// Activity and Context
private Context context;
private Activity activity;
private WifiManager wifiManager;
private BroadcastReceiver wifiScanReceiver;
public CrystalWiFi(ComponentContainer container) {
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
List<String> availableNetworks = new ArrayList<>();
for (ScanResult result : wifiManager.getScanResults()) {
availableNetworks.add(result.SSID);
}
YailList networkList = YailList.makeList(availableNetworks);
onNetworksListReceived(networkList);
}
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in onReceive: " + e.getMessage());
}
}
};
context.registerReceiver(wifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@SimpleFunction(description = "Scan for available WiFi networks.")
public void ScanNetwork() {
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
wifiManager.startScan();
} else {
wifiManager.startScan();
}
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in ScanNetwork: " + e.getMessage());
}
}
@SimpleEvent(description = "Fired when the Wi-Fi scan is completed.")
public void onNetworksListReceived(YailList networkList) {
try {
EventDispatcher.dispatchEvent(this, "onNetworksListReceived", networkList);
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in onNetworksListReceived: " + e.getMessage());
}
}
@SimpleFunction(description = "Connect to a WiFi network with SSID and password.")
public void ConnectToNetwork(String ssid, String password) {
try {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + password + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in ConnectToNetwork: " + e.getMessage());
}
}
@SimpleFunction(description = "Disconnect from the currently connected WiFi network.")
public void Disconnect() {
try {
wifiManager.disconnect();
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in Disconnect: " + e.getMessage());
}
}
@SimpleFunction(description = "Enable WiFi.")
public void EnableWiFi() {
try {
wifiManager.setWifiEnabled(true);
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in EnableWiFi: " + e.getMessage());
}
}
@SimpleFunction(description = "Disable WiFi.")
public void DisableWiFi() {
try {
wifiManager.setWifiEnabled(false);
} catch (Exception e) {
Log.e("CrystalWiFi", "Error in DisableWiFi: " + e.getMessage());
}
}
}
Hi
I added GPS Permission
Still Problem Persist
This is not only to add permissions in the manifest, you also have to ask for the permissions
Create a test project and use logcat to see what is going on
Taifun