使用NFC无法发送和图像

时间:2016-08-18 15:34:47

标签: java android nfc

我有一种方法,当我点击按钮时,通过NFC将图像发送到另一部智能手机。代码似乎是正确的,执行到达最后一行代码但没有任何反应。这两款智能手机都配备了NFC和Android Beam。这是代码:

public void sendFile(View view){
    nfcAdapter=NfcAdapter.getDefaultAdapter(this);

    if(!nfcAdapter.isEnabled()){
        Toast.makeText(this,"Please enable NFC",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 {
        //If we got here, Nfc and Android beam are enabled
        Toast.makeText(this,"Ok",Toast.LENGTH_SHORT).show();

        String fileName="test.jpg";
        File fileDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File fileToTransfer =new File(fileDirectory,fileName);

        Toast.makeText(this,"Tutto Ok2",Toast.LENGTH_SHORT).show();
        fileToTransfer.setReadable(true,false);
        Uri fileUri=Uri.fromFile(fileToTransfer);
        if(fileUri==null){
            Toast.makeText(this,"File not found",Toast.LENGTH_SHORT).show();
        } else{
            //If we got here, the app has found the image and is ready to send it
            Toast.makeText(this,"Tutto Ok3",Toast.LENGTH_SHORT).show();
            //Send the image
            nfcAdapter.setBeamPushUris(new Uri[]{Uri.fromFile(fileToTransfer)},this);
        }

    }

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

光束通信无法在点击时启动。

请参阅官方文档:Sharing Files with NFC

  

此功能具有简单的API,允许用户开始传输   通过简单地触摸设备来处理。

一个设备必须触摸另一个设备才能激活Beam通信。 您可以在点击时允许光束传输,但必须触摸设备后。

请阅读:

例如,将文件发送到另一台设备:

public class MainActivity extends Activity {

    NfcAdapter mNfcAdapter;
    // Flag to indicate that Android Beam is available
    boolean mAndroidBeamAvailable  = false;

    // Instance that returns available files from this app
    private FileUriCallback mFileUriCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // NFC isn't available on the device
        if (!PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)){
            /*
             * Disable NFC features here.
             * For example, disable menu items or buttons that activate
             * NFC-related features
             */
            ...
            // Android Beam file transfer isn't supported
        } else if (Build.VERSION.SDK_INT <
                Build.VERSION_CODES.JELLY_BEAN_MR1) {
            // If Android Beam isn't available, don't continue.
            mAndroidBeamAvailable = false;
            /*
             * Disable Android Beam file transfer features here.
             */
            ...
            // Android Beam file transfer is available, continue
        } else {
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
            /*
             * Instantiate a new FileUriCallback to handle requests for
             * URIs
             */
            mFileUriCallback = new FileUriCallback();
            // Set the dynamic callback for URI requests.
            mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);
        }
    }


    /**
     * Callback that Android Beam file transfer calls to get
     * files to share
     */
    private class FileUriCallback implements
            NfcAdapter.CreateBeamUrisCallback {
        public FileUriCallback() {
        }
        /**
         * Create content URIs as needed to share with another device
         */
        @Override
        public Uri[] createBeamUris(NfcEvent event) {
            // List of URIs to provide to Android Beam
            Uri[] mFileUris = new Uri[1];

            String transferFile = "transferimage.jpg";
            File extDir = getExternalFilesDir(null);
            File requestFile = new File(extDir, transferFile);
            requestFile.setReadable(true, false);
            // Get a URI for the File and add it to the list of URIs
            fileUri = Uri.fromFile(requestFile);
            if (fileUri != null) {
                mFileUris[0] = fileUri;
            } else {
                Log.e("My Activity", "No File URI available for file.");
            }
            return mFileUris;
        }
    }
}

答案 1 :(得分:0)

使用其他智能手机解决。代码工作正常。某些设备存在NFC问题(在LG G2和Nexus 5上测试)