Event Listeners with custom cordova plugin in Ionic


#1

I’m using a custom plugin that was developed to integrate Validic with Cordova. I’ve created a service file have been able to pull some functionality from the plugin but need some help with the event listeners. My main issue is getting the event listeners to work with Ionic.

    declare var ValidicMobile;
    export class ValidicService {
  
    startSession() {
      ValidicMobile.Session.isSessionActive(function (response) {
        if (!response.isActive) {
          // this works
          ValidicMobile.Session.startSession('abc123', 'abc123', 'abc123');
          console.log("Validic isSessionActive " + response.isActive);
        }
        alert("Validic isSessionActive " + response.isActive);
      });
    
    ValidicMobile.BLE.setPassiveReadPeripheralIDs([14]); 
    // Need help getting these event listeners to work.
    //window.addEventListener(ValidicMobile.BLE.EventName.ON_PASSIVE_DID_READ, this.onBLEPassiveDidRead);
    //window.addEventListener(ValidicMobile.BLE.EventName.ON_PASSIVE_DID_FAIL, this.onBLEPassiveDidFail;
    
    }   
  
     readDevice(device_id) { 
      // this works
      alert("reading device " + device_id);
      ValidicMobile.BLE.read(device_id, (function (response) {
        alert(JSON.stringify(response.payload));
      }), (function (response) {
        alert('read fail: ' + response.payload.error);
      }));
     }
  
    readSuccess(response) {
      alert(JSON.stringify(response.payload)) 
      alert(response.payload.records[0].weight + "kg")
    }
  
    readFail(response) {
      alert('read fail: ' + response.payload.error)
    }
    
    supportedDevices() {
      // this works
      console.log("supportedDevices begin ");
      alert("supportedDevices begin");
  
      ValidicMobile.BLE.getSupportedPeripherals().then(response => {
        console.log("supportedDevices follows");
        console.log(response.peripherals);
      });
    
      ValidicMobile.BLE.getSupportedPeripherals(function (response) {
        //this works
        alert("Supported devices count: " + response.peripherals.length);
      });
    
      ValidicMobile.BLE.getPeripheralsOfType(ValidicMobile.PeripheralType.WEIGHT_SCALE, function (response) {
        //this works
        alert(response.peripherals.map(function(item){return item.Manufacturer + ": " + item.Model + " ID: " + item.ID}));
      }, function (err) {
        alert(err.error);
      });
    
    }
  
    pairDevice(device_id) {
    
      ValidicMobile.BLE.getPeripheral(device_id, function (response) {
          alert(response.peripheral.Model + " need pairing? " + response.peripheral.RequiresPairing);
          alert(response.peripheral.PairingInstruction);
          if (response.peripheral.RequiresPairing) {
            ValidicMobile.BLE.pair(14, (function (response) { alert('pair success') }), (function (response) { alert('pair fail: ' + response.payload.error)}));
          }
      }, function (err) {
          alert(err.error);
      });
    
    }   
  
    onBLEPassiveDidRead(event) {
      alert("onBLEPassiveDidRead");
    }
  
    onBLEPassiveDidFail(event) {
      alert("onBLEPassiveDidFail");
    }
      
    }