Wifi receiver

This commit is contained in:
Jens 2023-12-21 23:16:21 +01:00
parent 5f8ed5765a
commit a21f90acb5
4 changed files with 8 additions and 19 deletions

View File

@ -52,7 +52,6 @@
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />-->
<uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.NFC" />

View File

@ -52,7 +52,6 @@
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />-->
<uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.NFC" />

View File

@ -51,7 +51,6 @@
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />-->
<uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.NFC" />

View File

@ -1,6 +1,5 @@
package com.jens.automation2.location; package com.jens.automation2.location;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -30,7 +29,7 @@ public class WifiBroadcastReceiver extends BroadcastReceiver
protected static boolean mayCellLocationChangedReceiverBeActivatedFromWifiPointOfView = true; protected static boolean mayCellLocationChangedReceiverBeActivatedFromWifiPointOfView = true;
protected static WifiBroadcastReceiver wifiBrInstance; protected static WifiBroadcastReceiver wifiBrInstance;
protected static IntentFilter wifiListenerIntentFilter; protected static IntentFilter wifiListenerIntentFilter;
protected static boolean wifiListenerActive=false; protected static boolean wifiListenerActive = false;
final static String unknownSsidName = "<unknown ssid>"; final static String unknownSsidName = "<unknown ssid>";
@ -46,13 +45,16 @@ public class WifiBroadcastReceiver extends BroadcastReceiver
public static void setLastWifiSsid(String newWifiSsid) public static void setLastWifiSsid(String newWifiSsid)
{ {
// Remove double quotes
if(newWifiSsid.startsWith("\"") && newWifiSsid.endsWith("\"")) if(newWifiSsid.startsWith("\"") && newWifiSsid.endsWith("\""))
newWifiSsid = newWifiSsid.substring(1, newWifiSsid.length()-1); newWifiSsid = newWifiSsid.substring(1, newWifiSsid.length()-1);
// If it's a real name, not an empty string, it's stored as the last ssid
if(newWifiSsid.length() > 0) if(newWifiSsid.length() > 0)
{ {
if(newWifiSsid.equals(unknownSsidName)) if(newWifiSsid.equals(unknownSsidName))
WifiBroadcastReceiver.lastWifiSsidReal = lastWifiSsid; WifiBroadcastReceiver.lastWifiSsidReal = lastWifiSsid;
WifiBroadcastReceiver.lastWifiSsid = newWifiSsid; WifiBroadcastReceiver.lastWifiSsid = newWifiSsid;
} }
} }
@ -72,24 +74,15 @@ public class WifiBroadcastReceiver extends BroadcastReceiver
{ {
try try
{ {
// int state = -1;
NetworkInfo myWifi = null; NetworkInfo myWifi = null;
if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) // fired upon disconnection if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) // fired upon disconnection
{ {
// state = intent.getIntExtra(WifiManager.NETWORK_STATE_CHANGED_ACTION, -1);
// Miscellaneous.logEvent("i", "WifiReceiver", "NETWORK_STATE_CHANGED_ACTION: " + String.valueOf(state));
myWifi = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); myWifi = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
} }
WifiManager myWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiManager myWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
// ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
// myWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// myWifi = state
// WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
// SupplicantState supState = wifiInfo.getSupplicantState();
if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) // fired upon connection if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) // fired upon connection
{ {
String ssid = myWifiManager.getConnectionInfo().getSSID(); String ssid = myWifiManager.getConnectionInfo().getSSID();
@ -132,7 +125,7 @@ public class WifiBroadcastReceiver extends BroadcastReceiver
} }
else if(!myWifi.isConnectedOrConnecting()) // really disconnected? because sometimes also fires on connect else if(!myWifi.isConnectedOrConnecting()) // really disconnected? because sometimes also fires on connect
{ {
if(wasConnected) // wir könnten einfach noch nicht daheim sein if(wasConnected) // we could simply not be home yet
{ {
try try
{ {
@ -226,17 +219,16 @@ public class WifiBroadcastReceiver extends BroadcastReceiver
{ {
try try
{ {
if(wifiListenerActive) if (wifiListenerActive)
{ {
Miscellaneous.logEvent("i", "Wifi Listener", "Stopping wifiListener", 4); Miscellaneous.logEvent("i", "Wifi Listener", "Stopping wifiListener", 4);
wifiListenerActive = false; wifiListenerActive = false;
parentLocationProvider.getParentService().unregisterReceiver(wifiBrInstance); parentLocationProvider.getParentService().unregisterReceiver(wifiBrInstance);
} }
} }
catch(Exception ex) catch (Exception ex)
{ {
Miscellaneous.logEvent("e", "Wifi Listener", "Error stopping wifiListener: " + Log.getStackTraceString(ex), 3); Miscellaneous.logEvent("e", "Wifi Listener", "Error stopping wifiListener: " + Log.getStackTraceString(ex), 3);
} }
} }
} }