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¶
Note
As of CaptureSDK 2.0, auto-discovery using ‘*’ as a Favorite and manual discovery using START_DISCOVERY for Bluetooth LE devices are no longer supported. Please follow the migration guide to adopt the updated APIs.
Add Device¶
The addDevice method allows you to programmatically discover nearby Socket Mobile devices. A DeviceDiscoveryEvent is sent for each discovered device.
byte deviceType = BluetoothDiscoveryMode.BLUETOOTH_LOW_ENERGY; // Specify the device type either BLUETOOTH_LOW_ENERGY or BLUETOOTH_CLASSIC
mCaptureClient.addDevice(deviceType, new PropertyCallback() {
@Override
public void onComplete(@Nullable CaptureError error, @Nullable Property property) {
if (error == null) {
// Device added successfully
} else {
// Handle error
}
}
});
To receive the list of the discovered devices use the subscription to DeviceDiscoveryEvent.
.. code-block:: java
public void onCaptureDeviceDiscoveryChange(DeviceDiscoveryEvent event){
DiscoveredDevice device=event.getDiscoveredDevice();
if(device!=null){
String uniqueDeviceIdentifier= device.getUniqueIdentifier()
}
}
Remove Device¶
The removeDevice method allows you to remove a device from CaptureSDK using its unique identifier (UUID).
mCaptureClient.removeDevice(uniqueDeviceIdentifier, new PropertyCallback() { // uniqueDeviceIdentifier from the result of DeviceDiscoveryEvent
@Override
public void onComplete(@Nullable CaptureError error, @Nullable Property property) {
if (error == null) {
// Device removed successfully
} else {
// Handle error
}
}
});
Connect to Discovered Device¶
After performing a discovery using addDevice and receiving discovered devices through DeviceDiscoveryEvent, you can connect to a specific device using its unique identifier.
mCaptureClient.connectDevice(deviceUniqueIdentifier, new PropertyCallback() {
@Override
public void onComplete(@Nullable CaptureError error, @Nullable Property property) {
if (error == null) {
// Connection initiated successfully
// Wait for DeviceStateEvent with READY state
} else {
// Handle connection error
}
}
});
Note
After calling connectDevice, subscribe to DeviceStateEvent to receive the device’s connection state changes.
Disconnect Discovered Device¶
You can disconnect a previously connected device using its unique identifier.
mCaptureClient.disconnectDevice(deviceUniqueIdentifier, new PropertyCallback() {
@Override
public void onComplete(@Nullable CaptureError error, @Nullable Property property) {
if (error == null) {
// Device disconnected successfully
} else {
// Handle disconnection error
}
}
});
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());
}
Create Product-Host Pair¶
The Lasso feature allows you to assign a Bluetooth LE product to a specific host, preventing other hosts from establishing a connection to that product once it is assigned. The Lasso feature ensures that a product will only accept a connection from a host that presents a matching Lasso ID, which serves as an authorization identifier for the product-host pair.
The Lasso feature can be enabled or disabled on an supporting product in two ways:
By using a master card or command barcode.
By calling the appropriate methods in the CaptureSDK API.
Use the following commands to enable or disable the Lasso feature: java
device.setLassoStatus(LassoStatus.ENABLE, propertyCallback);
device.setLassoStatus(LassoStatus.DISABLE, propertyCallback);
Ensure that the PropertyCallback is implemented to handle the result of the setLassoStatus method.
The Lasso ID is unique to each product-host pair. When the product is discovered by a host, the CaptureSDK automatically generates a unique Lasso ID for that pair. You can also override the automatically generated Lasso ID with a custom one. If the application is connected to the product using the automatically generated Lasso ID, it is possible to change the Lasso ID. When a new Lasso ID is set, it must be used for all subsequent connections in order for the product to accept the connection. Here’s an example of setting a custom Lasso ID: java
device.setLassoId(UUID.fromString("550e8400-e29b-41d4-a716-446655440000"), propertyCallback);
The Lasso ID lifespan defines how long the product-host pairing will remain active. After the lifespan expires, the Lasso ID will no longer be valid, and the product will be available for connection by other hosts.
Default lifespan: By default, the Lasso ID never expires after being set. The lifespan is set to 0, meaning the product will remain paired with the host indefinitely unless the Lasso ID is changed.
Changing the Lasso ID: If a host sends a new Lasso ID to replace the current one, the lifespan resets and starts anew from that point.
To set a Lasso ID lifespan (for example, 1 minute), use the following command: java
device.setLassoLifeSpan(60, propertyCallback); // 60 seconds