赞
踩
从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;
- @Override
- protected 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);
- }
- }
- }
-
- @Override
- protected 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);
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- nfcAdapter.disableForegroundDispatch(MainActivity.this);
- }
-
- @Override
- protected 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;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_nfc_p2p);
- checkNfcBeam();
- //第二步,绑定callBack
- nfcAdapter.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);
- }
- }
- }
-
- @Override
- public NdefMessage createNdefMessage(NfcEvent event) {
- //封装数据,即写数据,发送数据
- NdefMessage ndefMessage=NfcWriteUtil.writeText("我是测试", Locale.CANADA,true);
- return ndefMessage;
- }
-
- @Override
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- //接收数据
- setIntent(intent);
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- //判断数据是否为想要的
- if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
- //解析数据(读数据)
- }
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- }
- }

方法三的应用
- public class NfcP2p2Activity extends AppCompatActivity {
- private NfcAdapter nfcAdapter;
- private NdefMessage ndefMessage;
- @Override
- protected 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);
- }
- }
- }
-
- @Override
- protected void onNewIntent(Intent intent) {
- super.onNewIntent(intent);
- //接收数据
- setIntent(intent);
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- //判断数据是否为想要的
- if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
- //解析数据(读数据)
- }
- }
-
- @Override
- protected 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";
-
- @Override
- protected 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);
-
- }
-
- @Override
- public Uri[] createBeamUris(NfcEvent event) {
- Uri[] uris = new Uri[1];
- Uri uri = Uri.parse("file://" + targetFilename);
- uris[0] = uri;
- return uris;
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。