使用Android Beam进行近场通信(NFC)


要使用Android Beam特性需满足以下要求:

1.对于大文件的传输只能运行在Android4.1(API Level 16)或以上的系统上;
2.要传输的文件必须是位于外部存储空间(external storage)中,且对外具有可读性;
3.必须为要传输的文件提供一个file URI,由FileProvider.getUriForFile()方法生成的URI无效。

一、发送文件:

在manifest文件中添加权限:

uses-permission android:name="android.permission.NFC" />
    

指明你的app需要使用带有NFC硬件的设备:


如果不在manifest文件中声明,也可以在代码中验证,如:

public class MainActivity extends Activity {
    ...
    NfcAdapter mNfcAdapter;
    // Flag to indicate that Android Beam is available
    boolean mAndroidBeamAvailable  = false;
    ...
    @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);
        ...
        }
    }
    ...
}


实现 NfcAdapter.CreateBeamUrisCallback接口,添加createBeamUris回调方法,该方法返回一个URI数组,Android Beam会传输URI对应的文件,
该回调方法会在Android bean探测到用户想和另一个具备NFC功能的设备传输文件时调用:

private class FileUriCallback implements
            NfcAdapter.CreateBeamUrisCallback {
        public FileUriCallback() {
        }
        /**
         * Create content URIs as needed to share with another device
         */
        @Override
        public Uri[] createBeamUris(NfcEvent event) {
            return mFileUris;
        }
    }

给Android Beam设置该回调类,调用setBeamPushUrisCallback()方法:
 
mFileUriCallback = new FileUriCallback();
        // Set the dynamic callback for URI requests.
        mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);

指定要发送文件的file URI:

private Uri[] mFileUris = new Uri[10];
        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.");
        }



二、接收文件:

当Android Beam传输文件完成后,会向接收者发送一个通知,该通知包含一个Intent,action为ACTION_VIEW;mimeType为传送的第一个文件的
类型,以及指向第一个文件的URI,当接收者点击通知时,会开启相应的Activity。如果想让你的app对该意图做出反应,则应添加相应的,如:


        ...
        
            
            
            ...
        
    

添加权限:



Android Beam将所有传送的文件放在接收设备的同一个文件夹下,为使你的Activity能够更精确的反应(设备上的其他app也可能发出带ACTION_VIEW的intent),
应该对其中的URI进行协议验证,通过调用Uri.getScheme()方法,如:

public class ViewActivity extends Activity {
    ...
    // A File object containing the path to the transferred files
    private File mParentPath;
    // Incoming Intent
    private Intent mIntent;
    ...
    /*
     * Called from onNewIntent() for a SINGLE_TOP Activity
     * or onCreate() for a new Activity. For onNewIntent(),
     * remember to call setIntent() to store the most
     * current Intent
     *
     */
    private void handleViewIntent() {
        ...
        // Get the Intent action
        mIntent = getIntent();
        String action = mIntent.getAction();
        /*
         * For ACTION_VIEW, the Activity is being asked to display data.
         * Get the URI.
         */
        if (TextUtils.equals(action, Intent.ACTION_VIEW)) {
            // Get the URI from the Intent
            Uri beamUri = mIntent.getData();
            /*
             * Test for the type of URI, by getting its scheme value
             */
            if (TextUtils.equals(beamUri.getScheme(), "file")) {
                mParentPath = handleFileUri(beamUri);
            } else if (TextUtils.equals(
                    beamUri.getScheme(), "content")) {
                mParentPath = handleContentUri(beamUri);
            }
        }
        ...
    }
    ...
}


如果是file URI(scheme 为"file://"),该URI包含了传输的第一个文件的绝对路径,我们
可以获取它的上一级文件夹路径,这样可以得到包含所有的传输文件的文件夹,如handleFileUri()方法可以这样实现:

public String handleFileUri(Uri beamUri) {
        // Get the path part of the URI
        String fileName = beamUri.getPath();
        // Create a File object for this filename
        File copiedFile = new File(fileName);
        // Get a string containing the file's parent directory
        return copiedFile.getParent();
    }


当你传输的第一个文件类型为 "audio/*", "image/*", 或者"video/*",通常这个intent包含的是content URI,此时通过MediaStore
内容提供者可以获得对应传输文件的文件夹。有时候该content URI并不对应于MediaStore内容提供者,该种情况下这个content URI通常不指向
文件夹。

可以通过调用Uri.getAuthority()方法来确定是否为MediaStore对应的content URI,如果返回值是MediaStore.AUTHORITY,则该URI指向对应的文件夹,可以通过MediaStore
内容提供者获得;如果是其他任何值,那么只能展示URI对应的文件,不能获得对应的文件夹。如handleContentUri()方法可以这样实现:

先判断Authority值,然后调用query()方法,查询MediaColumns.DATA数据行,返回的cursor包含了URI对应文件的绝对路径,如:

public String handleContentUri(Uri beamUri) {
        // Position of the filename in the query Cursor
        int filenameIndex;
        // File object for the filename
        File copiedFile;
        // The filename stored in MediaStore
        String fileName;
        // Test the authority of the URI
        if (!TextUtils.equals(beamUri.getAuthority(), MediaStore.AUTHORITY)) {
            /*
             * Handle content URIs for other content providers
             */
        // For a MediaStore content URI
        } else {
            // Get the column that contains the file name
            String[] projection = { MediaStore.MediaColumns.DATA };
            Cursor pathCursor =
                    getContentResolver().query(beamUri, projection,
                    null, null, null);
            // Check for a valid cursor
            if (pathCursor != null &&
                    pathCursor.moveToFirst()) {
                // Get the column index in the Cursor
                filenameIndex = pathCursor.getColumnIndex(
                        MediaStore.MediaColumns.DATA);
                // Get the full file name including path
                fileName = pathCursor.getString(filenameIndex);
                // Create a File object for the filename
                copiedFile = new File(fileName);
                // Return the parent directory of the file
                return new File(copiedFile.getParent());
             } else {
                // The query didn't work; return null
                return null;
             }
        }
    }


 
 



你可能感兴趣的:(Google,Android,Developer,文档学习)