Android 11 完整代码实现NFC和读卡器连接
时间: 2024-02-05 10:02:33 浏览: 30
以下是一个简单的Android 11 NFC和读卡器连接的实现示例:
1. 添加NFC权限和NFC功能到AndroidManifest.xml文件中:
```xml
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
```
2. 在你的Activity中,创建一个NFC adapter对象:
```java
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
```
3. 确认读卡器连接:
```java
if (nfcAdapter != null && nfcAdapter.isEnabled()) {
Toast.makeText(this, "NFC is available", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
}
```
4. 创建一个NFC读取intent:
```java
private PendingIntent createPendingIntent() {
Intent intent = new Intent(this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(this, 0, intent, 0);
}
```
5. 处理NFC读取intent:
```java
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
}
if (messages.length > 0) {
String message = new String(messages[0].getRecords()[0].getPayload());
Log.d(TAG, "NFC message received: " + message);
}
}
}
}
```
6. 在onCreate()方法中启动NFC读取:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PendingIntent pendingIntent = createPendingIntent();
IntentFilter[] intentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);
}
```
7. 在onPause()方法中停止NFC读取:
```java
@Override
protected void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}
```
8. 为了连接读卡器,你需要使用UsbManager和一个BroadcastReceiver:
```java
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private UsbManager usbManager;
private UsbDevice usbDevice;
private final BroadcastReceiver usbBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case ACTION_USB_PERMISSION:
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
boolean permissionGranted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (device != null && permissionGranted) {
if (device.equals(usbDevice)) {
// 连接读卡器
// ...
}
}
break;
}
}
};
```
9. 在onCreate()方法中初始化UsbManager和读卡器:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_PERMISSION);
registerReceiver(usbBroadcastReceiver, filter);
// 查找和连接读卡器
findAndConnectUsbDevice();
}
private void findAndConnectUsbDevice() {
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
if (!usbDevices.isEmpty()) {
for (UsbDevice device : usbDevices.values()) {
if (isSupported(device)) {
PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(device, permissionIntent);
usbDevice = device;
break;
}
}
}
}
private boolean isSupported(UsbDevice device) {
// 检查设备是否支持读卡器的VID和PID
// ...
return true;
}
```
10. 在onDestroy()方法中,注销BroadcastReceiver:
```java
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(usbBroadcastReceiver);
}
```
这个示例可以帮助你开始使用NFC和读卡器连接,但具体实现还需要根据你的需求进行调整和拓展。