如何在Android模拟器上模拟读取NFC标签(Java自动化测试,API30)
Android模拟器模拟NFC标签读取(API30,Java)
一、开启模拟器NFC支持
- 创建或编辑Android模拟器时,在Hardware配置中确保NFC选项处于启用状态。
- 启动模拟器后,下拉快捷设置面板,打开NFC开关。
二、用ADB命令模拟NFC标签触发
Android模拟器支持通过ADB命令直接发送NFC NDEF消息,这是自动化测试中最便捷的方式:
- 发送纯文本标签:
adb shell nfc send -t "text/plain" -d "模拟NFC文本内容" - 发送URI类型标签:
adb shell nfc send -t "application/vnd.android.nfc.uri" -d "https://example.com"
参数说明:
-t:指定NDEF记录的MIME类型-d:指定标签携带的数据内容
三、应用侧NFC读取逻辑实现(API30适配)
1. 配置Manifest
添加NFC权限和Intent过滤器,让应用能接收NFC触发的Intent:
<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity>
2. Java代码处理NFC Intent
在Activity中处理NFC触发的Intent,并更新UI用于测试验证:
import android.nfc.NfcAdapter; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import java.io.UnsupportedEncodingException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handleNfcIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleNfcIntent(intent); } private void handleNfcIntent(Intent intent) { String action = intent.getAction(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if (rawMessages != null && rawMessages.length > 0) { NdefMessage message = (NdefMessage) rawMessages[0]; NdefRecord record = message.getRecords()[0]; try { // 解析文本类型的NDEF记录 String payload = new String(record.getPayload(), "UTF-8"); // 更新UI显示读取到的内容 TextView nfcContentTv = findViewById(R.id.nfc_content_tv); nfcContentTv.setText(payload); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } }
四、自动化测试集成(Espresso示例)
在UI自动化测试中,通过调用ADB命令模拟NFC触发,再验证UI反馈:
import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static androidx.test.espresso.assertion.ViewAssertions.matches; import java.io.IOException; @RunWith(AndroidJUnit4.class) public class NfcAutomationTest { @Rule public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class); @Test public void testNfcTagRead() throws IOException, InterruptedException { // 执行ADB命令发送模拟NFC标签 Process process = Runtime.getRuntime().exec("adb shell nfc send -t \"text/plain\" -d \"自动化测试NFC内容\""); process.waitFor(); // 验证UI是否正确显示读取到的内容 onView(withId(R.id.nfc_content_tv)) .check(matches(withText("自动化测试NFC内容"))); } }
注意事项:
- 测试前确保模拟器NFC已开启
- API30及以上版本中,NFC属于正常权限,无需动态申请
内容的提问来源于stack exchange,提问作者Igor
相关产品推荐
相关产品推荐

