feat(nfc): 优化 NFC写卡功能并添加新方法- 在 BaseNfcWriteHelper 中添加了新的 writeData 方法,增加了 isConnect 参数
- 优化了 NativeNfcWriteHelper 中的 writeData 实现,支持选择性连接 NFC
- 更新了 NfcWriteAdapter,支持新的 writeData 方法- 修改了 NfcWreatHelper 中的 writeData 方法,使用新的接口
- 优化了 NfcWreatActivity 中的写卡逻辑
| | |
| | | |
| | | public abstract boolean writeData(byte[] str, int a, int b,NFCCallBack callBack); |
| | | |
| | | public abstract boolean writeData(byte[] str, int a, int b,boolean isConnect,NFCCallBack callBack); |
| | | /** |
| | | * 修改密码 |
| | | * |
| | |
| | | |
| | | @Override |
| | | public String getCradType() { |
| | | |
| | | MifareClassic mfc = MifareClassic.get(tag); |
| | | if (null != mfc) { |
| | | try { |
| | |
| | | } |
| | | } |
| | | } else if (!listA_PS.isEmpty()) { |
| | | if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { |
| | | if (mfc.authenticateSectorWithKeyA(0, defauleKey)) { |
| | | isOpen = true; |
| | | } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { |
| | | isOpen = true; |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | } else if (!listA_PS.isEmpty()) { |
| | | if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { |
| | | if (mfc.authenticateSectorWithKeyA(0, defauleKey)) { |
| | | isOpen = true; |
| | | } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { |
| | | isOpen = true; |
| | | } |
| | | } |
| | |
| | | |
| | | @Override |
| | | public boolean writeData(byte[] str, int a, int b, NFCCallBack callBack) { |
| | | return writeData(str, a, b, true,null); |
| | | } |
| | | |
| | | @Override |
| | | public boolean writeData(byte[] str, int a, int b, boolean isConnect, NFCCallBack callBack) { |
| | | Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b); |
| | | if (str.length == 16) { |
| | | try { |
| | |
| | | if (null != mfc) { |
| | | try { |
| | | //连接NFC |
| | | mfc.connect(); |
| | | if (isConnect){ |
| | | mfc.connect(); |
| | | } |
| | | //获取扇区数量 |
| | | int count = mfc.getSectorCount(); |
| | | //如果传进来的扇区大了或者小了直接退出方法 |
| | |
| | | } |
| | | } |
| | | } else if (listA_PS.size() != 0 && listA_PS.size() > a) { |
| | | if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(a))) { |
| | | if (mfc.authenticateSectorWithKeyA(a, defauleKey)){ |
| | | changePasword(a, mfc); |
| | | isOpen = true; |
| | | if (listKeyA.get(a).equals(defauleKey)) { |
| | | //当前为默认白卡密码时写卡时修改密码 |
| | | changePasword(a, mfc); |
| | | } |
| | | |
| | | }else if (mfc.authenticateSectorWithKeyA(a, listA_PS.get(a))) { |
| | | isOpen = true; |
| | | } |
| | | } |
| | | if (isOpen) { |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | callBack.isSusses(false, a + "扇区写卡报错" + e.getMessage()); |
| | | if (callBack != null) { |
| | | callBack.isSusses(false, a + "扇区写卡报错" + e.getMessage()); |
| | | } |
| | | return false; |
| | | } |
| | | } else { |
| | | callBack.isSusses(false, a + "扇区写卡报错,byte数组大小不为16"); |
| | | if (callBack != null) { |
| | | callBack.isSusses(false, a + "扇区写卡报错,byte数组大小不为16"); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public boolean writeData(byte[] str, int a, int b, boolean isConnect, NFCCallBack callBack) { |
| | | switch (BaseNfcActivity.adapterType) { |
| | | case ModelUtils.defaultType: |
| | | return nativeNfcWriteHelper.writeData(str, a, b,isConnect,callBack); |
| | | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public boolean changePasword(int a, MifareClassic mfc) { |
| | | switch (BaseNfcActivity.adapterType) { |
| | | case ModelUtils.defaultType: |
New file |
| | |
| | | package com.dayu.general.activity |
| | | |
| | | class CardListActivity:BaseActivity() { |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | package com.dayu.general.activity |
| | | |
| | | import android.app.Activity |
| | | import android.content.Context |
| | | import android.content.Intent |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.general.BaseApplication |
| | | import com.dayu.general.bean.net.LoginResult |
| | | import com.dayu.general.bean.net.SearchUserResult |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | | |
| | |
| | | BaseApplication.userId = t.content?.id ?: "" |
| | | val intent = Intent(myContext, MainActivity::class.java) |
| | | myContext.startActivity(intent) |
| | | myContext.finish() |
| | | } else { |
| | | // 处理搜索失败的情况 |
| | | ToastUtil.show(t.msg) |
| | |
| | | package com.dayu.general.activity |
| | | |
| | | import android.os.Bundle |
| | | import android.view.KeyEvent |
| | | import android.view.LayoutInflater |
| | | import android.widget.Toast |
| | | import androidx.core.content.ContextCompat |
| | | import androidx.fragment.app.Fragment |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | |
| | | import com.dayu.general.BaseApplication |
| | | import com.dayu.general.R |
| | | import com.dayu.general.adapter.TabAdapter |
| | | import com.dayu.general.bean.net.LoginResult |
| | | import com.dayu.general.bean.net.UserInfoResult |
| | | import com.dayu.general.databinding.ActivityMainBinding |
| | | import com.dayu.general.net.ApiManager |
| | |
| | | |
| | | var binding: ActivityMainBinding? = null |
| | | private val fragments: ArrayList<Fragment> = ArrayList() |
| | | var mExitTime: Long = 0 |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | | super.onCreate(savedInstanceState) |
| | | binding = ActivityMainBinding.inflate(LayoutInflater.from(this)) |
| | |
| | | setupFragments() |
| | | initView() |
| | | initTab() |
| | | getUserInfo(); |
| | | getUserInfo() |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | //点击两次退出程序 有时间间隔 间隔内点击则退出程序 否则 则提示 |
| | | override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { |
| | | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| | | if ((System.currentTimeMillis() - mExitTime) > 2000) { |
| | | Toast.makeText(this@MainActivity, "再按一次退出程序", Toast.LENGTH_SHORT) |
| | | .show() |
| | | mExitTime = System.currentTimeMillis() |
| | | } else { |
| | | this@MainActivity.finish() |
| | | } |
| | | return true |
| | | } |
| | | return super.onKeyDown(keyCode, event) |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | savedInstanceState: Bundle? |
| | | ): View? { |
| | | binding = FragmentMyBinding.inflate(inflater, container, false) |
| | | binding?.logoutRL?.setOnClickListener { |
| | | logout() |
| | | } |
| | | return binding?.root |
| | | } |
| | | |
| | |
| | | |
| | | if (cardType.isNotEmpty()) { |
| | | // map["cardType"] = cardType |
| | | map["cardType"] = "5" |
| | | map["operateType"] = "1" |
| | | } |
| | | if (remark.isNotEmpty()) { |
| | | map["remarks"] = remark |
| | |
| | | object : SubscriberListener<BaseResponse<String>>() { |
| | | override fun onNext(t: BaseResponse<String>) { |
| | | if (t.success) { |
| | | ToastUtil.show("写卡成功") |
| | | this@NfcWreatActivity.finish() |
| | | |
| | | } else { |
| | | // 处理搜索失败的情况 |
| | | ToastUtil.show(t.msg) |
| | |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("搜索失败: ${e?.message ?: "未知错误"}") |
| | | ToastUtil.show("上报失败: ${e?.message ?: "未知错误"}") |
| | | } |
| | | } |
| | | ) |
| | |
| | | val map = mutableMapOf<String, Any>() |
| | | |
| | | if (farmerId.isNotEmpty()) { |
| | | map["farmerId"] = farmerId |
| | | map["clientNum"] = farmerId |
| | | } |
| | | |
| | | if (farmerName.isNotEmpty()) { |
| | | map["farmerName"] = farmerName |
| | | map["name"] = farmerName |
| | | } |
| | | |
| | | if (cardNumber.isNotEmpty()) { |
| | | map["cardNumber"] = cardNumber |
| | | map["cardNum"] = cardNumber |
| | | } |
| | | |
| | | // 使用正确的类型参数 |
| | |
| | | */ |
| | | class NetConstans { |
| | | companion object { |
| | | const val BASE_URL: String = "https://no253541tf71.vicp.fun/" |
| | | // const val BASE_URL: String = "https://no253541tf71.vicp.fun/" |
| | | const val BASE_URL: String = "http://192.168.40.166:54321/" |
| | | |
| | | const val TOKEN_INVALID: String = "0000" |
| | | } |
| | |
| | | */ |
| | | fun writeData(str: ByteArray?, a: Int, b: Int,callBack: NFCCallBack): Boolean { |
| | | try { |
| | | return adapter.writeData(str, a, b,callBack) |
| | | return adapter.writeData(str, a, b,false,callBack) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | |
| | | android:textStyle="bold" /> |
| | | |
| | | <RelativeLayout |
| | | android:id="@+id/logoutRL" |
| | | android:layout_width="match_parent" |
| | | android:layout_height="wrap_content" |
| | | android:orientation="vertical" |
| | |
| | | android:layout_width="wrap_content" |
| | | android:layout_height="wrap_content" |
| | | android:layout_alignParentEnd="true" |
| | | android:layout_centerVertical="true" |
| | | android:src="@drawable/ic_arrow_right"/> |
| | | </RelativeLayout> |
| | | |