2016-08-18 138 views
1

图片我有,当我点击一个按钮,通过NFC的图像发送到另一智能手机的方法。代码似乎是正确的,执行到达代码的最后一行,但没有任何反应。两款智能手机都有NFC和Android Beam。下面是代码:无法发送和使用NFC

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); 
     } 

    } 

任何帮助,将不胜感激

回答

0

梁通信不能在点击开始。

参见官方文档:Sharing Files with NFC

这个功能有一个简单的API,允许用户通过简单地触摸设备开始传输 过程。

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

请阅读:

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

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; 
     } 
    } 
} 
+0

即使使用此代码,NFC不work..the图像存在,但它不会把它(对LG G2和Nexus 5测试)的问题(当然,我改变了资源的路径) –

+0

编辑:正如我的第一个代码,它的工作原理1个超时的10 ..我不明白为什么 –

+0

什么步骤不工作?当触摸或在传输过程中或...您是否登录logcat? – LaurentY

0

使用其他智能手机解决。代码正常工作。有些设备有NFC