一、混合应用开发
混合应用顾名思义就是网页html和原生APP共同作用的结果
好处在一既有web的跨平台优势(安卓、苹果,电脑、国产电脑、平板电脑,自助机都能用)
好处二可以离线使用,比较稳定
好处三可以与本地硬件交互
二、web调用本地资源
web调用本地交互采用jsapi
代码示例
public WebView cwpd_Web;
java未来之窗内部插件 csp = new java未来之窗内部插件d(main_instance);
webview.addJavascriptInterface(new 未来之窗交互(this, this), "未来之窗交互名称");
js调用方式
未来之窗交互名称.读取酒店门锁(酒店门锁厂家)
三、android调用web
android 一般不知道当前web情况,一般采用2中方式
1.web不断轮询查询检测结果
缺点:消耗web资源,优点,技术要求低
2.采用webview内部通知机制
优点:不消耗内部资源,缺点:需要了解系统底层
四、webview调用NFC
权限
javascript
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" />
nfc 资源库
java
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.nfc.tech.NfcA;
nfc 服务
java
private NfcAdapter NFC_mAdapter2023;
private NdefMessage mNdefPushMessage;
private PendingIntent mPendingIntent;
nfc 服务启动
java
resolveIntent(getIntent());
NFC_mAdapter2023 = NfcAdapter.getDefaultAdapter(this);
//初始化NfcAdapter
LogToFile.d_windows("nfc","nfc调式记录","初始化NfcAdapter");
if (NFC_mAdapter2023 == null) {
// showMessage("无设备", "没有NFC设备");
// finish();
// return;
}else {
LogToFile.d_windows("nfc","nfc调式记录","NFC正常");
//初始化PendingIntent
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
LogToFile.d_windows("nfc","nfc调式记录","初始化PendingIntent");
mNdefPushMessage = new NdefMessage(new NdefRecord[]{
newTextRecord(
"Message from NFC Reader :-)", Locale.ENGLISH, true)}
);
}
android宿主
java
protected void onResume() {
if (NFC_mAdapter2023 != null) {
if (!NFC_mAdapter2023.isEnabled()) {
showWirelessSettingsDialog();
}
NFC_mAdapter2023.enableForegroundDispatch(this, mPendingIntent, null, null);
NFC_mAdapter2023.enableForegroundNdefPush(this, mNdefPushMessage);
}
}
五、android 检测NFC并通知web html
java
private void resolveIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
// Unknown tag type
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String 未来之窗NFC=dumpTagData(tag);
// LogToFile.d_windows();
LogToFile.d_windows("nfc","nfc调式记录",未来之窗NFC);
String 未来之窗反向js2 = "javascript:" + "未来之窗人工智能交互" + "(\"nfc_reader\",`" + 未来之窗NFC + "`)";
/*
webview.evaluateJavascript(未来之窗反向js, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
// LogUtil.e(TAG, "getPayId onReceiveValue " + functionName);
}
}
);
*/
cwpd_Web.evaluateJavascript(未来之窗反向js2, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
// LogUtil.e(TAG, "getPayId onReceiveValue " + functionName);
}
}
);
byte[] payload = 未来之窗NFC.getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
msgs = new NdefMessage[] { msg };
// mTags.add(tag);
}
// Setup the views
// buildTagViews(msgs);
}
}
六、web接受端
javascript
function cyberWin_Device_AIOT_Monitor(action,param){
// alert("类型:"+action);
// alert(param);
var 未来之窗json = eval("("+param+")");//转换为json对象
// alert(未来之窗json.id_dec);
}
到此设备上刷卡web就会通知