.. title:: Java Getting Started Getting Started on Java ======================= Add the SDK to your project --------------------------- .. code-block:: groovy :caption: build.gradle :emphasize-lines: 3-5,9 repositories { mavenCentral() maven { url "https://bin.socketmobile.com/repo/releases" } } dependencies { implementation 'com.socketmobile:capture:1.6.3' } .. NOTE:: Beta versions of the sdk will be distributed in https://bin.socketmobile.com/repo/snapshots Start Capture ------------- Create a singleton instance of :java:ref:`CaptureClient` at the highest appropriate scope for your application. .. code-block:: java // Note: The second argument, , is the colon separated platform and app id you used // to generate your appkey - e.g. android:com.example.myapp AppKey appkey = new AppKey("", "", ""); CaptureClient client = new CaptureClient(appkey); client.setListener(mListener); client.connect(connectionCallback); Where mListener implements the :java:ref:`CaptureClient.Listener` interface and connectionCallback implements the :java:ref:`ConnectionCallback` interface. .. code-block:: java ConnectionCallback connectionCallback = new ConnectionCallback() { @override public void onConnectionStateChanged(ConnectionState state) { switch(state.get()) { case ConnectionState.CONNECTING: // do something or nothing break; case ConnectionState.CONNECTED: // client is now usable break; case ConnectionState.DISCONNECTING: // only called when shutting down gracefully break; case ConnectionState.DISCONNECTED: if(state.disconnectCausedByError()) { // Handle error } else { // Shut down normally } default: // Unreachable break; } } } The listener will be notified as Capture devices become usable or unusable. .. code-block:: java @Override public void onDeviceStateEvent(DeviceStateEvent event) { mDevice = event.getDevice(); DeviceState state = event.getState() switch (state.intValue()) { case DeviceState.GONE: // Scanner is gone break; case DeviceState.AVAILABLE: // Scanner is connected to the service. You can choose to open the device or not. break; case DeviceState.OPEN: // Scanner is open, but you do not have control of it. It may be in the process of // opening or another application may have opened the scanner. break; case DeviceState.READY: // Scanner is ready. Configure scanner break; } } Most importantly, the listener will also be notified of decoded data .. code-block:: java public void onData(DataEvent event) { DeviceClient device = event.getDevice(); String data = event.getData().getString(); // do something }