`
闫老三
  • 浏览: 103264 次
社区版块
存档分类
最新评论

com.android.bluetooth.opp.BluetoothOppLauncherActivity 代码

 
阅读更多
		 
 	BluetoothOppLauncherActivity.java	 
/*
 * 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;
    }
}

 

 转载请注明原文地址: http://bajiewuneng.iteye.com/blog/1915962

 

分享到:
评论

相关推荐

    Android Bluetooth OPP Bugreport

    在Android系统中,Bluetooth OPP(Object Push Profile)是一种用于设备间传输小文件的协议,它允许用户方便地发送图片、联系人、日历事件等数据。这篇博客文章的附件是针对一个特定的Android设备(可能是Xiaomi设备...

    Android Bluetooth文件传输

    在Android平台上,蓝牙(Bluetooth)是一种常见的无线通信技术,用于设备间的短距离数据交换。本文将深入探讨如何在Android应用中实现蓝牙文件传输,包括如何建立连接、发送和接收文件,以及显示传输进度。 首先,...

    no.nordicsemi.android.apk

    no.nordicsemi.android.beacon.service.apk no.nordicsemi.android.mcp.apk no.nordicsemi.android.nrfbeacon.apk no.nordicsemi.android.nrftoolbox.apk nRFUART_Googlev2.apk nrfTempAndroid.apk

    Android Bluetooth

    通过分析这个示例代码,我们可以更深入地理解Android蓝牙编程的实际应用。 总之,Android Bluetooth功能提供了一个丰富的API来支持蓝牙设备的交互。开发者需要掌握如何开启和关闭蓝牙,搜索和连接蓝牙设备,以及...

    安卓开发-BluetoothHDP.zip

    这个压缩包"安卓开发-BluetoothHDP.zip"可能包含了一系列关于如何在安卓平台上实现Bluetooth HDP功能的教程、代码示例、文档或库文件。 【描述】:在描述中并未提供具体细节,但可以推测这可能是一个教学资源,帮助...

    Android SubSettings跳转

    &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" &gt; android:key="settings" android:title="@string/settings" &gt; android:targetClass="com.android.settings.Settings" ...

    Bluetooth_share

    蓝牙共享APK文件是Android设备之间常见的数据交换方式之一,尤其是在没有Wi-Fi网络或者数据流量限制的情况下。通常,这个过程包括以下步骤: 1. **开启蓝牙**:首先,用户需要在设备上开启蓝牙功能,并使其可被其他...

    Android bluetooth

    在Android系统中,蓝牙(Bluetooth)技术是一种广泛用于设备间无线通信的标准,它允许设备进行数据交换和配对,例如连接耳机、智能手表、车载音响等。本篇将深入探讨在Android蓝牙开发过程中可能遇到的问题及如何...

    Winsoft Bluetooth Library for Android v2.2 for Delphi FS.rar

    Delphi and C++ Builder Bluetooth library for Android. Features Uses Android Bluetooth API Supports BLE and BT Classic clients Available for Delphi/C++ Builder 10.1 - 11 Source code included in ...

    Android代码-Android-Bluetooth-Simulator

    As for now, you can communicate between different emulators using the RFComm protocol, you can start a discovery phase and enable/disable the bluetooth. What you need to do in order to use the ...

    no.nordicsemi.android.nrfmeshprovisioner.zip

    BLE(Bluetooth Low Energy)Mesh是一种基于蓝牙技术的无线通信网络协议,专为低功耗设备设计,适用于大规模设备网络,如智能家居、工业自动化和智能城市等场景。BLE Mesh网络中的节点可以互相发送消息,形成一个...

    Bluetooth LE for iOS and Android v2.3.unitypackage

    "Bluetooth LE for iOS and Android v2.3.unitypackage"是专门为iOS和Android平台设计的一个Unity插件,它为开发者提供了在Unity中实现BLE功能的工具集。 1. **蓝牙低功耗(Bluetooth LE)介绍** Bluetooth LE是一...

    Android Bluetooth初始化代码解析

    Framework使用BinderIPC机制来使用蓝牙服务,其代码路径是android\frameworks\base\core\java\android\bluetooth。Bluedroid协议栈是Android官方提供的实现,代码路径位于android\external\bluetooth\bluedroid。 ...

    Bluetooth Library for Android 1.6.rar

    bluetooth.url 可能是一个指向有关蓝牙开发或相关教程的网页链接,为开发者提供更深入的学习材料。 Delphi104、Delphi104-64、Delphi103、Delphi101 目录名表明该蓝牙库支持Embarcadero Delphi的不同版本,特别是...

    Android蓝牙开发工具 Bluetooth-LE-Library---Android-master

    在Android平台上进行蓝牙低功耗(Bluetooth Low Energy, BLE)开发时,`Bluetooth-LE-Library---Android-master` 是一个非常重要的工具库。这个库专为Android应用开发者设计,简化了与BLE设备交互的复杂性,使开发者...

    安卓DownloadProvider下载文件

    在Android开发中,实现文件下载是一项常见的需求。`DownloadProvider`是Android系统提供的一种机制,允许应用程序处理文件的下载操作,特别适用于后台下载和多线程下载。本篇将深入探讨如何利用`DownloadProvider`来...

    InTheHand.Net.Bluetooth.zip

    《InTheHand.Net.Bluetooth》是一款基于C#编程语言的蓝牙开发库,专为Windows操作系统(包括WinXp、2000、2003、Nt、Me、9x、Vista、Win7和Win8)设计。该库允许开发者在.NET Framework环境下轻松地集成蓝牙功能到...

    android中Content Provider

    在Android系统中,Content Provider是四大组件之一,它是应用程序间数据共享的核心机制。Content Provider允许一个应用暴露其数据,使得其他应用可以读取或者写入这些数据,甚至跨应用程序进行数据交换。这篇博客...

    Android_Bluetooth_Multiplayer_Basic_2.1.3.1.unitypackage

    Android_Bluetooth_Multiplayer_Basic_2.1.3.1

Global site tag (gtag.js) - Google Analytics