首次提交

This commit is contained in:
DESKTOP-E478071\lzj
2020-01-06 10:44:45 +08:00
parent 8706cfc223
commit 5961d4734e
13 changed files with 420 additions and 2 deletions

View File

@@ -1,2 +1,66 @@
# RFID
乐亭rfid读取EPC
#### 乐亭项目RFID读取
**安装方式**
`cordova plugin add `
**相关方法**
- openBlueTooth
- 参数
- success 成功回调
- error 失败回调
- 返回值
- success => 蓝牙开启成功
- error => 蓝牙开启失败
- 调用示例
```
rfid.openBlueTooth(success,error);
```
- getBlueToothList
- 参数
- success 成功回调
- error 失败回调
- 返回值
- success => { message: "", data:[ 蓝牙设备list ] }
- error => errorMsg
- 调用示例
```
rfid.getBlueToothList(success,error);
```
- connectBlueTooth
- 参数
- success 成功回调
- error 失败回调
- address 待链接蓝牙地址
- 返回值
- success => 模块打开成功
- error => 模块打开失败
- 调用示例
```
rfid.connectBlueTooth(success,error,{address: address});
```
- read
- 参数
- success 成功回调
- error 失败回调
- 返回值
- success => { message: "", data:[ EPClist ] }
- error => errorMsg
- 调用示例
```
rfid.read(success,error);
```
- write
- 参数
- success 成功回调
- error 失败回调
- epc_number 待写入EPC值
- epc_len EPC编码方式16进制 **选填默认0c**
- 返回值
- success => 写入成功
- error => errorMsg
- 调用示例
```
rfid.write(success,error, {epc_number: $scope.input.epc_number});
```

17
package.json Normal file
View File

@@ -0,0 +1,17 @@
{
"name": "rfid",
"version": "1.0.0",
"description": "rfid",
"cordova": {
"id": "rfid",
"platforms": [
"android"
]
},
"keywords": [
"ecosystem:cordova",
"cordova-android"
],
"author": "shuto",
"license": "n"
}

35
plugin.xml Normal file
View File

@@ -0,0 +1,35 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="rfid" version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>rfid</name>
<js-module name="rfid" src="www/rfid.js">
<clobbers target="rfid" />
</js-module>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="rfid">
<param name="android-package" value="cn.shuto.rfid.RfidPlugin"/>
<param name="onload" value="true" />
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</config-file>
<!--lib-->
<source-file src="src/android/libs/SZForeRFID.jar" target-dir="libs"/>
<source-file src="src/android/libs/arm64-v8a/libszfore.so" target-dir="libs/arm64-v8a"/>
<source-file src="src/android/libs/armeabi/libszfore.so" target-dir="libs/armeabi"/>
<source-file src="src/android/libs/armeabi-v7a/libszfore.so" target-dir="libs/armeabi-v7a"/>
<source-file src="src/android/libs/mips/libszfore.so" target-dir="libs/mips"/>
<source-file src="src/android/libs/mips64/libszfore.so" target-dir="libs/mips64"/>
<source-file src="src/android/libs/x86/libszfore.so" target-dir="libs/x86"/>
<source-file src="src/android/libs/x86_64/libszfore.so" target-dir="libs/x86_64"/>
<!--src-->
<source-file src="src/android/cn/shuto/rfid/RfidPlugin.java" target-dir="src/cn/shuto/rfid"/>
</platform>
</plugin>

View File

