Getting Started

The Android Capture client aims to make it as simple as possible to add scanning functionality into an Android application. It hides the complexity of the Android activity lifecycle by hooking into it. If you use a non-standard application architecture or just want more explicit control over the client and it’s lifecycle, please refer to the Java docs and create and manage an instance of CaptureClient directly.

Add the SDK to your project

app/build.gradle
repositories {
    jcenter()
    maven {
        url "https://bin.socketmobile.com/repo/snapshots"
    }
}

dependencies {
    implementation 'com.socketmobile:capture-android:1.0.3'
}

Provide your appkey

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="..." >
  <application android:label="..." >

    <meta-data
        android:name="com.socketmobile.capture.APP_KEY"
        android:value="..."/>
    <meta-data
        android:name="com.socketmobile.capture.DEVELOPER_ID"
        android:value="..."/>

  </application>
</manifest>

Start Capture

You can set up capture in the onCreate method of either your Application or initial Activity

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.socketmobile.capture.android.Capture;

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main)
      Capture.builder(getApplicationContext())
        .enableLogging(BuildConfig.DEBUG)
        .build();
  }
}

Targeting Android 28+?

Apps that target Android 28+ must also enable cleartext traffic to localhost. If you do not already have a network security configuration file, you will need to create one.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="..." >
  <application android:label="..."
      android:networkSecurityConfig="@xml/network_security_config"
      ...>

  </application>
</manifest>
res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <base-config cleartextTrafficPermitted="false" />
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="false">localhost</domain>
    <domain includeSubdomains="false">127.0.0.1</domain>
  </domain-config>
</network-security-config>

Subscribe to Capture Events

Just add a @Subscribe annotated method that takes a DataEvent as an argument to any Activity and Capture will automatically register your Activity to receive notifications when a barcode is scanned.

@Subscribe(threadMode = ThreadMode.MAIN)
public onData(DataEvent event) {
    DeviceClient device = event.getDevice();
    String data = event.getData().getString();
    // Do something
}

Tip

Using ThreadMode.MAIN allows you to modify your UI in onData without switching threads - e.g. to display the decoded data. If you do not need to modify your UI in onData, consider using ThreadMode.ASYNC instead. For more info, see EventBus - Delivery Threads