SocketMobile / capturebasic-cordova-ios

Very basic Cordova plugin working with Capture
Other
0 stars 0 forks source link

Adding to Basic Cordova App #2

Open eddinsw opened 2 years ago

eddinsw commented 2 years ago

I am attempting to add this to basic Cordova app as POC to make sure I understand how it work. I create a new project and this is the only plugin added. I modified the plugin.xml to pull in the new SDK locations.

<?xml version='1.0' encoding='utf-8'?>
<plugin
  id="com-socketmobile-capturebasic-cordova"
  version="0.1.0"
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <name>CaptureBasicCordova</name>
  <description>
    This version of Capture for Cordova, provides only the device presence events and decoded data
  </description>
  <author>
    Socket Mobile, Inc.
  </author>
  <keywords>barcode,scanner</keywords>
  <license>Apache 2.0 License</license>
  <js-module name="CaptureBasicCordova" src="www/CaptureBasicCordova.js">
    <clobbers target="CaptureBasic" />
  </js-module>
  <platform name="ios">
    <config-file parent="/*" target="config.xml">
      <feature name="CaptureBasicCordova">
        <param name="ios-package" value="CaptureBasicCordova" />
      </feature>
    </config-file>
    <config-file target="*-Info.plist" parent="UISupportedExternalAccessoryProtocols">
      <key>UISupportedExternalAccessoryProtocols</key>
      <array>
        <string>com.socketmobile.chs</string>
      </array>
    </config-file>
    <source-file src="src/ios/CaptureBasicCordova.m" />
    <source-file src="src/ios/sdk/SktCaptureHelper.m" />
    <header-file src="src/ios/sdk/SktCaptureHelper.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SKTCapture.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureDataSource.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureDeviceTypes.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureErrors.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureEvent.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureProperty.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCapturePropertyIds.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureVersion.h" />
    <source-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_armv7/libCapture.a" framework="true"/>
    <resource-file src="src/ios/sdk/socketmobilepublickey.pem"/>
    <resource-file src="src/ios/sdk/softScanBeep.wav"/>
    <framework src="ExternalAccessory.framework" />
    <framework src="AudioToolbox.framework" />
    <framework src="AVFoundation.framework" />
    <framework src="CoreBluetooth.framework" />
    <framework src="libc++.a" />
    <framework src="libicucore.a" />
  </platform>
</plugin>

Here is how I am trying to implement. I have excluded App info.

document.addEventListener('deviceready', onDeviceReady, false);

var CaptureBasic;

function onDeviceReady() {
    // Cordova is now initialized. Have fun!

    console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
    document.getElementById('deviceready').classList.add('ready');

    CaptureBasic.addListener('notifications', (success)=> { 
        alert('first list' + success); 
        SetupCapture();
    }, (error)=>{
        alert('first list' + error);
    });

}

function SetupCapture(){
    const appInfo = {
        appId: '',
        developerId: '',
        appKey: ''
      };

       CaptureBasic.addListener('notifications', (success)=> { 
        alert('first list' + success); 
        SetupCapture();
    }, (error)=>{
        alert('first list' + error);
    });

}

function AddCaptureListener(){
    CaptureBasic.addListener('notifications', (success)=>{
        const notification = JSON.parse(success);
        if (notification.name === 'initializeComplete') {
            alert('Capture initialization completed');
          console.log('Capture initialization completed');
        }
        else if (notification.name === 'deviceArrival'){
            alert('device arrival: ' + notification.deviceName);
          console.log('device arrival: ', notification.deviceName);
        }
        else if (notification.name === 'deviceRemoval'){
            alert('no device connected');
          console.log('no device connected');
        }
        else if (notification.name === 'decodedData') {
          const decodedData = notification.decodedData.map(c => String.fromCharCode(c)).join('');
          document.getElementById('barcode').innerHTML = decodedData;
          alert(decodedData);
          console.log('decodedData: ', decodedData);
        }
      },(error)=>{
        console.log('notification error: ', error);
      });
}

I am not hitting any of the alerts but the device onReady is getting called. Do I need to add the NSBluetoothAlwaysUsageDescription when just debugging on an Device?