@@ -0,0 +1,281 @@
package cn.shuto.rfid;
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import com.szfore.api.SZForeElectricAPI;
import com.szfore.listener.CallbackListener;
import com.szfore.listener.RFIDListener;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PermissionHelper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class RfidPlugin extends CordovaPlugin {
//SDK
private SZForeElectricAPI mAPI;
//蓝牙适配器
private BluetoothAdapter mBluetoothAdapter = null;
//设备蓝牙开关定义code
private static final int REQUEST_ENABLE_BT = 1001;
//蓝牙权限code
private static final int REQUEST_PERMISSION_ACCESS_LOCATION = 1002;
private CallbackContext mCallbackContext;
//权限数组
private String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
//定义操作方法
//打开设备蓝牙
private static final String OPEN_BLUETOOTH = "open_bluetooth";
//获取蓝牙列表
private static final String GET_BLUETOOTH_LIST = "get_bluetooth_list";
//链接某一设备
private static final String CONNECT_BLUETOOTH = "connect_bluetooth";
//读EPC
private static final String READ = "read";
//写EPC
private static final String WRITE = "write";
//蓝牙设备列表
private List<BluetoothDevice> mBlueToothList;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
//初始化sdk
mAPI = SZForeElectricAPI.getInstance(cordova.getContext().getApplicationContext());
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mAPI.setRFIDListener(mRFIDListener);
}
/**
* @param action 操作action
* @param args 参数
* @param callbackContext 回调
* @return true / false
* @throws JSONException
*/
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.mCallbackContext = callbackContext;
switch (action) {
case OPEN_BLUETOOTH:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
this.cordova.startActivityForResult(this, enableIntent, REQUEST_ENABLE_BT);
} else {
this.mCallbackContext.success("蓝牙已开启");
}
break;
case GET_BLUETOOTH_LIST:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
this.cordova.startActivityForResult(this, enableIntent, REQUEST_ENABLE_BT);
} else {
if (!hasPermission()) {
PermissionHelper.requestPermissions(this, REQUEST_PERMISSION_ACCESS_LOCATION, permissions);
} else {
doDiscovery();
}
}
break;
case CONNECT_BLUETOOTH:
JSONObject obj = args.getJSONObject(0);
String address = obj.getString("address");
if ("".equals(address)) {
this.mCallbackContext.error("蓝牙地址为空");
} else {
mAPI.onStart(address, new CallbackListener() {
@Override
public void callback(final boolean b, final String s) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (b) {
mCallbackContext.success(s);
} else {
mCallbackContext.error(s);
}
}
});
}
});
}
break;
case READ:
if (mAPI != null) {
mAPI.sendCmdOnReadTag();
}
break;
case WRITE:
if (mAPI != null) {
//获取客户端传来的epc值与编码方式。编码方式如果不传则默认0C
JSONObject obj1 = args.getJSONObject(0);
String number = obj1.getString("epc_number");
String epclen = "0c";
if (obj1.has("epc_len")) {
epclen = obj1.getString("epc_len");
if ("".equals(epclen)) {
epclen = "0c";
}
}
int value;
//将16进制0C转换为十进制数 12
value = Integer.parseInt(epclen, 16);
if (value % 2 != 0) {
mCallbackContext.error("epclen输入字节长度不能为奇数");
} else {
if ("".equals(number)) {
mCallbackContext.error("EPC值不能为空");
} else if (value * 2 != number.length()) {
mCallbackContext.error("输入内容长度与设置的字节长度不一致");
} else {
mAPI.sendCmdOnEditEPC(number);
}
}
}
break;
default:
return false;
}
return true;
}
private RFIDListener mRFIDListener = new RFIDListener() {
@Override
public void onResult(final int i, final String s, final List<String> list) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (0 == i) {
if ("success".equals(s)) {
//读取成功的返回值
JSONObject obj = new JSONObject();
try {
obj.put("message","读取成功");
obj.put("data", list);
} catch (JSONException e) {
mCallbackContext.error(e.getMessage());
}
mCallbackContext.success(obj);
} else if ("指令执行成功".equals(s)) {
mCallbackContext.success("写入成功");
}
} else {
mCallbackContext.error(s);
}
}
});
}
};
private boolean hasPermission() {
for (String p : permissions) {
if (!PermissionHelper.hasPermission(this, p)) {
return false;
}
}
return true;
}
@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
if (requestCode == REQUEST_PERMISSION_ACCESS_LOCATION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doDiscovery();
} else {
this.mCallbackContext.error("请允许使用蓝牙权限");
}
}
super.onRequestPermissionResult(requestCode, permissions, grantResults);
}
/**
* 搜索蓝牙设备列表
*/
private void doDiscovery() {
mBlueToothList = new ArrayList<>();
// 注册蓝牙开始搜索广播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.cordova.getActivity().registerReceiver(mReceiver, filter);
// 注册蓝牙搜索完毕广播
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.cordova.getActivity().registerReceiver(mReceiver, filter);
//如果正在搜索,关闭
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
//开始搜索
mBluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBlueToothList.add(device);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
JSONObject obj = new JSONObject();
try {
obj.put("message", "蓝牙搜索完毕");
obj.put("data", mBlueToothList);
} catch (JSONException e) {
mCallbackContext.error(e.getMessage());
}
mCallbackContext.success(obj);
}
}
};
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
this.mCallbackContext.success("蓝牙开启成功");
} else {
this.mCallbackContext.error("蓝牙开启失败");
}
}
}
/**
* 断开蓝牙
*/
private void tryDisconnect() {
try {
if (null != mAPI) {
mAPI.onStop();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onDestroy() {
tryDisconnect();
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

21
www/rfid.js Normal file
View File

@@ -0,0 +1,21 @@
var exec = require('cordova/exec');
var rfid = {
openBlueTooth: function(success, error) {
exec(success, error, "rfid", "open_bluetooth", []);
},
getBlueToothList: function(success, error) {
exec(success, error, "rfid", "get_bluetooth_list", []);
},
connectBlueTooth: function(success, error, address) {
exec(success, error, "rfid", "connect_bluetooth", [address]);
},
read: function(success, error) {
exec(success, error, "rfid", "read", []);
},
write: function(success, error, epc_number, epc_len) {
exec(success, error, 'rfid', 'write', [epc_number, epc_len]);
}
};
module.exports = rfid;