diff --git a/README.md b/README.md index 0f2fd89..956f2dd 100644 --- a/README.md +++ b/README.md @@ -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}); + ``` \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..1494d53 --- /dev/null +++ b/package.json @@ -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" +} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..b9ca556 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,35 @@ + + + rfid + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/android/cn/shuto/rfid/RfidPlugin.java b/src/android/cn/shuto/rfid/RfidPlugin.java new file mode 100644 index 0000000..54de0af --- /dev/null +++ b/src/android/cn/shuto/rfid/RfidPlugin.java @@ -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 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 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(); + } + +} diff --git a/src/android/libs/SZForeRFID.jar b/src/android/libs/SZForeRFID.jar new file mode 100644 index 0000000..d8ec447 Binary files /dev/null and b/src/android/libs/SZForeRFID.jar differ diff --git a/src/android/libs/arm64-v8a/libszfore.so b/src/android/libs/arm64-v8a/libszfore.so new file mode 100644 index 0000000..1f442d8 Binary files /dev/null and b/src/android/libs/arm64-v8a/libszfore.so differ diff --git a/src/android/libs/armeabi-v7a/libszfore.so b/src/android/libs/armeabi-v7a/libszfore.so new file mode 100644 index 0000000..df701da Binary files /dev/null and b/src/android/libs/armeabi-v7a/libszfore.so differ diff --git a/src/android/libs/armeabi/libszfore.so b/src/android/libs/armeabi/libszfore.so new file mode 100644 index 0000000..5549225 Binary files /dev/null and b/src/android/libs/armeabi/libszfore.so differ diff --git a/src/android/libs/mips/libszfore.so b/src/android/libs/mips/libszfore.so new file mode 100644 index 0000000..1b03ec9 Binary files /dev/null and b/src/android/libs/mips/libszfore.so differ diff --git a/src/android/libs/mips64/libszfore.so b/src/android/libs/mips64/libszfore.so new file mode 100644 index 0000000..f98879c Binary files /dev/null and b/src/android/libs/mips64/libszfore.so differ diff --git a/src/android/libs/x86/libszfore.so b/src/android/libs/x86/libszfore.so new file mode 100644 index 0000000..274a0e6 Binary files /dev/null and b/src/android/libs/x86/libszfore.so differ diff --git a/src/android/libs/x86_64/libszfore.so b/src/android/libs/x86_64/libszfore.so new file mode 100644 index 0000000..4ec1430 Binary files /dev/null and b/src/android/libs/x86_64/libszfore.so differ diff --git a/www/rfid.js b/www/rfid.js new file mode 100644 index 0000000..8d9f9e5 --- /dev/null +++ b/www/rfid.js @@ -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; \ No newline at end of file