调用 系统蓝牙接口 发送文件

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

               

先看一下系统源码

com.android.bluetooth.opp.BluetoothOppLauncherActivity

/* * Copyright (c) 2008-2009, Motorola, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of the Motorola, Inc. nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */package com.android.bluetooth.opp;import com.android.bluetooth.R;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import android.app.Activity;import android.bluetooth.BluetoothDevicePicker;import android.content.Intent;import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.provider.Settings;/** * This class is designed to act as the entry point of handling the share intent * via BT from other APPs. and also make "Bluetooth" available in sharing method * selection dialog. */public class BluetoothOppLauncherActivity extends Activity {    private static final String TAG = "BluetoothLauncherActivity";    private static final boolean D = Constants.DEBUG;    private static final boolean V = Constants.VERBOSE;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Intent intent = getIntent();        String action = intent.getAction();        if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {            /*             * Other application is trying to share a file via Bluetooth,             * probably Pictures, videos, or vCards. The Intent should contain             * an EXTRA_STREAM with the data to attach.             */            if (action.equals(Intent.ACTION_SEND)) {                // TODO: handle type == null case                String type = intent.getType();                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);                CharSequence extra_text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);                // If we get ACTION_SEND intent with EXTRA_STREAM, we'll use the                // uri data;                // If we get ACTION_SEND intent without EXTRA_STREAM, but with                // EXTRA_TEXT, we will try send this TEXT out; Currently in                // Browser, share one link goes to this case;                if (stream != null && type != null) {                    if (V) Log.v(TAG, "Get ACTION_SEND intent: Uri = " + stream + "; mimetype = "                                + type);                    // Save type/stream, will be used when adding transfer                    // session to DB.                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,                            stream.toString());                } else if (extra_text != null && type != null) {                    if (V) Log.v(TAG, "Get ACTION_SEND intent with Extra_text = "                                + extra_text.toString() + "; mimetype = " + type);                    Uri fileUri = creatFileForSharedContent(this, extra_text);                    if (fileUri != null) {                        BluetoothOppManager.getInstance(this).saveSendingFileInfo(type,                                fileUri.toString());                    }                } else {                    Log.e(TAG, "type is null; or sending file URI is null");                    finish();                    return;                }            } else if (action.equals(Intent.ACTION_SEND_MULTIPLE)) {                ArrayList<Uri> uris = new ArrayList<Uri>();                String mimeType = intent.getType();                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);                if (mimeType != null && uris != null) {                    if (V) Log.v(TAG, "Get ACTION_SHARE_MULTIPLE intent: uris " + uris + "\n Type= "                                + mimeType);                    BluetoothOppManager.getInstance(this).saveSendingFileInfo(mimeType, uris);                } else {                    Log.e(TAG, "type is null; or sending files URIs are null");                    finish();                    return;                }            }            if (isAirplaneModeOn()) {                Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                in.putExtra("title", this.getString(R.string.airplane_error_title));                in.putExtra("content", this.getString(R.string.airplane_error_msg));                this.startActivity(in);                finish();                return;            }            // TODO: In the future, we may send intent to DevicePickerActivity            // directly,            // and let DevicePickerActivity to handle Bluetooth Enable.            if (!BluetoothOppManager.getInstance(this).isEnabled()) {                if (V) Log.v(TAG, "Prepare Enable BT!! ");                Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                this.startActivity(in);            } else {                if (V) Log.v(TAG, "BT already enabled!! ");                Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);                in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);                in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);                in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,                        BluetoothDevicePicker.FILTER_TYPE_TRANSFER);                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,                        Constants.THIS_PACKAGE_NAME);                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,                        BluetoothOppReceiver.class.getName());                this.startActivity(in1);            }        } else if (action.equals(Constants.ACTION_OPEN)) {            Uri uri = getIntent().getData();            if (V) Log.v(TAG, "Get ACTION_OPEN intent: Uri = " + uri);            Intent intent1 = new Intent();            intent1.setAction(action);            intent1.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());            intent1.setData(uri);            this.sendBroadcast(intent1);        }        finish();    }    /* Returns true if airplane mode is currently on */    private final boolean isAirplaneModeOn() {        return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,                0) == 1;    }    private Uri creatFileForSharedContent(Context context, CharSequence shareContent) {        if (shareContent == null) {            return null;        }        Uri fileUri = null;        FileOutputStream outStream = null;        try {            String fileName = getString(R.string.bluetooth_share_file_name) + ".html";            context.deleteFile(fileName);            String uri = shareContent.toString();            String content = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;"                + " charset=UTF-8\"/></head><body>" + "<a href=\"" + uri + "\">" + uri + "</a></p>"                + "</body></html>";            byte[] byteBuff = content.getBytes();            outStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);            if (outStream != null) {                outStream.write(byteBuff, 0, byteBuff.length);                fileUri = Uri.fromFile(new File(context.getFilesDir(), fileName));                if (fileUri != null) {                    if (D) Log.d(TAG, "Created one file for shared content: "                            + fileUri.toString());                }            }        } catch (FileNotFoundException e) {            Log.e(TAG, "FileNotFoundException: " + e.toString());            e.printStackTrace();        } catch (IOException e) {            Log.e(TAG, "IOException: " + e.toString());        } catch (Exception e) {            Log.e(TAG, "Exception: " + e.toString());        } finally {            try {                if (outStream != null) {                    outStream.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return fileUri;    }}


配置文件

  <activity android:name=".opp.BluetoothOppLauncherActivity"            android:process="@string/process"            android:theme="@android:style/Theme.Dialog" android:label="@string/bt_share_picker_label">            <intent-filter>                <action android:name="android.intent.action.SEND" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="image/*" />                <data android:mimeType="video/*" />                <data android:mimeType="audio/*" />                <data android:mimeType="text/x-vcard" />                <data android:mimeType="text/plain" />                <data android:mimeType="text/html" />                <data android:mimeType="application/zip" />                <data android:mimeType="application/vnd.ms-excel" />                <data android:mimeType="application/msword" />                <data android:mimeType="application/vnd.ms-powerpoint" />                <data android:mimeType="application/pdf" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.SEND_MULTIPLE" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="image/*" />                <data android:mimeType="video/*" />                <data android:mimeType="x-mixmedia/*" />                <data android:mimeType="text/x-vcard" />            </intent-filter>            <intent-filter>                <action android:name="android.btopp.intent.action.OPEN" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.item/vnd.android.btopp" />            </intent-filter>        </activity>

客户端调用实现

        Intent intent = new Intent();  intent.setAction(Intent.ACTION_SEND);//  intent.setType("application/octet-stream");  intent.setType("*/*"); //  intent.setClassName("com.android.bluetooth"                , "com.android.bluetooth.opp.BluetoothOppLauncherActivity");  intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile( new File("/sdcard/20095111013295162.jpg")));  startActivity(intent);

真机测试可以直接发送image到另一台设备中




           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值