1+ package vendor .datalogic .service .eventprofiles ;
2+
3+ import android .app .Activity ;
4+ import android .content .Context ;
5+ import android .content .pm .PackageManager ;
6+ import android .net .ConnectivityManager ;
7+ import android .net .NetworkRequest ;
8+ import android .net .NetworkRequest .Builder ;
9+ import android .net .Network ;
10+ import android .net .NetworkCapabilities ;
11+ import android .os .Bundle ;
12+ import androidx .appcompat .app .AppCompatActivity ;
13+
14+ import android .util .Log ;
15+ import android .widget .Toast ;
16+
17+ import com .datalogic .device .PersistenceType ;
18+ import com .datalogic .device .configuration .ConfigException ;
19+ import com .datalogic .device .configuration .ProfileManager ;
20+ import com .datalogic .device .configuration .PropertyID ;
21+
22+ import java .util .HashMap ;
23+
24+ public class MainActivity extends AppCompatActivity {
25+
26+ private String TAG = "EventProfiles" ; // Tag for logging
27+ private ConnectivityManager connectivityManager ; // Manages network connectivity
28+ private ConnectivityManager .NetworkCallback networkCallback ; // Callback for network events
29+ private ProfileManager pm ; // Manages device profiles
30+ private HashMap map ; // Stores profile properties
31+
32+ @ Override
33+ protected void onCreate (Bundle savedInstanceState ) {
34+ super .onCreate (savedInstanceState );
35+ Log .d (TAG , "onCreate is called" );
36+ setContentView (R .layout .activity_main );
37+
38+ // 1. Setting up Wi-Fi connectivity to notify when Access Point changes
39+ setUp ();
40+
41+ // 2. Create a new instance of ProfileManager with the current context
42+ pm = new ProfileManager (this );
43+
44+ // 3. Create a HashMap and add key-value pairs for profile properties
45+ map = new HashMap ();
46+ map .put (PropertyID .GREEN_SPOT_ENABLE , "false" ); // Disable green spot (BooleanProperty)
47+ map .put (PropertyID .DEVICE_NAME_BASE , "wifi" ); // Set device name base to "wifi" (TextProperty)
48+
49+ // 4. Create a profile using the ProfileManager
50+ pm .createProfile (
51+ "wifi_test.json" , // Profile file name
52+ map , // Profile properties
53+ "Wifi Test Profile" , // Profile description
54+ PersistenceType .ENTERPRISE_RESET_PERSISTENT // Persistent across device reboots
55+ );
56+
57+ // Note:
58+ // - When Wi-Fi connection is ready, the profile "wifi_test.json" will be loaded (see onAvailable).
59+ // - When Wi-Fi connection is lost, the profile "wifi_test.json" will be unloaded (see onLost).
60+ }
61+
62+ private void setUp () {
63+ // Initialize ConnectivityManager to monitor network changes
64+ connectivityManager = (ConnectivityManager ) getSystemService (Context .CONNECTIVITY_SERVICE );
65+
66+ // Check for location permission (required for Wi-Fi scanning on API 23+)
67+ if (checkSelfPermission (android .Manifest .permission .ACCESS_FINE_LOCATION ) == PackageManager .PERMISSION_GRANTED ) {
68+ // Permission granted, register network callback
69+ registerNetworkCallback ();
70+ } else {
71+ // Request location permission from the user
72+ requestPermissions (new String []{android .Manifest .permission .ACCESS_FINE_LOCATION }, 1 );
73+ }
74+ }
75+
76+ private void registerNetworkCallback () {
77+ // Define a network callback to handle network events
78+ networkCallback = new ConnectivityManager .NetworkCallback () {
79+ @ Override
80+ public void onAvailable (Network network ) {
81+ // Called when a network becomes available
82+ ConnectivityManager cm = (ConnectivityManager ) getSystemService (Context .CONNECTIVITY_SERVICE );
83+ NetworkCapabilities capabilities = cm .getNetworkCapabilities (network );
84+
85+ // Check if the available network is Wi-Fi
86+ if (capabilities != null && capabilities .hasTransport (NetworkCapabilities .TRANSPORT_WIFI )) {
87+ Toast .makeText (MainActivity .this , "Connected to Wi-Fi" , Toast .LENGTH_SHORT ).show ();
88+ // Load the Wi-Fi profile
89+ pm .loadProfile ("wifi_test.json" );
90+ }
91+ }
92+
93+ @ Override
94+ public void onLost (Network network ) {
95+ // Called when a network is lost
96+ Toast .makeText (MainActivity .this , "Network lost" , Toast .LENGTH_SHORT ).show ();
97+ // Unload the Wi-Fi profile
98+ pm .unloadProfile ();
99+ }
100+ };
101+
102+ // Build a network request to listen for Wi-Fi connectivity changes
103+ NetworkRequest .Builder requestBuilder = new NetworkRequest .Builder ();
104+ requestBuilder .addTransportType (NetworkCapabilities .TRANSPORT_WIFI ); // Listen only for Wi-Fi
105+ connectivityManager .registerNetworkCallback (requestBuilder .build (), networkCallback );
106+ }
107+
108+ @ Override
109+ protected void onPostResume () {
110+ super .onPostResume ();
111+ // Recreate the profile when the activity resumes
112+ pm .createProfile (
113+ "wifi_test.json" , // Profile file name
114+ map , // Profile properties
115+ "Wifi Test Profile" , // Profile description
116+ PersistenceType .ENTERPRISE_RESET_PERSISTENT // Persistent across device reboots
117+ );
118+ }
119+
120+ @ Override
121+ protected void onStop () {
122+ super .onStop ();
123+ Log .d (TAG , "onDestroy is called" );
124+
125+ // Unregister the network callback to avoid memory leaks
126+ if (networkCallback != null ) {
127+ connectivityManager .unregisterNetworkCallback (networkCallback );
128+ }
129+
130+ // Delete the profile when the activity stops
131+ pm .deleteProfile ("wifi_test.json" );
132+ }
133+ }
0 commit comments