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.

simple_tracking.dart view source

1
import 'dart:io';
2
import 'package:flutter/material.dart';
3
import 'package:nb_asset_tracking_flutter/nb_asset_tracking_flutter.dart';
4
import 'package:nb_asset_tracking_flutter_example/util/consts.dart';
5
import 'package:nb_asset_tracking_flutter_example/util/permiss_checker.dart';
6
import 'package:nb_asset_tracking_flutter_example/util/toast_mixin.dart';
7
import 'package:uuid/uuid.dart';
8
9
class SimpleTrackingExample extends StatefulWidget {
10
@override
11
SimpleTrackingExampleState createState() => SimpleTrackingExampleState();
12
}
13
14
class SimpleTrackingExampleState extends State<SimpleTrackingExample>
15
with ToastMixin
16
implements OnTrackingDataCallBack {
17
bool bindAsset = false;
18
final assetTracking = AssetTracking();
19
String locationInfo = "";
20
String assetId = "";
21
bool isTracking = false;
22
23
@override
24
void initState() {
25
super.initState();
26
initAssetTracking();
27
createAndBindAssetId();
28
}
29
30
void initAssetTracking() {
31
assetTracking.initialize(apiKey: accessKey);
32
assetTracking.setFakeGpsConfig(allow: true);
33
assetTracking.addDataListener(this);
34
}
35
36
@override
37
Widget build(BuildContext context) {
38
return Scaffold(
39
appBar: AppBar(
40
toolbarHeight: 0,
41
),
42
body: Container(
43
padding: EdgeInsets.all(16),
44
child: Column(
45
crossAxisAlignment: CrossAxisAlignment.start,
46
children: [
47
Row(
48
children: [
49
ElevatedButton(
50
onPressed: (bindAsset && !isTracking)
51
? () async {
52
if (Platform.isAndroid) {
53
var granted = await checkAndRequestLocationPermission();
54
if (!granted) {
55
showToast("Please granted location access for this app");
56
return;
57
}
58
}
59
if (assetId.isEmpty) {
60
showToast("You mast bind a asset Id first");
61
return;
62
}
63
assetTracking.startTracking();
64
}
65
: null,
66
child: const Text("Start Tracking"),
67
),
68
Padding(
69
padding: const EdgeInsets.only(left: 8.0),
70
child: ElevatedButton(
71
onPressed: isTracking
72
? () {
73
assetTracking.stopTracking();
74
}
75
: null,
76
child: const Text("Stop Tracking"),
77
),
78
),
79
],
80
),
81
Padding(
82
padding: const EdgeInsets.only(top: 8.0),
83
child: Text("Current Asset id: $assetId"),
84
),
85
Padding(
86
padding: const EdgeInsets.only(top: 10.0, bottom: 10),
87
child: Text("Asset Tracking status: ${isTracking ? "on" : "off"} "),
88
),
89
Text(locationInfo)
90
],
91
),
92
),
93
);
94
}
95
96
void createAndBindAssetId() async {
97
AssetProfile profile = AssetProfile(
98
customId: const Uuid().v4().toString(), name: "test asset", description: "asset descriptions", attributes: {});
99
AssetResult result = await assetTracking.createAsset(profile: profile);
100
if (result.success) {
101
String assetID = result.data;
102
var assetResult = await assetTracking.bindAsset(customId: assetID);
103
if (assetResult.success) {
104
showToast("Bind asset successfully with asset id ${assetResult.data}");
105
setState(() {
106
assetId = assetResult.data;
107
bindAsset = true;
108
});
109
} else {
110
showToast(assetResult.msg.toString());
111
}
112
} else {
113
showToast(result.msg.toString());
114
}
115
}
116
117
@override
118
void onLocationFailure(String message) {}
119
120
@override
121
void onLocationSuccess(NBLocation location) {
122
setState(() {
123
locationInfo = "------- Location Info ------- \n"
124
"Provider: ${location.provider} \n"
125
"Latitude: ${location.latitude}\n"
126
"Longitude: ${location.longitude}\n"
127
"Altitude: ${location.altitude}\n"
128
"Accuracy: ${location.accuracy}\n"
129
"Speed: ${location.speed}\n"
130
"Bearing: ${location.heading}\n"
131
"Time: ${location.timestamp}\n";
132
});
133
}
134
135
@override
136
void onTrackingStart(String message) {
137
setState(() {
138
isTracking = true;
139
});
140
}
141
142
@override
143
void onTrackingStop(String message) {
144
setState(() {
145
isTracking = false;
146
locationInfo = "";
147
});
148
}
149
150
@override
151
void dispose() {
152
super.dispose();
153
assetTracking.removeDataListener(this);
154
assetTracking.stopTracking();
155
}
156
}

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, the initAssetTracking() function is called to initialize asset tracking. It sets up the necessary configurations and listeners.
  • Add Asset tracking callback:

    • The SimpleTrackingExampleState class implements the OnTrackingDataCallBack interface, which defines callback methods for handling tracking events such as location updates and tracking status changes.
  • 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.
  • 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() and onTrackingStop() methods update the UI to reflect the tracking status.
  1. Initialize Asset Tracking

    1. assetTracking.initialize(apiKey: accessKey);
    2. assetTracking.addDataListener(this);
  2. Create and Bind Asset

    1. AssetProfile profile = AssetProfile(...)
    2. AssetResult result = await assetTracking.createAsset(profile: profile);
    3. var assetResult = await assetTracking.bindAsset(customId: assetID);
  3. Check Permissions and Start Tracking

    1. var granted = await checkAndRequestLocationPermission();
    2. assetTracking.startTracking();
    3. assetTracking.stopTracking();
  4. Handle Tracking Events

    1. onLocationSuccess(NBLocation location)
    2. onTrackingStart(String message)
    3. onTrackingStop(String message)

© 2024 NextBillion.ai all rights reserved.