To implement cmbSDK Toolkit in your application, please follow these instructions. Note that cmbSDK Toolkit depends on cmbSDK and you should always import cmbSDK in your project to use the cmbSDK Toolkit.
In the Source directory field browse the cmbsdktoolkit directory that contains the build.gradle file and the cmbsdktoolkit.aar file inside. In the Module name field you should see :cmbsdktoolkit, and click Finish. In the same way import cmbSDK.
After the new modules are available, right-click your app module, select the Open Module Settings, and choose the Dependencies tab.
import cmbSDK
import cmbSDKToolkit
cmbSDK Toolkit contains a Dynamic Symbology Settings module that can be used to configure Cognex readers and the camera API through a symbology settings screen in your application. If you want to see how it works in live, I would suggest to try one of the Cognex mobile applications (Quick Setup, Cognex Browser, Cognex Keyboard).
The module displays all known barcode type by connected reader. If user turns on the symbology, a new gear icon is displayed where further sub-symbology types (if applicable) and symbology dependent settings are available.
There are checks implemented to prevent that all sub-symbology types are turned off.
Certain symbology types have Standard as sub-symbology type, that means the main symbol type and cannot be turned off (disabled).
parentFragmentManager.beginTransaction()
.replace(
R.id.fragment_holder,
SymbologySettingsFragment.newInstance(readerDevice),
SymbologySettingsFragment.TAG
)
.addToBackStack(SymbologySettingsFragment.TAG)
.commit()
If the reader device is not connected or disconnected while this fragment is shown, symbology settings fragment will be closed and removed from the back stack with warning message.
The symbology settings module is available as a UIViewController instance that has to be instantiated as the following code snippet demonstrates.
let symbologySettingsVC = SymbologySettingsTVC(dataManSystem: self.datamanSystem, style: UITableView.Style.grouped)
Please note that you have to pass a CDMDataManSystem
object to let the module communicate with the reader. If you are using the high level CMBReaderDevice
class you can get the underlying CDMDataManSystem
object by calling the dataManSystem
instance method on your CMBReaderDevice
object.
cmbSDK Toolkit contains a Pairing module that makes the implementation easy to connect to Bluetooth Direct Connect enabled Cognex readers. If you want to see how it works in live, I would suggest to try one of the Cognex mobile applications (Quick Setup, Cognex Browser, Cognex Keyboard).
There are several possibilities within the Pairing module to establish a connection with a BT enabled Cognex device:
- One-Step Pairing: This is the easiest approach, as the user needs to read only a DataMatrix code from the phone's screen and after accepting the OS Pairing request information, the reader and the mobile device are paired and connected to each other.
- Advanced Mode: If the above method does not work for some reason, Pairing modul provides an alternative way to establish the connection. In this case user needs to put the reader into discoverable mode (either with a data matrix code or following the steps shown on the mobile device and executing them on the reader's OLED screen) and select the reader to connect to.
First we need to add required bluetooth permissions in the application manifest file:
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30"
tools:ignore="CoarseFineLocation" />
<!---->
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Inside the cmbsdktoolkit module, bluetooth pairing functionality is implemented as fragment that can be attached from any fragment manager.
This fragment has two optional input parameters:
parentFragmentManager.beginTransaction()
.replace(
R.id.fragment_holder,
BluetoothPairingFragment.newInstance(),
BluetoothPairingFragment.TAG
)
.addToBackStack(BluetoothPairingFragment.TAG)
.commit()
The parent activity need to implement BluetoothPairingFragmentListener:
class MainActivity : AppCompatActivity(), BluetoothPairingFragmentListener
override fun onBluetoothDevicePaired(device: BluetoothDevice) {
// In BluetoothPairingFragment as input param we've set connectAfterPairing to true, which means
// that connection to the bluetooth device will be performed automatically after successful pairing.
// If we set that to false, from here we can start the connection process
}
override fun onBluetoothDeviceConnectionFailed(error: Throwable) {
Snackbar.make(
findViewById(R.id.fragment_holder),
getString(R.string.bluetooth_pairing_error, error.localizedMessage),
Snackbar.LENGTH_LONG
).show()
}
override fun onBluetoothReaderDeviceConnected(reader: BluetoothReaderDevice) {
reader.setReaderDeviceListener(this)
readerDevice = reader
clearAllFragments()
configureReaderDevice()
}
NotificationCenter.default.addObserver(self, selector: #selector(handlePeripheralDidPairNotification(_ :)), name: .cognexBTDeviceDidPairNotification, object: nil)
@objc func handlePeripheralDidPairNotification(_ notification: NSNotification) {
let peripheralUUID = notification.userInfo![pairedCognexBTDeviceUUIDKey] as! UUID
// Save peripheralUUID...
// Pop the presented pairing viewcontroller
}
let settingsStoryBoard = UIStoryboard(name: "cmbSDKToolkit", bundle: Bundle(identifier: "com.cognex.cmbSDKToolkit"))
let btPairingVC = settingsStoryBoard.instantiateViewController(withIdentifier: "BT_AUTO_CONNECT_VC") as! BTAutoConnect
btPairingVC.peripheralUUID = // load previously paired device's UUID or use nil
let navController = UINavigationController(rootViewController: btPairingVC as! UIViewController)