2016-08-18 139 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