Asset Tracking Extended
SDK, users can do asset creation and binding, switch between different tracking modes, and receive callbacks for the data tracking.
This detailed example walks you through the following steps:
-
Creating a New Asset Based on User Input: Learn how to generate a new asset within your application, leveraging user input to create assets dynamically.
-
Binding User-Created Asset IDs to the Current Device: Discover the process of associating user-generated asset IDs with the device you're currently using, facilitating tracking and management of these assets.
-
Starting and Stopping Tracking Based on User Operations: Gain insights into initiating and halting tracking operations, giving users control over the tracking process.
-
Switching Between Tracking Modes Based on User Operations: Learn how to transition between different tracking modes, providing flexibility and adaptability to user preferences.
-
Receiving AssetTrackingCallbacks and Displaying Results in the User Interface: Explore how to handle AssetTrackingCallbacks and effectively communicate tracking results to the user interface, ensuring a seamless and informative user experience.
For all code examples, refer to Asset Tracking Android Code Examples
activity_set_profile.xml view source
activity_extended_tracking.xml view source
SetProfileActivity view source
ExtendedTrackingActivity view source
Upon executing the code example provided above, your app's appearance will resemble the following snippet:
Code Highlights
The SetProfileActivity is for an Android activity that allows users to create and bind assets. The activity has the following main steps:
-
Initialize the shared preferences.
-
Initialize the view objects.
-
Read the asset information from the view.
-
Validate the user input.
-
Check whether the asset ID is already set in shared preferences.
-
Create a new asset or bind an existing asset.
-
Save the asset information to shared preferences.
-
Launch the MainActivity if the asset ID is already set in shared preferences and the activity was launched from the splash screen.
The following is a description of each step:
-
To initialize the shared preferences, the activity calls the PreferenceManager.getDefaultSharedPreferences() method. This method returns a SharedPreferences object that contains the default shared preferences for the application.
-
To initialize the view objects, the activity finds the view objects by their ID. The view objects include the following:
-
editIdView: An EditText object that allows users to enter the asset's custom ID.
-
editAssetNameView: An EditText object that allows users to enter the asset's name.
-
editDescriptionView: An EditText object that allows users to enter the asset's description.
-
editAttributesView: An EditText object that allows users to enter the asset's attributes.
-
editAssetIdView: An EditText object that allows users to enter the asset's ID.
-
lastAssetIdView: An EditText object that displays the last used asset ID.
-
-
To read the asset information from the view, the activity gets the text from each of the EditText objects.
-
To validate the user input, the activity checks the following:
-
The asset's custom ID must be a valid UUID.
-
The asset's name must not be empty.
-
The asset's description must not be empty.
-
The asset's attributes must be a valid JSON string.
-
-
To check whether the asset ID is already set in shared preferences, the activity calls the checkAssetId() function.
-
To create a new asset, the activity calls the AssetTracking.instance.createNewAsset() method. This method creates a new asset with the information that was entered by the user.
-
To bind an existing asset, the activity calls the AssetTracking.instance.bindAsset() method. This method binds the asset that is specified by the assetId parameter to the device.
-
To save the asset information to shared preferences, the activity calls the SharedPreferences.Editor.putString() method for each of the asset information keys.
-
To launch the MainActivity, if the asset ID is already set in shared preferences and the activity was launched from the splash screen, the activity calls the launchMainActivity() function.
The ExtendedTrackingActivity is for an Android activity that allows users to start and stop tracking an asset. The activity has the following main steps:
-
Initialize the location permissions manager.
-
Bind an existing asset, if there is one.
-
Initialize the view.
-
Set up the click listeners for the buttons.
-
Implement the onRequestPermissionsResult() method to handle the results of the location permission requests.
-
Implement the onLocationSuccess() and onLocationFailure() methods to handle location updates.
-
Implement the onTrackingStart() and onTrackingStop() methods to handle tracking events.
The following is a description of each step:
-
To initialize the location permissions manager, the activity creates a new LocationPermissionsManager object and sets a listener for the onPermissionResult() event.
-
To bind an existing asset, the activity gets the asset ID from shared preferences and calls the AssetTracking.instance.bindAsset() method.
-
To initialize the view, the activity finds the view objects by their ID. The view objects include the following:
-
start_tracking: A button that starts tracking the asset.
-
stop_tracking: A button that stops tracking the asset.
-
dit_asset_profile: A button that opens the asset profile activity.
-
isStopTracking: A text view that displays the tracking status.
-
locationInfo: A text view that displays location information.
-
locationEngineInfo: A text view that displays the location engine information.
-
-
To set up the click listeners for the buttons, the activity sets a click listener for the start_tracking button that calls the checkPermissionsAndStartTracking() method. The activity also sets a click listener for the stop_tracking button that calls the assetTrackingStop() method.
-
To implement the onRequestPermissionsResult() method, the activity checks the results of the location permission requests and calls the startTracking() method if the permissions are granted.
-
To implement the onLocationSuccess() and onLocationFailure() methods, the activity updates the locationInfo text view with the location information or the error message, respectively.
-
To implement the onTrackingStart() and onTrackingStop() methods, the activity updates the isStopTracking text view to reflect the tracking status.