• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

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

1<?xml version="1.0" encoding="utf-8"?>
2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context=".SetProfileActivity">
7
8 <ScrollView
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content">
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:orientation="vertical"
16 android:paddingStart="20dp"
17 android:paddingEnd="20dp"
18 android:paddingBottom="20dp">

activity_extended_tracking.xml view source

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="vertical"
8 android:padding="10dp"
9 tools:context=".MainActivity">
10
11 <LinearLayout
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content">
14 <Button
15 android:id="@+id/start_tracking"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:text="@string/start_tracking"

SetProfileActivity view source

1class SetProfileActivity : AppCompatActivity() {
2 private lateinit var sharedPreferences: SharedPreferences
3 private lateinit var editIdView: EditText
4 private lateinit var editAssetNameView: EditText
5 private lateinit var editDescriptionView: EditText
6 private lateinit var editAttributesView: EditText
7 private lateinit var editAssetIdView: EditText
8 private lateinit var lastAssetIdView: EditText
9
10 private lateinit var createNewAssetView: TextView
11 private lateinit var bindAssetView: TextView
12 private lateinit var progressBar: View
13
14 private lateinit var customId: String
15 private lateinit var assetName: String
16 private lateinit var assetDescription: String
17 private lateinit var assetAttributes: String
18 private lateinit var assetId: String

ExtendedTrackingActivity view source

1class ExtendedTrackingActivity : AppCompatActivity(), View.OnClickListener,
2 AssetTrackingCallBack {
3 private lateinit var startTrackingButton: Button
4 private lateinit var stopTrackingButton: Button
5 private lateinit var radioGroup: RadioGroup
6 private lateinit var activeRadioButton: RadioButton
7 private lateinit var trackingStatusView: TextView
8 private lateinit var locationEngineInfoView: TextView
9 private lateinit var locationInfoView: TextView
10 private lateinit var editAssetProfileView: TextView
11
12
13 var permissionsManager: LocationPermissionsManager? = null
14 var currentSelectedTrackingMode = TrackingMode.ACTIVE
15
16 private val mainHandler by lazy {
17 Handler(Looper.getMainLooper())
18 }

Upon executing the code example provided above, your app's appearance will resemble the following snippet:

docs-imagedocs-image
Asset Profile PageAsset Tracking Page

Code Highlights

The SetProfileActivity is for an Android activity that allows users to create and bind assets. The activity has the following main steps:

  1. Initialize the shared preferences.

  2. Initialize the view objects.

  3. Read the asset information from the view.

  4. Validate the user input.

  5. Check whether the asset ID is already set in shared preferences.

  6. Create a new asset or bind an existing asset.

  7. Save the asset information to shared preferences.

  8. 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:

  1. 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.

  2. To initialize the view objects, the activity finds the view objects by their ID. The view objects include the following:

    1. editIdView: An EditText object that allows users to enter the asset's custom ID.

    2. editAssetNameView: An EditText object that allows users to enter the asset's name.

    3. editDescriptionView: An EditText object that allows users to enter the asset's description.

    4. editAttributesView: An EditText object that allows users to enter the asset's attributes.

    5. editAssetIdView: An EditText object that allows users to enter the asset's ID.

    6. lastAssetIdView: An EditText object that displays the last used asset ID.

  3. To read the asset information from the view, the activity gets the text from each of the EditText objects.

  4. To validate the user input, the activity checks the following:

    1. The asset's custom ID must be a valid UUID.

    2. The asset's name must not be empty.

    3. The asset's description must not be empty.

    4. The asset's attributes must be a valid JSON string.

  5. To check whether the asset ID is already set in shared preferences, the activity calls the checkAssetId() function.

  6. 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.

  7. 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.

  8. To save the asset information to shared preferences, the activity calls the SharedPreferences.Editor.putString() method for each of the asset information keys.

  9. 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:

  1. Initialize the location permissions manager.

  2. Bind an existing asset, if there is one.

  3. Initialize the view.

  4. Set up the click listeners for the buttons.

  5. Implement the onRequestPermissionsResult() method to handle the results of the location permission requests.

  6. Implement the onLocationSuccess() and onLocationFailure() methods to handle location updates.

  7. Implement the onTrackingStart() and onTrackingStop() methods to handle tracking events.

The following is a description of each step:

  1. To initialize the location permissions manager, the activity creates a new LocationPermissionsManager object and sets a listener for the onPermissionResult() event.

  2. To bind an existing asset, the activity gets the asset ID from shared preferences and calls the AssetTracking.instance.bindAsset() method.

  3. To initialize the view, the activity finds the view objects by their ID. The view objects include the following:

    1. start_tracking: A button that starts tracking the asset.

    2. stop_tracking: A button that stops tracking the asset.

    3. dit_asset_profile: A button that opens the asset profile activity.

    4. isStopTracking: A text view that displays the tracking status.

    5. locationInfo: A text view that displays location information.

    6. locationEngineInfo: A text view that displays the location engine information.

  4. 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.

  5. 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.

  6. To implement the onLocationSuccess() and onLocationFailure() methods, the activity updates the locationInfo text view with the location information or the error message, respectively.

  7. To implement the onTrackingStart() and onTrackingStop() methods, the activity updates the isStopTracking text view to reflect the tracking status.

Have Questions ?