Bluetooth Operations

Cordova ==> Bluetooth module

Pair

Some peripherals require pairing with the end user's phone before they can be read. These BluetoothPeripherals will have a requiresPairing value of true and will have PairingInstruction that can be used to display to the end user.

To pair a peripheral, pass its ID to the pair method:

const peripheralID = 3; // choose a peripheral

// Get peripheral object and log pairing instructions
const peripheral = await ValidicMobile.InformBluetooth.getPeripheral(peripheralID);
log(peripheral.pairingInstructions);

try {
    await ValidicMobile.InformBluetooth.pair(peripheral.id);
    log("Pairing completed.");
}
catch (error) {
    log("Pairing error: " + JSON.stringify(error));
}

Read

To read a peripheral, pass its ID to the read method:

const peripheralID = 3; // choose a peripheral

// Get peripheral object and log reading instructions
const peripheral = await ValidicMobile.InformBluetooth.getPeripheral(peripheralID);
log(peripheral.readingInstructions);

try {
    // Set autoSubmit to false so you can manually submit records
    const result = await ValidicMobile.InformBluetooth.read(peripheral.id);
}
catch (error) {
    log("Reading error: " + JSON.stringify(error));
}

Read With A Filter Applied

The read method accepts an optional options object to adjust behavior for reading. The available options are:

  • autoSubmit (boolean): Set to false to avoid automatically sending collected records to the Validic API. If omitted or true, records will be submitted automatically.
  • allowedDeviceIdentifiers (Array of strings): An array of device identifiers (Bluetooth MAC addresses for Android or UUIDs for iOS) that are permitted for the read operation. If the array is empty, then no identifiers are allowed. If not specified, the read operation will not restrict which devices can be read. Note that device identifiers are different between Android and iOS. iOS Bluetooth identifiers are only valid for the iOS device they are received on.

Example for use with iOS or Android:

const peripheralID = 3;

try {
    // Use an identifier from a pair or read operation result
    const pairResult = await ValidicMobile.InformBluetooth.pair(peripheralID);
    // For Android, this is a MAC addresses. For iOS, this is a device UUID.
    const identifier = pairResult.deviceIdentifier; // Android MAC address "AA:BB:CC:DD:EE:FF" or iOS UUID  "A1B2C3D4-E5F6-7890-1234-56789ABCDEF0"
    
    const result = await ValidicMobile.InformBluetooth.read(peripheralID, null, { 
        autoSubmit: false, // false to not automatically submit records after they have been collected. 
        allowedDeviceIdentifiers: [identifier] 
    });
    // Submit records manually
    if (result.informRecords && result.informRecords.length > 0) {
        for (const record of result.informRecords) {
            await ValidicMobile.InformCore.submitInformRecord(record);
        }
        log("All records submitted using InformCore.");
    } else {
        log("No records to submit.");
    }
}
catch (error) {
    log("Reading or submission error: " + JSON.stringify(error));
}

Events

Events can be listened for by attaching a listener to the document:

document.addEventListener(ValidicMobile.InformBluetooth.InformBluetoothReadyToReadEvent, onBluetoothReadyToRead);
  • InformBluetoothReadyToReadEvent: event fired when a Bluetooth device is ready to be read in the foreground.
  • PassiveReadyToReadEvent: event fired when a Bluetooth device is readu to be read passively.
  • PassiveDidReadEvent: event fired when a passive read completes. The event object will have an array of collected records in it.
  • PassiveDidFailEvent: event fired when a passive read fails. The event object will have error details in it.