In this page

Asset profile Operations

这篇文档目前尚未提供译文,将以原文展示。

This example demonstrates:

  1. Creating an Asset: Learn how to generate a new asset within your application, enabling the introduction of new assets into your tracking system.

  2. Binding an Existing Asset ID to the Current Device: Discover the process of associating an existing asset ID with the device you're currently using, facilitating tracking and management of that asset.

  3. Updating the Current Asset Profile: Gain insights into how to modify the profile of an asset, ensuring that the asset's information remains accurate and up-to-date.

  4. Retrieving Asset Details for the Bound Asset ID: Learn how to access and retrieve detailed information about an asset that has been successfully bound to the current device, enabling comprehensive asset management.

For all code examples, refer to Asset Tracking Android Code Examples

activity_asset_profile_operation.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=".codeexample.AssetProfileOperations">
10
11   <Button
12       android:id="@+id/create_asset"
13       android:layout_width="wrap_content"
14       android:layout_height="wrap_content"
15       android:text="@string/create_asset" />
16
17   <Button
18       android:id="@+id/bind_asset_to_device"
19       android:layout_width="wrap_content"
20       android:layout_height="wrap_content"
21       android:text="@string/bind_asset" />
22
23   <Button
24       android:id="@+id/update_asset"
25       android:layout_width="wrap_content"
26       android:layout_height="wrap_content"
27       android:text="@string/update_asset" />
28
29   <Button
30       android:id="@+id/get_asset_detail"
31       android:layout_width="wrap_content"
32       android:layout_height="wrap_content"
33       android:text="@string/get_asset_detail" />
34
35   <TextView
36       android:id="@+id/asset_detail"
37       android:layout_width="wrap_content"
38       android:layout_height="wrap_content"
39       android:layout_marginTop="10dp" />
40
41</LinearLayout>

AssetProfileOperations view source

