通过NPM下载插件
$ npm install cmbsdk-react-native --save
主要是自动安装
$ react-native link cmbsdk-react-native
Import the component in your react-native app by adding this
从'cmbsdk-reacter-native'导入{cmbreader,cmb};
You can access all of the API methods through thecmbconstant, and all of the enums and constants are available in the CMBReader class.
Before continuing, download the cmbSDK React-Native zip file from our download section, and see the API reference.
Download page:https://cmbdn.cognex.com/download
API reference:https://cmbdn.cognex.com/knowledge/react-nat/rct-api-metho
Make sure you have downloaded the cmbSDK React-Native zip archive from our download page. From the downloaded zip file, open the iOS directory.
1. If you are using the mobile device's built in camera, do the same with the "MWBScannerImages.xcassets" file located in the iOS/Resources directory.
2. In your projects info.plist file you need to add a key depending on the readerDevice type that you are using.
就是这样。您应该能够使用CMBSDK React-Native模块工作来运行应用程序。
Make sure you have downloaded the cmbSDK React-Native zip archive from our download page. From the downloaded zip file, open the Android/cmbsdk-android-binary directory.
1. Open theyourRNApp/android/app/directory, create a new folder named "libs" if there isn't one already, and open it. Place the file "cmbsdklib-release.aar" inside the libs folder. This aar can be found in "Android/cmbsdk-android-binary" from the downloaded zip file
2. Open the app build.gradle located inyourRNApp/android/appand add these lines:
- Above the dependencies add this:
repositories { flatDir { dirs 'libs' } }
3.打开构建。yourRNApp/androidand change the minSdkVersion to 19 or above
就是这样。您应该能够使用CMBSDK React-Native模块工作来运行应用程序。
To get a scanner up and running, the first thing to do, is to call theloadScanner()方法。它期望cmbreader.device_type param。此方法不连接到阅读器设备。我们需要打电话连接()在回调中实际连接到读取器设备
cmb.loadScanner(cmbreader.device_type.mxreader).then((wendesp)=> {cmb.getAvailability()。然后(response)=> {if(wif(wif(wif)= = = = = = = = = = = = = = = = = =>})。catch(((ounders)=> {})});
/ * @return(Promise){状态:布尔值,如果连接成功如果不是false erry:string,则如果状态false false erry不会为null} */
The result from the connect() method is returned as a Promise and it will contain the result of the connection attempt:
cmb.connect().then((connectMethodResult) => { // do something after a connection has been established configureReader(); }).catch((failure) => { console.log("CMB - connectReader failed: "+JSON.stringify(failure)) });
有一个事件监听器的连接属性us of the ReaderDevice, namely theCMBReader.EVENT.ConnectionStateChangedevent which is explained in more detail below.
/* @return (promise) { status : boolean, if disconnect succeeded true if not false err : string , if status false err will not be null } */
Just as there is连接(),,,,there is adisconnect()method that does the opposite of连接():
cmb.disconnect();
Similarly to连接(),,,,disconnect()also triggers theCMBReader.EVENT.ConnectionStateChangedevent.
/* @return Promise (bool) value of the Scanner Activity (true if the command was successful, false otherwise ex: if readerDevice not initialized) */
To start / stop the scanning process, we use these methods. They return a promise, which will be resolved if the command was successful (the scanning has started or stopped) or rejected otherwise (if there is no active ReaderDevice initialized or isn't connected).
启动扫描仪并扫描条形码后,扫描结果触发了cmbreader.event.ReadResultreceivedevent.
Once there is a connection to the Reader, we can enable symbologies by callingsetSymbologyEnabled()。It expects three params: a CMBReader.SYMBOLOGY which is the symbology to be enabled or disabled, a boolean for ON/OFF, and a String for the commandID for handling the command result.
cmb.setsymbology(cmbreader.symbology.qr,true,cmbreader.symbology_name.qr);
This method triggers theCMBReader.EVENT.CommandCompletedevent, whose result contains thecommandIDstring from the third parameter, so that you know which commands have succeeded and which have failed in the event result.
To check if we have a symbol enabled, we useIssymbology nobable()。It takes two arguments: the CMBReader.SYMBOLOGY that we are checking, and acommandIDstring used for identifying the response. The result triggers theCMBReader.EVENT.CommandCompleted事件,其中包含commandIDstring from the second parameter, so that you know which commands have succeeded and which have failed in the event result.
cmb.isSymbologyEnabled(CMBReader.SYMBOLOGY.QR, CMBReader.SYMBOLOGY_NAME.QR);
/ * @return(promise){状态:布尔值,如果成功执行命令err:string,则为true,如果状态false false不会为null} */
如果我们想启用闪光灯,我们可以使用setlightson()。It expects one argument boolean and returns a promise.
/* @return (promise) { status : boolean, true if lights are on, false otherwise err : string , in case of error (e.g. reader not initialized) } */
We can check the lights status with isLightsOn(), which returns a promise.
/** @return A promise that resolves with the CMBReader.CONNECTION_STATE value of the current reader device */
If you need to get the current connection state,getConnectionState()can be used
cmb.getConnectionState()。然后(function(connectionState){if(connectionState == cmbreader.connection_state.connection){// reader已连接}});
This should be used only when using the device's built in camera for scanning (CMBReader.DEVICE_TYPE.Camera).
设置相机将如何表现当我们使用了RA device as a barcode reader, we use setCameraMode(). It takes a CMBReader.CAMERA_MODE argument.
cmb.setcameramode(cmbreader.camera_mode) /**使用没有瞄准器的相机。预览已经开始,可以使用照明。cmbreader.camera_mode.noaimer = 0,将相机与基本瞄准器(例如Stingray)一起使用。预览已经关闭,没有照明。cmbreader.camera_mode.passiveaimer = 1,将摄像头与活动瞄准器(例如MX-100)一起使用。预览已经关闭,可以使用照明。cmbreader.camera_mode.activeaimer = 2,使用移动设备前置摄像头。预览已经开始,没有照明。cmbreader.camera_mode.frontcamera = 3 */
Note: CameraMode should be set BEFORE we callloadScanner()for it to take effect.
This should be used only when using the device's built in camera for scanning (CMBReader.DEVICE_TYPE.Camera).
This function expects one integer argument that is a result of the OR-ed result of all the preview options that we want enabled.
cmb.setPreviewOptions(CMBReader.CAMERA_PREVIEW_OPTION.NoZoomBtn | CMBReader.CAMERA_PREVIEW_OPTION.NoIllumBtn);
注意:应在我们致电之前设置预览loadScanner()for it to take effect.
This should be used only when using the device's built in camera for scanning (CMBReader.DEVICE_TYPE.Camera).
cmb.setpreviewContainerPositionandSize([0,0,100,50]);//will set the preview to 0,0 and 100% width 50% height
This should be used only when using the device's built in camera for scanning (CMBReader.DEVICE_TYPE.Camera).
将相机预览设置为全屏而不是部分视图。
cmb.setPreviewContainerFullScreen();
This should be used only when using the device's built in camera for scanning (CMBReader.DEVICE_TYPE.Camera).
Available only on iOS.
设置摄像头预览部分视图顶轴以启动与Android行为相匹配的状态栏。它期望有一个布尔论证。
cmb.setpreviewContainerBelowStatusBar(true);cmb.setpreviewContainerPositionandSize([0,0,100,50]);//将预览设置为0,0和100%宽度50%高度。//在iOS上,部分视图将显示在状态栏下方。
cmb.setPreviewContainerBelowStatusBar(false); cmb.setPreviewContainerPositionAndSize([0,0,100,50]); //will set the preview to 0,0 and 100% width 50% height. //On iOS the partial view will start from the top of the screen, and will overlap the status bar.
Used to enable / disable image result type. Expects one boolean argument.
cmb.enableImage(true);
Used to enable / disable svg result type. Expects one boolean argument.
cmb.enableImageGraphics(true);
启用或禁用解析扫描条形码。期望一个类型cmbreader.result_parser的参数。
cmb.setParser(CMBReader.RESULT_PARSER.GS1);
Set encoding for the readString result type. Expects one argument of type CMBReader.READSTRING_ENCODING.
cmb.setReadStringencoding(cmbreader.readstring_encoding.utf_8);
/* @return (promise) { status : boolean, true if reset was successful, false otherwise err : string , in case of error (e.g. reader not initialized) } */
To reset the configuration options we can useresetConfig.
cmb.resetConfig(function(result){ console.log(result); })
/* @return (promise) { value : int err : string , in case of error (e.g. reader not initialized) } */
显示连接设备的电池电量的方法。不采取任何争论。
Available only on iOS.
Used for creating authentication credentials used for MDM reporting. It takes four string arguments: username, password, clientID and clientSecret.
应在setMdMreportingEnabled之前调用。
More on the MDM Reporting can be foundhere
cmb.createMDMAuthCredentials("username", "password", "clientID", "clientSecret");
Available only on iOS.
A company owning and operating many Cognex Mobile Terminals may want to remotely collect up-to-date information about battery level, battery health, installed firmware, etc.
An iOS application using the cmbSDK framework can report status information of the attached Mobile Terminal to an MDM instance. This can be enabled with the setMDMReportingEnabled method that accepts one boolean argument.
More on the MDM Reporting can be foundhere
cmb.setMDMReportingEnabled(true);
The React native cmbSDK module emits Events that can be used in the js application.
These should be added in the componentDidMount function, and removed in componentWillUnmount (seeReact component lifecycle).
First, create the event emitter:
import NativeModules.NativeEventEmitter; const scannerListener = new NativeEventEmitter(cmb);
and then add listeners for each event you want to handle:
scannerListener.addListener( CMBReader.EVENT.ReadResultReceived, (result) => { if (result.goodRead == true){ Alert.alert( result.symbologyString, result.readString ); } } );
Here are all the events that the cmbSDK module can emit:
cmbreader.event.ReadResultreceivedCMBReader.EVENT.AvailabilityChanged CMBReader.EVENT.ConnectionStateChanged CMBReader.EVENT.ScanningStateChanged CMBReader.EVENT.CommandCompleted
CommandID(字符串,用于发送命令的param)eventType(字符串,ex:issymbologyEnabled)命令(字符串,已发送的命令)success(boolean)status(nullable,int,int,命令状态(请参阅cdmresponse.h))消息(无效,字符串,命令有效载荷)图像(nullable,base64扫描映像的字符串表示)响应(可nullable,boolean,boolean,命令响应,例如:issymbologyEnabled将返回true/true/false true/false在此处)