Skip to content

Commit

Permalink
changes in wifi strength
Browse files Browse the repository at this point in the history
  • Loading branch information
engineer-ece committed Feb 12, 2024
1 parent ae07c09 commit d7166b9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon Feb 12 16:02:15 IST 2024
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;


/** Reports connectivity related information such as connectivity type and wifi information. */
public class Connectivity {
Expand Down Expand Up @@ -50,6 +53,25 @@ String getNetworkType() {
return getNetworkTypeLegacy();
}


int wifiStrength_java(){
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);

if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
// If the active network is Wi-Fi, get Wi-Fi signal strength
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int signalStrength = wifiInfo.getRssi(); // Returns the received signal strength in dBm

// Do something with the signalStrength
System.out.println("Wi-Fi Signal Strength: " + signalStrength + " dBm");
return signalStrength;
}
return 0;
}


@SuppressWarnings("deprecation")
private String getNetworkTypeLegacy() {
// handle type for Android versions less than Android 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ConnectivityMethodChannelHandler implements MethodChannel.MethodCallHandle
public void onMethodCall(MethodCall call, @NonNull MethodChannel.Result result) {
if ("check".equals(call.method)) {
result.success(connectivity.getNetworkType());
} else if("wifi_strength".equals(call.method)){
result.success(connectivity.wifiStrength_java());
} else {
result.notImplemented();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ class _MyHomePageState extends State<MyHomePage> {
ConnectivityResult _connectionStatus = ConnectivityResult.none;
final Connectivity _connectivity = Connectivity();
late StreamSubscription<ConnectivityResult> _connectivitySubscription;
late StreamSubscription<int> _strengthValue;
int value=-2;

@override
void initState() {
super.initState();
initConnectivity();
wifiStrength();

_connectivitySubscription =
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);

_strengthValue = _connectivity.onWifiStrengthChanged_java.listen(_updateWifiStrength);
}

@override
void dispose() {
_connectivitySubscription.cancel();
_strengthValue.cancel();
super.dispose();
}

Expand All @@ -82,11 +88,38 @@ class _MyHomePageState extends State<MyHomePage> {
return _updateConnectionStatus(result);
}

Future<void> wifiStrength() async {
late int result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await _connectivity.getWifiStrength();
value = result;
} on PlatformException catch (e) {
developer.log('Couldn\'t check connectivity status', error: e);
return;
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
return Future.value(null);
}

return _updateWifiStrength(result);
}

Future<void> _updateConnectionStatus(ConnectivityResult result) async {
setState(() {
_connectionStatus = result;
});
}

Future<void> _updateWifiStrength(int result) async {
setState(() {
value = result;
});
}

@override
Widget build(BuildContext context) {
Expand All @@ -97,7 +130,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
body: Center(
child: Text(
'Connection Status: ${_connectionStatus.toString()} - hello')),
'Connection Status: ${value} -+- ${_connectionStatus.toString()} - hello')),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ class Connectivity {
Future<ConnectivityResult> checkConnectivity() {
return _platform.checkConnectivity();
}

Future<int> wifiStrength() {
return _platform.wifiStrength();
}

Stream<int> get onWifiStrengthChanged_java {
return _platform.onWifiStrengthChanged;
}
}

0 comments on commit d7166b9

Please sign in to comment.