Skip to main content

Documentation for wifi Connectivity Class

The ConnectivityCubit class is responsible for managing the connectivity state of the application. It extends the Cubit class from the flutter_bloc package and provides methods to check if the device is connected to a Wi-Fi network, send TCP messages to a server, and manage the Wi-Fi connection. Additionally, the class includes helper methods to convert a string to a hexadecimal representation and handle socket events.

Dependencies

The class utilizes several packages:

  • connectivity_plus
  • network_info_plus
  • wifi_iot
  • flutter_bloc
  • flutter/material.dart
  • dart:async
  • dart:convert
  • dart:developer
  • dart:io

Methods

bool convertToTime(int? timestampMilliseconds)
Converts a timestamp in milliseconds to a DateTime object and checks if it is before the current time. Returns: true if the timestamp is before the current time, false otherwise.

Future<bool> isWifiConnected()
Checks if the device is connected to a Wi-Fi network. Returns: true if connected to Wi-Fi, false otherwise.

List<int> stringToHex(int connectivityCode, String message)
Converts a string to a list of hexadecimal integers.

Parameters:
connectivityCode: The connectivity code.
message: The string to be converted.
Returns: A list of integers representing the hexadecimal values of the characters in the string, including the length of the message and the connectivity code at the beginning.

Future<bool> sendTcpMessage(BuildContext context)
Sends a TCP message to a specified IP address and port, managing the socket connection and handling various socket events.
Parameters:
context: The current BuildContext.
Returns: true if the TCP message is sent successfully, false otherwise.
Throws: An exception if an error occurs while connecting to the server.

Future<void> sendSSID(String message)
Sends the SSID (Service Set Identifier) message to the server. Parameters: message: The SSID message to be sent. Throws: An exception if an error occurs during the connection or sending process.

Usage Example dart Copy code

ConnectivityCubit cubit = ConnectivityCubit();

bool isWifiConnected = await cubit.isWifiConnected();

if (isWifiConnected) {
bool success = await cubit.sendTcpMessage(context);
if (success) {
// TCP message sent successfully
} else {
// Failed to send TCP message
}
} else {
// Device is not connected to a Wi-Fi network
}

State Management

The ConnectivityCubit class manages its state using different states defined in connectivity_state.dart:

ConnectivityUnpaired: Initial state.
ConnectivityLoaded: State when the connectivity is loaded.
ConnectivityLoading: State during the connectivity process.
ConnectivityError: State when there is an error in connectivity.

Helper Properties

late StreamSubscription subscription: For listening to connectivity changes.
ValueNotifier<bool> isDone: Notifier to track the completion status of the connection process.
Socket? socket: Socket instance for TCP communication.
String? lastUpdated: Timestamp of the last update.

Error Handling

The class includes error handling mechanisms in the sendTcpMessage and sendSSID methods to manage exceptions and provide appropriate feedback to the user through custom snackbars and state emissions.