当前位置:   article > 正文

Android Nfc Beam数据传输_nfcadapter没有setndefpushmessage方法

nfcadapter没有setndefpushmessage方法

   从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:方法二用于传输不可变的数据,而方法三多用于传输可变的数据;

   三、应用:

                                                                             方法一的应用

  1. public class MainActivity extends AppCompatActivity{
  2. private NfcAdapter nfcAdapter;
  3. private PendingIntent pendingIntent;
  4. private IntentFilter[] filters;
  5. private String[][] techLists=null;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. checkNdef();
  11. }
  12. private void checkNdef() {
  13. nfcAdapter=NfcAdapter.getDefaultAdapter(this);
  14. pendingIntent=PendingIntent.getActivity(MainActivity.this, 500, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  15. IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
  16. try {
  17. filter.addDataType("*/*");
  18. filters=new IntentFilter[]{filter};
  19. } catch (IntentFilter.MalformedMimeTypeException e) {
  20. e.printStackTrace();
  21. }
  22. if (nfcAdapter==null){
  23. Toast.makeText(this,"不支持NFC功能",Toast.LENGTH_SHORT).show();
  24. }else{
  25. if (nfcAdapter.isEnabled()){
  26. Toast.makeText(this,"NFC功能已打开",Toast.LENGTH_SHORT).show();
  27. }else{
  28. Intent intent= new Intent(Settings.ACTION_NFC_SETTINGS);
  29. startActivity(intent);
  30. }
  31. }
  32. }
  33. @Override
  34. protected void onResume() {
  35. super.onResume();
  36. //前台调度系统
  37. nfcAdapter.enableForegroundDispatch(MainActivity.this,pendingIntent,filters,techLists);
  38. //enableForegroundNdefPush(Activity activity, NdefMessage message)的使用
  39. NdefMessage ndefMessage=NfcWriteUtil.writeAbsoluteUri("http://www.baidu.com");
  40. nfcAdapter.enableForegroundNdefPush(this,ndefMessage);
  41. }
  42. @Override
  43. protected void onPause() {
  44. super.onPause();
  45. nfcAdapter.disableForegroundDispatch(MainActivity.this);
  46. }
  47. @Override
  48. protected void onNewIntent(Intent intent) {
  49. super.onNewIntent(intent);
  50. Tag tag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  51. }
  52. }

                                                                           方法二的应用

  1. //第一步,实现callBack
  2. public class NfcP2pActivity extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback{
  3. private NfcAdapter nfcAdapter;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_nfc_p2p);
  8. checkNfcBeam();
  9. //第二步,绑定callBack
  10. nfcAdapter.setNdefPushMessageCallback(this,this);
  11. }
  12. private void checkNfcBeam() {
  13. nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  14. if (nfcAdapter !=null){
  15. if (!nfcAdapter.isEnabled()){
  16. //支持NFC,但是没有打开
  17. Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
  18. startActivity(intent);
  19. }else if(!nfcAdapter.isNdefPushEnabled()){
  20. //API 16+
  21. Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
  22. startActivity(intent);
  23. }
  24. }
  25. }
  26. @Override
  27. public NdefMessage createNdefMessage(NfcEvent event) {
  28. //封装数据,即写数据,发送数据
  29. NdefMessage ndefMessage=NfcWriteUtil.writeText("我是测试", Locale.CANADA,true);
  30. return ndefMessage;
  31. }
  32. @Override
  33. protected void onNewIntent(Intent intent) {
  34. super.onNewIntent(intent);
  35. //接收数据
  36. setIntent(intent);
  37. }
  38. @Override
  39. protected void onResume() {
  40. super.onResume();
  41. //判断数据是否为想要的
  42. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
  43. //解析数据(读数据)
  44. }
  45. }
  46. @Override
  47. protected void onPause() {
  48. super.onPause();
  49. }
  50. }
                                                                              方法三的应用

  1. public class NfcP2p2Activity extends AppCompatActivity {
  2. private NfcAdapter nfcAdapter;
  3. private NdefMessage ndefMessage;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_nfc_p2p);
  8. checkNfcBeam();
  9. nfcAdapter.setNdefPushMessage(ndefMessage,this);
  10. }
  11. private void checkNfcBeam() {
  12. nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  13. if (nfcAdapter !=null){
  14. if (!nfcAdapter.isEnabled()){
  15. //支持NFC,但是没有打开
  16. Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
  17. startActivity(intent);
  18. }else if(!nfcAdapter.isNdefPushEnabled()){
  19. //API 16+
  20. Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
  21. startActivity(intent);
  22. }
  23. }
  24. }
  25. @Override
  26. protected void onNewIntent(Intent intent) {
  27. super.onNewIntent(intent);
  28. //接收数据
  29. setIntent(intent);
  30. }
  31. @Override
  32. protected void onResume() {
  33. super.onResume();
  34. //判断数据是否为想要的
  35. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
  36. //解析数据(读数据)
  37. }
  38. }
  39. @Override
  40. protected void onPause() {
  41. super.onPause();
  42. }
  43. }
                                                                            文件传输

一、API:

   1、setBeamPushUris(Uri[] uris, Activity activity)

   2、setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback callback, Activity activity)

二、应用:

    

  1. public class NFCFileActivity extends Activity implements CreateBeamUrisCallback {
  2. private NfcAdapter mNfcAdapter;
  3. private PendingIntent mPendingIntent;
  4. private final String targetFilename = "/sdcard/temp_icon.png";
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_nfcfile);
  9. mNfcAdapter = mNfcAdapter.getDefaultAdapter(this);
  10. mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
  11. getClass()), 0);
  12. try {
  13. InputStream is = getResources().getAssets().open("icon.png");
  14. FileOutputStream fos = new FileOutputStream(targetFilename);
  15. byte[] buffer = new byte[10000];
  16. int n = is.read(buffer);
  17. fos.write(buffer, 0, n);
  18. fos.close();
  19. is.close();
  20. } catch (Exception e) {
  21. }
  22. mNfcAdapter.setBeamPushUrisCallback(this, this);
  23. }
  24. @Override
  25. public Uri[] createBeamUris(NfcEvent event) {
  26. Uri[] uris = new Uri[1];
  27. Uri uri = Uri.parse("file://" + targetFilename);
  28. uris[0] = uri;
  29. return uris;
  30. }
  31. }

     




     

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/320563
推荐阅读
相关标签
  

闽ICP备14008679号