EricGlaenzer commented 2 years ago

yes without it would crash I believe and you probably need to add the external accessory protocol string com.socketmobile.chs into your info.plist file too.

eddinsw commented 2 years ago

I got it working. I added NSBluetoothAlwaysUsageDescription using the plugin.xml as well.

<?xml version='1.0' encoding='utf-8'?>
<plugin
  id="com-socketmobile-capturebasic-cordova"
  version="0.1.0"
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <name>CaptureBasicCordova</name>
  <description>
    This version of Capture for Cordova, provides only the device presence events and decoded data
  </description>
  <author>
    Socket Mobile, Inc.
  </author>
  <keywords>barcode,scanner</keywords>
  <license>Apache 2.0 License</license>
  <js-module name="CaptureBasicCordova" src="www/CaptureBasicCordova.js">
    <clobbers target="CaptureBasic" />
  </js-module>
  <platform name="ios">
    <config-file parent="/*" target="config.xml">
      <feature name="CaptureBasicCordova">
        <param name="ios-package" value="CaptureBasicCordova" />
      </feature>
    </config-file>
    <config-file target="*-Info.plist" parent="UISupportedExternalAccessoryProtocols">
      <key>UISupportedExternalAccessoryProtocols</key>
      <array>
        <string>com.socketmobile.chs</string>
      </array>
    </config-file>
    <config-file target="*-Info.plist" parent="NSBluetoothAlwaysUsageDescription">
      <key>NSBluetoothAlwaysUsageDescription</key>
      <string>Bluetooth Accessed Needed for Socket Mobile Scanner</string>
    </config-file>
    <source-file src="src/ios/CaptureBasicCordova.m" />
    <source-file src="src/ios/sdk/SktCaptureHelper.m" />
    <header-file src="src/ios/sdk/SktCaptureHelper.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SKTCapture.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureDataSource.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureDeviceTypes.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureErrors.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureEvent.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureProperty.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCapturePropertyIds.h" />
    <header-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_i386_x86_64-simulator/Headers/SktCaptureVersion.h" />
    <source-file src="src/ios/sdk/lib/SKTCapture.xcframework/ios-arm64_armv7/libCapture.a" framework="true"/>
    <resource-file src="src/ios/sdk/socketmobilepublickey.pem"/>
    <resource-file src="src/ios/sdk/softScanBeep.wav"/>
    <framework src="ExternalAccessory.framework" />
    <framework src="AudioToolbox.framework" />
    <framework src="AVFoundation.framework" />
    <framework src="CoreBluetooth.framework" />
    <framework src="libc++.a" />
    <framework src="libicucore.a" />
  </platform>
</plugin>

I also was incorrectly using the addListener. Below is the javascript file for the codova side.

document.addEventListener('deviceready', onDeviceReady, false);

var CaptureBasic;

function onDeviceReady() {
    // Cordova is now initialized. Have fun!

    console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
    document.getElementById('deviceready').classList.add('ready');

    addCaptureListener();
}

function addCaptureListener() {

  CaptureBasic.addListener('notifications', function (success) {
      var notification = JSON.parse(success);
      if (notification.name === 'initializeComplete') {
        document.getElementById('barcode').innerHTML = 'Initialized';
          console.log('Capture initialization completed');
      }
      else if (notification.name === 'deviceArrival') {
        document.getElementById('barcode').innerHTML = notification.deviceName;
          console.log('device arrival: ', notification.deviceName);
      }
      else if (notification.name === 'deviceRemoval') {
          console.log('no device connected');
      }
      else if (notification.name === 'decodedData') {
          var decodedData = notification.decodedData.map(function (c) { return String.fromCharCode(c); }).join('');
          document.getElementById('barcode').innerHTML = decodedData;
          console.log('decodedData: ', decodedData);
      }
  }, function (error) {
      console.log('notification error: ', error);
  });

  var appInfo = {
      appId: '',
      developerId: '',
      appKey: ''
  };

  CaptureBasic.useCaptureBasic(appInfo, function (success) {
      console.log('useCaptureBasic returns: ', success);
  }, function (error) {
      console.log('useCaptureBasic returns an error: ', error);
  });

}