1class AssetProfileOperations : AppCompatActivity() {
2    private var assetId = ""
3    private var assetName = "testName"
4    private var assetDescription = "testDescription"
5    private var assetAttributes: Map<String, String> = mapOf("attribute 1" to "test 1", "attribute 2" to "test 2")
6
7    private lateinit var createAssetButton: Button
8    private lateinit var bindAssetButton: Button
9    private lateinit var updateAssetButton: Button
10    private lateinit var getAssetDetailButton: Button
11    private lateinit var assetDetailView: TextView
12
13
14    override fun onCreate(savedInstanceState: Bundle?) {
15        super.onCreate(savedInstanceState)
16        setContentView(R.layout.activity_asset_profile_operation)
17        initView()
18
19        // initialize the Asset Tracking SDK
20        initialize( "PUT YOUR API KEY HERE")
21    }
22
23    private fun initView() {
24        createAssetButton = findViewById(R.id.create_asset)
25        createAssetButton.setOnClickListener {
26            createAsset()
27        }
28
29        bindAssetButton = findViewById(R.id.bind_asset_to_device)
30        bindAssetButton.setOnClickListener {
31            onBindAsset()
32        }
33
34        updateAssetButton = findViewById(R.id.update_asset)
35        updateAssetButton.setOnClickListener {
36            updateAsset()
37        }
38
39        getAssetDetailButton = findViewById(R.id.get_asset_detail)
40        getAssetDetailButton.setOnClickListener {
41            getAssetDetail()
42        }
43
44        assetDetailView = findViewById(R.id.asset_detail)
45    }
46
47    private fun createAsset() {
48        val assetProfile = AssetProfile.Builder().setCustomId(UUID.randomUUID().toString()).setName(assetName)
49            .setDescription(assetDescription).setAttributes(assetAttributes).build()
50
51        createNewAsset(assetProfile, object : AssetApiCallback<AssetCreationResponse> {
52            @SuppressLint("SetTextI18n")
53            override fun onSuccess(result: AssetCreationResponse) {
54                assetId = result.data.id
55                Toast.makeText(
56                    this@AssetProfileOperations,
57                    "create asset successfully with asset id: $assetId",
58                    Toast.LENGTH_LONG
59                ).show()
60
61                val assetJsonString = Gson().toJson(assetProfile)
62                assetDetailView.text = "asset profile is: $assetJsonString"
63            }
64
65            override fun onFailure(exception: Exception) {
66                val exceptionMessage = exception.message ?: ""
67                Toast.makeText(
68                    this@AssetProfileOperations,
69                    "update asset profile failed with error: $exceptionMessage",
70                    Toast.LENGTH_LONG
71                ).show()
72            }
73        })
74    }
75
76    private fun onBindAsset() {
77        bindAsset(assetId, object : AssetApiCallback<Unit> {
78            override fun onSuccess(result: Unit) {
79                Toast.makeText(
80                    this@AssetProfileOperations,
81                    String.format(
82                        "bind asset successfully with assetId: %s",
83                        assetId
84                    ),
85                    Toast.LENGTH_LONG
86                ).show()
87            }
88
89            override fun onFailure(exception: Exception) {
90                val exceptionMessage = exception.message ?: ""
91                Toast.makeText(
92                    this@AssetProfileOperations,
93                    "bind asset failed: $exceptionMessage",
94                    Toast.LENGTH_LONG
95                ).show()
96            }
97        })
98    }
99
100    // Operation of updating asset can only be done after binding to an asset
101    private fun updateAsset() {
102        assetName = "newName"
103        assetDescription = "newDescription"
104        val assetProfile = AssetProfile.Builder().setCustomId(assetId).setName(assetName)
105            .setDescription(assetDescription).setAttributes(assetAttributes).build()
106
107        updateAssetInfo(assetProfile, object : AssetApiCallback<Unit> {
108            @SuppressLint("SetTextI18n")
109            override fun onSuccess(result: Unit) {
110                Toast.makeText(
111                    this@AssetProfileOperations,
112                    "update asset profile successfully",
113                    Toast.LENGTH_LONG
114                ).show()
115
116                val assetJsonString = Gson().toJson(assetProfile)
117                assetDetailView.text = "asset profile is: $assetJsonString"
118            }
119
120            override fun onFailure(exception: Exception) {
121                val exceptionMessage = exception.message ?: ""
122                Toast.makeText(
123                    this@AssetProfileOperations,
124                    "update asset profile failed with error: $exceptionMessage",
125                    Toast.LENGTH_LONG
126                ).show()
127            }
128
129        })
130    }
131
132    // User can only get current asset info, and this operation can be done only after binding to an asset id
133    private fun getAssetDetail() {
134        getAssetInfo(object : AssetApiCallback<GetAssetResponse> {
135            @SuppressLint("SetTextI18n")
136            override fun onSuccess(result: GetAssetResponse) {
137                val asset: Asset = result.data.asset
138                val assetJsonString = Gson().toJson(asset)
139                assetDetailView.text = "full asset info: $assetJsonString"
140            }
141
142            override fun onFailure(exception: Exception) {
143                val exceptionMessage = exception.message ?: ""
144                Toast.makeText(
145                    this@AssetProfileOperations,
146                    "bind asset failed: $exceptionMessage",
147                    Toast.LENGTH_LONG
148                ).show()
149            }
150        })
151    }
152
153}

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

Code Highlights

The above code snippet is for an Android activity that demonstrates how to perform asset profile operations using the Asset Tracking SDK. The activity has the following main functionalities:

  1. Initialize the Asset Tracking SDK.

  2. Create an asset profile.

  3. Bind the asset profile to the device.

  4. Update the asset profile.

  5. Get the asset profile details.

The following is a description of each step:

  1. To initialize the Asset Tracking SDK, the activity calls the initialize() method with your API key.

  2. To create an asset profile, the activity creates an AssetProfile object and sets its properties. The AssetProfile object specifies the asset's custom ID, name, description, and attributes.

  3. To bind the asset profile to the device, the activity calls the bindAsset() method with the asset ID.

  4. To update the asset profile, the activity creates a new AssetProfile object with the updated properties and calls the updateAssetInfo() method.

  5. To get the asset profile details, the activity calls the getAssetInfo() method.

The code snippet also includes a few helper functions:

  1. createNewAsset(): This function creates a new asset and returns the asset ID.

  2. bindAsset(): This function binds an asset profile to the device.

  3. updateAssetInfo(): This function updates an asset profile.

  4. getAssetInfo(): This function gets the details of an asset profile.

没找到你要找的内容?