本文主要是介绍Android Nfc Beam数据传输,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
从NfcAdapter的官方文档我们可以得知,Android Beam技术可以实现简单的信息的传输,同样支持文件的传输。
简单消息的传输
一、简单信息的传输API:
1、enableForegroundNdefPush(Activity activity, NdefMessage message)
2、setNdefPushMessage (NdefMessage message, Activity activity, Activity... activities)
3、setNdefPushMessageCallback (NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities)
二、三者之间的区别:
1:方法一是API 10以后使用,方法二、三都是API 14以后才可以用;
2:方法二用于传输不可变的数据,而方法三多用于传输可变的数据;
三、应用:
方法一的应用
public class MainActivity extends AppCompatActivity{private NfcAdapter nfcAdapter;private PendingIntent pendingIntent;private IntentFilter[] filters;private String[][] techLists=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);checkNdef();}private void checkNdef() {nfcAdapter=NfcAdapter.getDefaultAdapter(this);pendingIntent=PendingIntent.getActivity(MainActivity.this, 500, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);try {filter.addDataType("*/*");filters=new IntentFilter[]{filter};} catch (IntentFilter.MalformedMimeTypeException e) {e.printStackTrace();}if (nfcAdapter==null){Toast.makeText(this,"不支持NFC功能",Toast.LENGTH_SHORT).show();}else{if (nfcAdapter.isEnabled()){Toast.makeText(this,"NFC功能已打开",Toast.LENGTH_SHORT).show();}else{Intent intent= new Intent(Settings.ACTION_NFC_SETTINGS);startActivity(intent);}}}@Overrideprotected void onResume() {super.onResume();//前台调度系统nfcAdapter.enableForegroundDispatch(MainActivity.this,pendingIntent,filters,techLists);//enableForegroundNdefPush(Activity activity, NdefMessage message)的使用NdefMessage ndefMessage=NfcWriteUtil.writeAbsoluteUri("http://www.baidu.com");nfcAdapter.enableForegroundNdefPush(this,ndefMessage);}@Overrideprotected void onPause() {super.onPause();nfcAdapter.disableForegroundDispatch(MainActivity.this);}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);Tag tag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);}
}
方法二的应用
//第一步,实现callBack
public class NfcP2pActivity extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback{private NfcAdapter nfcAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_nfc_p2p);checkNfcBeam();//第二步,绑定callBacknfcAdapter.setNdefPushMessageCallback(this,this);}private void checkNfcBeam() {nfcAdapter = NfcAdapter.getDefaultAdapter(this);if (nfcAdapter !=null){if (!nfcAdapter.isEnabled()){//支持NFC,但是没有打开Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);startActivity(intent);}else if(!nfcAdapter.isNdefPushEnabled()){//API 16+Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);startActivity(intent);}}}@Overridepublic NdefMessage createNdefMessage(NfcEvent event) {//封装数据,即写数据,发送数据NdefMessage ndefMessage=NfcWriteUtil.writeText("我是测试", Locale.CANADA,true);return ndefMessage;}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);//接收数据setIntent(intent);}@Overrideprotected void onResume() {super.onResume();//判断数据是否为想要的if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){//解析数据(读数据)}}@Overrideprotected void onPause() {super.onPause();}
}
方法三的应用
public class NfcP2p2Activity extends AppCompatActivity {private NfcAdapter nfcAdapter;private NdefMessage ndefMessage;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_nfc_p2p);checkNfcBeam();nfcAdapter.setNdefPushMessage(ndefMessage,this);}private void checkNfcBeam() {nfcAdapter = NfcAdapter.getDefaultAdapter(this);if (nfcAdapter !=null){if (!nfcAdapter.isEnabled()){//支持NFC,但是没有打开Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);startActivity(intent);}else if(!nfcAdapter.isNdefPushEnabled()){//API 16+Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);startActivity(intent);}}}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);//接收数据setIntent(intent);}@Overrideprotected void onResume() {super.onResume();//判断数据是否为想要的if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){//解析数据(读数据)}}@Overrideprotected void onPause() {super.onPause();}
}
文件传输
一、API:
1、setBeamPushUris(Uri[] uris, Activity activity)
2、setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback callback, Activity activity)
二、应用:
public class NFCFileActivity extends Activity implements CreateBeamUrisCallback {private NfcAdapter mNfcAdapter;private PendingIntent mPendingIntent;private final String targetFilename = "/sdcard/temp_icon.png";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_nfcfile);mNfcAdapter = mNfcAdapter.getDefaultAdapter(this);mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0);try {InputStream is = getResources().getAssets().open("icon.png");FileOutputStream fos = new FileOutputStream(targetFilename);byte[] buffer = new byte[10000];int n = is.read(buffer);fos.write(buffer, 0, n);fos.close();is.close();} catch (Exception e) {}mNfcAdapter.setBeamPushUrisCallback(this, this);}@Overridepublic Uri[] createBeamUris(NfcEvent event) {Uri[] uris = new Uri[1];Uri uri = Uri.parse("file://" + targetFilename);uris[0] = uri;return uris;}}
这篇关于Android Nfc Beam数据传输的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!