概要
DataWedge サービスは、Android を実行しているすべての Zebra モバイル デバイスにプリインストールされており、デバイス上のほぼすべてのアプリにスキャン機能を追加できます。この例では、DataWedge で Android インテントによってスキャンしたバーコード データを受信する方法を、簡略化したアプリケーションで説明します。
基本的なプロセスは、次のようになります。
- Zebra デバイスに (スキャンを有効にする) ターゲット Android アプリを展開します。
- デバイスで DataWedge を起動します。
- DataWedge の入力 (バーコード スキャナ) と出力 (Intent) を構成します。
- オプション: ターゲット アプリに関連付けるプロファイルを作成します。プロファイルが作成されておらず、割り当てられていない場合は、このために設計された組み込みプロファイルである Profile0 が使用されます。
詳細およびソース コードは、次のとおりです。
DataWedge の構成
インテントを介してスキャンを出力するように DataWedge を構成します。
1.[アプリケーション] --> [DataWedge] で DataWedge を起動します
2.プロファイルを選択します (プロファイルが明示的に割り当てられていないアプリには、Profile0 が使用されます)
3.次のプロファイル設定を確認します。
- プロファイルが有効になっている
- バーコード入力が有効になっている
- インテント出力が有効になっている
4.次のようにインテント出力を構成します。
- インテント アクション: com.dwexample.ACTION (
strings.xmlファイルで定義されている値と一致) - インテント カテゴリ: (空白のまま)
- インテント配信: ブロードキャスト インテント
次の図は、正しいプロファイルの設定を示しています。
これらの設定の詳細については、DataWedge の「インテント出力」ガイドを参照してください。
アプリケーションの実行
1.プロジェクト ページに進み、サンプル アプリをダウンロードし、ビルドして起動します。
2.初回起動時に、次のような画面が表示されます。
3.バーコードをスキャンすると、次の図のようなフィールドが表示されます。
ソース コード
メイン アクティビティ
以下は、サンプル アプリの MainActivity.java 用のソースコードです。
build.gradle およびその他のリソースについては、GitHub 上のアプリのプロジェクト ページを参照してください。
// // WARNING: This app is for demonstration purposes only. // It is not intended for use in a production environment // package com.darryncampbell.datawedgeintentexample1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { // // The section snippet below registers to receive the data broadcast from the // DataWedge intent output. In the example, a dynamic broadcast receiver is // registered in the onCreate() call of the target app. Notice that the filtered action // matches the "Intent action" specified in the DataWedge Intent Output configuration. // // For a production app, a more efficient way to the register and unregister the receiver // might be to use the onResume() and onPause() calls. // Note: If DataWedge had been configured to start an activity (instead of a broadcast), // the intent could be handled in the app's manifest by calling getIntent() in onCreate(). // If configured as startService, then a service must be created to receive the intent. // @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(); filter.addCategory(Intent.CATEGORY_DEFAULT); filter.addAction(getResources().getString(R.string.activity_intent_filter_action)); registerReceiver(myBroadcastReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(myBroadcastReceiver); } // // After registering the broadcast receiver, the next step (below) is to define it. // Here it's done in the MainActivity.java, but also can be handled by a separate class. // The logic of extracting the scanned data and displaying it on the screen // is executed in its own method (later in the code). Note the use of the // extra keys defined in the strings.xml file. // private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Bundle b = intent.getExtras(); // // The following is useful for debugging to verify // the format of received intents from DataWedge: // // for (String key : b.keySet()) // { // Log.v(LOG_TAG, key); // } // if (action.equals(getResources().getString(R.string.activity_intent_filter_action))) { // // Received a barcode scan // try { displayScanResult(intent, "via Broadcast"); } catch (Exception e) { // // Catch if the UI does not exist when broadcast is received // } } } }; // // The section below assumes that a UI exists in which to place the data. A production // application would be driving much of the behavior following a scan. // private void displayScanResult(Intent initiatingIntent, String howDataReceived) { String decodedSource = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_source)); String decodedData = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_data)); String decodedLabelType = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_label_type)); final TextView lblScanSource = (TextView) findViewById(R.id.lblScanSource); final TextView lblScanData = (TextView) findViewById(R.id.lblScanData); final TextView lblScanLabelType = (TextView) findViewById(R.id.lblScanDecoder); lblScanSource.setText(decodedSource + " " + howDataReceived); lblScanData.setText(decodedData); lblScanLabelType.setText(decodedLabelType); } }
Strings.xml
一部の文字列を事前定義することで、ターゲット アプリでスキャンしたデータを受信して抽出するプロセスを簡素化できます。受信時に、インテントのアクション (com.dwexample.ACTION) には、DataWedge の「インテント出力」ガイドで説明しているように、スキャン データのソース、タイプ、およびデータを指定したエクストラが含まれます。
<resources><string name="dw_action">com.dwexample.ACTION</string><string name="datawedge_intent_key_source">com.symbol.datawedge.source</string><string name="datawedge_intent_key_label_type">com.symbol.datawedge.label_type</string><string name="datawedge_intent_key_data">com.symbol.datawedge.data_string</string></resources>関連ガイド
- 元のチュートリアル | 著者: Zebra エンジニアの Darryn Campbell
- DataWedge の「インテント出力」ガイド | 使用時の設定および詳細について
- 「DataWedge API」 | インテントベースの関数について