Simple Asset Tracking Example
This example shows:
- Initialize asset tracking: This involves setting up the asset tracking module with necessary configurations such as API keys, data tracking configurations, and fake GPS settings.
- Add Asset tracking callback: Implement callbacks to handle events such as successful location updates, failures, start of tracking, and stop of tracking.
- Create and Bind Asset: Create an asset profile with a custom ID, name, description, and attributes. It then creates and binds this asset to the asset tracking system.
- Check permissions and start tracking: Check for location permissions, and if granted, it starts tracking the asset's location. Provides options to start and stop tracking.
For all code examples, refer to Asset Tracking Flutter Code Examples
simple_tracking.dart view source
This Flutter code snippet demonstrates a simple asset tracking example using a stateful widget and a mixin called ToastMixin to display toast messages. Here's a summary of the functionalities:
-
Initialize asset tracking:
- In the
initState()
method, theinitAssetTracking()
function is called to initialize asset tracking. It sets up the necessary configurations and listeners.
- In the
-
Add Asset tracking callback:
- The
SimpleTrackingExampleState
class implements theOnTrackingDataCallBack
interface, which defines callback methods for handling tracking events such as location updates and tracking status changes.
- The
-
Create and Bind Asset:
- The
createAndBindAssetId()
method creates a new asset profile and binds it to the asset tracking service. It generates a unique ID for the asset and updates the UI accordingly.
- The
-
Check permissions and start tracking:
- The UI consists of two buttons, "Start Tracking" and "Stop Tracking," which are enabled based on certain conditions. The "Start Tracking" button triggers the start of tracking if all conditions are met, including location permission on Android and the presence of a bound asset ID.
-
Handle tracking events:
- The
onLocationSuccess()
method updates the UI with location information whenever a new location is received. - The
onTrackingStart()
andonTrackingStop()
methods update the UI to reflect the tracking status.
- The
Code highlights related to main functionalities:
-
Initialize Asset Tracking
- assetTracking.initialize(apiKey: accessKey);
- assetTracking.addDataListener(this);
-
Create and Bind Asset
- AssetProfile profile = AssetProfile(...)
- AssetResult result = await assetTracking.createAsset(profile: profile);
- var assetResult = await assetTracking.bindAsset(customId: assetID);
-
Check Permissions and Start Tracking
- var granted = await checkAndRequestLocationPermission();
- assetTracking.startTracking();
- assetTracking.stopTracking();
-
Handle Tracking Events
- onLocationSuccess(NBLocation location)
- onTrackingStart(String message)
- onTrackingStop(String message)