从其他Android设备接收NFC文件无法正常工作

时间:2018-04-05 01:51:41

标签: android nfc nfc-p2p android-beam

我在String action = intent.getAction();

上发出了一个致命错误,说明了nullpointer异常

我如何让它发挥作用? 我想使用Android Beam将文本文件传输到另一台设备。光束检测到它并启动“水龙头”来发射光束。在电话上,但似乎永远不会完成光束。

           @TargetApi(16)
        public void sendNfcFile(){
            nfcAdapter = NfcAdapter.getDefaultAdapter(this);

            if(!nfcAdapter.isEnabled()){
                Toast.makeText(this, "Please enbable NFC in settings", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
            }else if(!nfcAdapter.isNdefPushEnabled()){
                Toast.makeText(this, "Please enable Android Beam", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
            }else{
                String filename = "personalShare.txt";
                File fileToTransfer = Environment.getExternalStoragePublicDirectory(filename);

                fileToTransfer.setReadable(true, false);

               nfcAdapter.setBeamPushUris(new Uri[]{Uri.fromFile(fileToTransfer)}, this);
            }
        }

        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);

        }
        @Override
        protected void onResume() {
            super.onResume();
            Intent intent = getIntent();
            String action = intent.getAction();
            if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
                Parcelable[] parcelables =
                        intent.getParcelableArrayExtra(
                                NfcAdapter.EXTRA_NDEF_MESSAGES);
                NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
                NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
                NdefRecord NdefRecord_0 = inNdefRecords[0];
                String inMsg = new String(NdefRecord_0.getPayload());
                TextView name  = (TextView) findViewById(R.id.fullname);
                name.setText(inMsg);
            }
        }

0 个答案:

没有答案