Next Steps¶
Your app can only receive data from the scanner if your app and the scanner are both connected to the service and your app has ownership of the scanner.
All Socket Mobile scanners have an LED which displays the connection status of the scanner to the service, but your app is responsible for displaying the status of its connection to both the service and the scanner to the user.
Scanner Status¶
To receive device availability events, add a @Subscribe
annotated method that takes a
DeviceStateEvent
as an argument to any Activity
and it will be called when a scanner
becomes available or unavailable.
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onCaptureDeviceStateChange(DeviceStateEvent event) {
DeviceClient device = event.getDevice();
DeviceState state = event.getState();
switch(state.intValue()) {
case DeviceState.READY:
// Ready to use
break;
default:
// Device not ready for use
}
}
Note
You should not need to store the current state of the device if you subscribe to the
event with sticky = true
. When an activity is destroyed and recreated, it will receive the
last event containing the current state of the device.
Service Status¶
Add an @Subscribe
annotated method that takes a ConnectionStateEvent
as an argument
to any Activity
to be notified of changes to the state of the connection to the service.
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onCaptureServiceConnectionStateChange(ConnectionStateEvent event) {
ConnectionState state = event.getState();
CaptureClient client = event.getClient();
if (state.hasError()) {
CaptureError error = state.getError();
switch(error.getCode()) {
// ...
}
}
switch(state.intValue()) {
case ConnectionState.CONNECTING:
// ...
break;
case ConnectionState.CONNECTED:
// ...
break;
case ConnectionState.READY:
// ...
break;
case ConnectionState.DISCONNECTING:
// ...
break;
case ConnectionState.DISCONNECTED:
// ...
break;
}
}