Skip to content

Commit 012c2ba

Browse files
committed
Add example SDK - Profiles base on Event
Add new example for updating new profiles base on the event related to the wifi connectivity status: - No wifi connection, using normal profile - With wifi connection, using custom profile
1 parent 3a7f844 commit 012c2ba

35 files changed

+890
-0
lines changed

Java/EventProfiles/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.propertiesa
16+
/.idea/
17+
/app/src/androidTest/
18+
/app/src/test/
19+

Java/EventProfiles/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
}
4+
5+
android {
6+
namespace 'vendor.datalogic.service.eventprofiles'
7+
compileSdk 34
8+
9+
defaultConfig {
10+
applicationId "vendor.datalogic.service.eventprofiles"
11+
minSdk 24
12+
targetSdk 34
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation libs.appcompat
34+
implementation libs.material
35+
implementation libs.activity
36+
implementation libs.constraintlayout
37+
testImplementation libs.junit
38+
androidTestImplementation libs.ext.junit
39+
androidTestImplementation libs.espresso.core
40+
implementation 'com.github.datalogic:datalogic-android-sdk:1.40'
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.EventProfiles"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
</activity>
24+
</application>
25+
26+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
27+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
28+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
29+
30+
</manifest>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.ProfileType;
21+
import com.datalogic.device.configuration.PropertyID;
22+
23+
import java.util.HashMap;
24+
25+
public class MainActivity extends AppCompatActivity {
26+
27+
private String TAG = "EventProfiles"; // Tag for logging
28+
private String profileName = "wifi_test.json";
29+
private ConnectivityManager connectivityManager; // Manages network connectivity
30+
private ConnectivityManager.NetworkCallback networkCallback; // Callback for network events
31+
private ProfileManager pm; // Manages device profiles
32+
private HashMap map; // Stores profile properties
33+
34+
@Override
35+
protected void onCreate(Bundle savedInstanceState) {
36+
super.onCreate(savedInstanceState);
37+
Log.d(TAG, "onCreate is called");
38+
setContentView(R.layout.activity_main);
39+
40+
// 1. Setting up Wi-Fi connectivity to notify when Access Point changes
41+
setUp();
42+
43+
// 2. Create a new instance of ProfileManager with the current context
44+
pm = new ProfileManager(this);
45+
46+
// 3. Create a HashMap and add key-value pairs for profile properties
47+
map = new HashMap();
48+
map.put(PropertyID.GREEN_SPOT_ENABLE, "false"); // Disable green spot (BooleanProperty)
49+
map.put(PropertyID.DEVICE_NAME_BASE, "wifi"); // Set device name base to "wifi" (TextProperty)
50+
51+
// 4. Create a profile using the ProfileManager
52+
// Step 4.1: Check if "wifi_test.json" profile already exists
53+
boolean profileExists = false;
54+
for (ProfileType name : pm.getProfilesList()) {
55+
if (profileName.equals(name.name)) {
56+
profileExists = true;
57+
break;
58+
}
59+
}
60+
61+
// Step 4.2: If not exists, create it
62+
if (!profileExists) {
63+
pm.createProfile(
64+
profileName, // Profile file name
65+
map, // Profile properties
66+
"Wifi Test Profile", // Profile description
67+
PersistenceType.ENTERPRISE_RESET_PERSISTENT // Persistent across device reboots
68+
);
69+
}
70+
71+
// Note:
72+
// - When Wi-Fi connection is ready, the profile "wifi_test.json" will be loaded (see onAvailable).
73+
// - When Wi-Fi connection is lost, the profile "wifi_test.json" will be unloaded (see onLost).
74+
}
75+
76+
private void setUp() {
77+
// Initialize ConnectivityManager to monitor network changes
78+
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
79+
80+
// Check for location permission (required for Wi-Fi scanning on API 23+)
81+
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
82+
// Permission granted, register network callback
83+
registerNetworkCallback();
84+
} else {
85+
// Request location permission from the user
86+
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
87+
}
88+
}
89+
90+
private void registerNetworkCallback() {
91+
Log.d(TAG, "registerNetworkCallback: Callback registered");
92+
// Define a network callback to handle network events
93+
networkCallback = new ConnectivityManager.NetworkCallback() {
94+
@Override
95+
public void onAvailable(Network network) {
96+
Log.d(TAG, "onAvailable: Network is available");
97+
// Called when a network becomes available
98+
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
99+
NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
100+
101+
// Check if the available network is Wi-Fi
102+
if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
103+
Toast.makeText(MainActivity.this, "Connected to Wi-Fi", Toast.LENGTH_SHORT).show();
104+
// Load the Wi-Fi profile
105+
pm.loadProfile(profileName);
106+
}
107+
}
108+
109+
@Override
110+
public void onLost(Network network) {
111+
Log.d(TAG, "onLost: Network is lost");
112+
// Called when a network is lost
113+
Toast.makeText(MainActivity.this, "Network lost", Toast.LENGTH_SHORT).show();
114+
// Unload the Wi-Fi profile
115+
pm.unloadProfile();
116+
}
117+
};
118+
119+
// Build a network request to listen for Wi-Fi connectivity changes
120+
NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
121+
requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); // Listen only for Wi-Fi
122+
connectivityManager.registerNetworkCallback(requestBuilder.build(), networkCallback);
123+
}
124+
125+
@Override
126+
protected void onDestroy() {
127+
super.onDestroy();
128+
Log.d(TAG, "onDestroy is called");
129+
130+
// Unregister the network callback to avoid memory leaks
131+
if (networkCallback != null) {
132+
connectivityManager.unregisterNetworkCallback(networkCallback);
133+
}
134+
135+
// Delete the profile when the activity destroys
136+
pm.deleteProfile(profileName);
137+
}
138+
}

0 commit comments

Comments
 (0)