Bluetooth LE Devices

Bluetooth LE devices use Bluetooth Low Energy (Bluetooth LE) to communicate with the host device (Android tablet or phone).

Discovering the Bluetooth LE Devices

The Device Manager drives the Bluetooth LE experience and communication with the Socket Mobile Bluetooth LE based devices. To receive the Device Manager subscribe to the DeviceManagerStateEvent.

DeviceManagerStateEvent event
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onCaptureDeviceManagerStateChange(DeviceManagerStateEvent event) {
    DeviceClient device = event.getDevice();
    DeviceState state = event.getState();

    mdeviceManager = device;

    /* If device manager is in ready state, get the current value for Favorite Bluetooth LE device */
    if(state.intValue() == DeviceState.READY) {
        getCurrentFavorite();
    }

}

The Device Manager has a property called “favorite” to configure and identify the Bluetooth LE devices that needs to be discovered. Favorite can be a single value or a semi-colon separated list. It can be a combination of any of the following values:

Favorites

Value

Description

*

Discover and connect to the first available Bluetooth LE devices

Unique Device Identifier

Identifier for a specific Bluetooth LE devices

“”

Empty for when the app is not interested in Bluetooth LE devices

Each value in the favorite list represents a Bluetooth LE device. To start the auto discovery for Bluetooth LE devices devices set favorite to a valid value. Auto discovery will stop if the number of devices represented in the favorite are connected.

If favorite is set to empty, auto discovery will not be triggered. Please note that setting favorite to empty, will not disconnect a already connected device. However if the device gets disconnected, then auto discovery will not start until favorite is set to a valid value.

Enable auto discovery
Property property=Property.create(Property.PROPERTY_FAVORITE,"*");
mdeviceManager = device.setProperty(property,propertyCallback);
Disable auto discovery
Property property=Property.create(Property.PROPERTY_FAVORITE,"");
mDeviceManager.setProperty(property, propertyCallback);

Implement the PropertyCallback to get the result of the setProperty.

Once a Bluetooth LE device is discovered, a connection is established. After this it can be used to read NFC cards or barcodes. To receive the state change of the device use the subscription to DeviceStateEvent

DeviceStateEvent event
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
       }
}

Setting Favorite Using Unique Device Identifier

Unique Device Identifier is generated by the service to identify the connection between the host device and the Bluetooth LE device. Setting the favorite to a Unique Device Identifier guarantees that the app will be connected to that specific Bluetooth LE device during consequent auto-discovery process. PROPERTY_UNIQUE_DEVICE_IDENTIFIER can be used to fetch the Unique Device Identifier. It takes Bluetooth LE device’s guid as an input.

Get unique identifier
Property property = Property.create(Property.PROPERTY_UNIQUE_DEVICE_IDENTIFIER, device.getDeviceGuid());
mDeviceManager.getProperty(property,propertyCallback);

The result is received in the PropertyCallback which can then be used to set the favorite.

Manual Discovery

This feature lists all the Bluetooth LE devices available in the vicinity by running the discovery process for a specified time. To trigger a manual discovery, set the PROPERTY_START_DISCOVERY along with the duration in milliseconds. Manual discovery runs for the specified duration and sends a DeviceDiscoveryEvent each time a Socket Mobile Bluetooth LE device is discovered. Discovery stops automatically once the duration in milliseconds has been reached.

Start manual discovery
int discoveryDuration = 10000;
Property property = Property.create(Property.START_DISCOVERY, discoveryDuration);
mDeviceManager.setProperty(property, propertyCallback);

Favorite should be empty before triggering a manual discovery, or error ESKT_FAVORITENOTEMPTY will be received in the propertyCallback. The BleDeviceManager Auto-Discovery should be stopped by setting its favorite property to an empty string prior starting a manual discovery.

To receive the list of the discovered devices use the subscription to DeviceDiscoveryEvent.

public void onCaptureDeviceDiscoveryChange(DeviceDiscoveryEvent event){
   DiscoveredDevice device=event.getDiscoveredDevice();
   if(device!=null){
       String uniqueDeviceIdentifier= device.getUniqueIdentifier()
   }
}

To receive the end of manual discovery use the subscription to DeviceDiscoveryCompleteEvent event. The manual discovery ends by sending the DeviceDiscoveryCompleteEvent notification. This allows to restart the discovery if no device has been discovered or simply to update the UI there is no discovery in progress.

Public void onCaptureDeviceDiscoveryChange(DeviceDiscoveryCompleteEvent event){
   if(event.isComplete()){
      // TODO your code here
   }
}

Reading Data

Once the connection is established successfully, data read from the device (NFC card. Smartphone wallet card, or Barcode) can be received by subscribing to the DataEvent

Public void onScan(DataEvent event){
     print(event.getData().getString());
}