当前位置: 首页 > 知识库问答 >
问题:

我如何通过蓝牙和android设备发送文件

庄宇
2023-03-14
BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) adapter.getBondedDevices().toArray(new BluetoothDevice[0]);

for (BluetoothDevice d : mAllBondedDevices) {
    Log.d("trace", d.getName().toString()); //check it here
}

共有1个答案

高德水
2023-03-14

如果我正确理解您的问题,将您的文件传输到其他设备而不编写任何服务器和客户端,您可以使用以下方法之一

1.使用呼叫意图发送文件

Intent intent = new Intent();  
intent.setAction(Intent.ACTION_SEND);   
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("your_file_address")));
startActivity(intent);

但是在这段代码中,出现了共享意图,然后您需要选择蓝牙来传输您的文件

    String className = null;
    String packageName = null;

    file = new File(Environment.getExternalStorageDirectory(), "file.pdf");

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file) );

    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> appsList = pm.queryIntentActivities( intent, 0);

    if(appsList.size() > 0 ){
        boolean found = false;

        for(ResolveInfo info: appsList){
            packageName = info.activityInfo.packageName;
            if( packageName.equals("com.android.bluetooth")){
                className = info.activityInfo.name;
                found = true;
                break;// found
            }
        }

    }

    intent.setClassName(packageName, className);
    activity.startActivity(intent);
ContentValues values = new ContentValues();
        values.put(BluetoothShare.URI, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "clip.3gp")).toString());
        values.put(BluetoothShare.DESTINATION, address);
        values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
        Long ts = System.currentTimeMillis();
        values.put(BluetoothShare.TIMESTAMP, ts);
        Uri contentUri = activity.getContentResolver().insert(BluetoothShare.CONTENT_URI, values); 
package com.android.bluetooth.opp;

/*
 * 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.
 */

import android.net.Uri;



import android.provider.BaseColumns;
import android.net.Uri;

/**
 * Exposes constants used to interact with the Bluetooth Share manager's content
 * provider.
 * @hide
 */

public final class BluetoothShare implements BaseColumns {
    private BluetoothShare() {
    }

    /**
     * The permission to access the Bluetooth Share Manager
     */
    public static final String PERMISSION_ACCESS = "android.permission.ACCESS_BLUETOOTH_SHARE";

    /**
     * The content:// URI for the data table in the provider
     */
    public static final Uri CONTENT_URI = Uri.parse("content://com.android.bluetooth.opp/btopp");

    /**
     * Broadcast Action: this is sent by the Bluetooth Share component to
     * transfer complete. The request detail could be retrieved by app * as _ID
     * is specified in the intent's data.
     */
    public static final String TRANSFER_COMPLETED_ACTION = "android.btopp.intent.action.TRANSFER_COMPLETE";

    /**
     * This is sent by the Bluetooth Share component to indicate there is an
     * incoming file need user to confirm.
     */
    public static final String INCOMING_FILE_CONFIRMATION_REQUEST_ACTION = "android.btopp.intent.action.INCOMING_FILE_NOTIFICATION";

    /**
     * This is sent by the Bluetooth Share component to indicate there is an
     * incoming file request timeout and need update UI.
     */
    public static final String USER_CONFIRMATION_TIMEOUT_ACTION = "android.btopp.intent.action.USER_CONFIRMATION_TIMEOUT";

    /**
     * The name of the column containing the URI of the file being
     * sent/received.
     * <P>
     * Type: TEXT
     * </P>
     * <P>
     * Owner can Init/Read
     * </P>
     */
    public static final String URI = "uri";

    /**
     * The name of the column containing the filename that the incoming file
     * request recommends. When possible, the Bluetooth Share manager will
     * attempt to use this filename, or a variation, as the actual name for the
     * file.
     * <P>
     * Type: TEXT
     * </P>
     * <P>
     * Owner can Init/Read
     * </P>
     */
    public static final String FILENAME_HINT = "hint";

    /**
     * The name of the column containing the filename where the shared file was
     * actually stored.
     * <P>
     * Type: TEXT
     * </P>
     * <P>
     * Owner can Read
     * </P>
     */
    public static final String _DATA = "_data";

    /**
     * The name of the column containing the MIME type of the shared file.
     * <P>
     * Type: TEXT
     * </P>
     * <P>
     * Owner can Init/Read
     * </P>
     */
    public static final String MIMETYPE = "mimetype";

    /**
     * The name of the column containing the direction (Inbound/Outbound) of the
     * transfer. See the DIRECTION_* constants for a list of legal values.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Init/Read
     * </P>
     */
    public static final String DIRECTION = "direction";

    /**
     * The name of the column containing Bluetooth Device Address that the
     * transfer is associated with.
     * <P>
     * Type: TEXT
     * </P>
     * <P>
     * Owner can Init/Read
     * </P>
     */
    public static final String DESTINATION = "destination";

    /**
     * The name of the column containing the flags that controls whether the
     * transfer is displayed by the UI. See the VISIBILITY_* constants for a
     * list of legal values.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Init/Read/Write
     * </P>
     */
    public static final String VISIBILITY = "visibility";

    /**
     * The name of the column containing the current user confirmation state of
     * the transfer. Applications can write to this to confirm the transfer. the
     * USER_CONFIRMATION_* constants for a list of legal values.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Init/Read/Write
     * </P>
     */
    public static final String USER_CONFIRMATION = "confirm";

    /**
     * The name of the column containing the current status of the transfer.
     * Applications can read this to follow the progress of each download. See
     * the STATUS_* constants for a list of legal values.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Read
     * </P>
     */
    public static final String STATUS = "status";

    /**
     * The name of the column containing the total size of the file being
     * transferred.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Read
     * </P>
     */
    public static final String TOTAL_BYTES = "total_bytes";

    /**
     * The name of the column containing the size of the part of the file that
     * has been transferred so far.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Read
     * </P>
     */
    public static final String CURRENT_BYTES = "current_bytes";

    /**
     * The name of the column containing the timestamp when the transfer is
     * initialized.
     * <P>
     * Type: INTEGER
     * </P>
     * <P>
     * Owner can Read
     * </P>
     */
    public static final String TIMESTAMP = "timestamp";

    /**
     * This transfer is outbound, e.g. share file to other device.
     */
    public static final int DIRECTION_OUTBOUND = 0;

    /**
     * This transfer is inbound, e.g. receive file from other device.
     */
    public static final int DIRECTION_INBOUND = 1;

    /**
     * This transfer is waiting for user confirmation.
     */
    public static final int USER_CONFIRMATION_PENDING = 0;

    /**
     * This transfer is confirmed by user.
     */
    public static final int USER_CONFIRMATION_CONFIRMED = 1;

    /**
     * This transfer is auto-confirmed per previous user confirmation.
     */
    public static final int USER_CONFIRMATION_AUTO_CONFIRMED = 2;

    /**
     * This transfer is denied by user.
     */
    public static final int USER_CONFIRMATION_DENIED = 3;

    /**
     * This transfer is timeout before user action.
     */
    public static final int USER_CONFIRMATION_TIMEOUT = 4;

    /**
     * This transfer is visible and shows in the notifications while in progress
     * and after completion.
     */
    public static final int VISIBILITY_VISIBLE = 0;

    /**
     * This transfer doesn't show in the notifications.
     */
    public static final int VISIBILITY_HIDDEN = 1;

    /**
     * Returns whether the status is informational (i.e. 1xx).
     */
    public static boolean isStatusInformational(int status) {
        return (status >= 100 && status < 200);
    }

    /**
     * Returns whether the transfer is suspended. (i.e. whether the transfer
     * won't complete without some action from outside the transfer manager).
     */
    public static boolean isStatusSuspended(int status) {
        return (status == STATUS_PENDING);
    }

    /**
     * Returns whether the status is a success (i.e. 2xx).
     */
    public static boolean isStatusSuccess(int status) {
        return (status >= 200 && status < 300);
    }

    /**
     * Returns whether the status is an error (i.e. 4xx or 5xx).
     */
    public static boolean isStatusError(int status) {
        return (status >= 400 && status < 600);
    }

    /**
     * Returns whether the status is a client error (i.e. 4xx).
     */
    public static boolean isStatusClientError(int status) {
        return (status >= 400 && status < 500);
    }

    /**
     * Returns whether the status is a server error (i.e. 5xx).
     */
    public static boolean isStatusServerError(int status) {
        return (status >= 500 && status < 600);
    }

    /**
     * Returns whether the transfer has completed (either with success or
     * error).
     */
    public static boolean isStatusCompleted(int status) {
        return (status >= 200 && status < 300) || (status >= 400 && status < 600);
    }

    /**
     * This transfer hasn't stated yet
     */
    public static final int STATUS_PENDING = 190;

    /**
     * This transfer has started
     */
    public static final int STATUS_RUNNING = 192;

    /**
     * This transfer has successfully completed. Warning: there might be other
     * status values that indicate success in the future. Use isSucccess() to
     * capture the entire category.
     */
    public static final int STATUS_SUCCESS = 200;

    /**
     * This request couldn't be parsed. This is also used when processing
     * requests with unknown/unsupported URI schemes.
     */
    public static final int STATUS_BAD_REQUEST = 400;

    /**
     * This transfer is forbidden by target device.
     */
    public static final int STATUS_FORBIDDEN = 403;

    /**
     * This transfer can't be performed because the content cannot be handled.
     */
    public static final int STATUS_NOT_ACCEPTABLE = 406;

    /**
     * This transfer cannot be performed because the length cannot be determined
     * accurately. This is the code for the HTTP error "Length Required", which
     * is typically used when making requests that require a content length but
     * don't have one, and it is also used in the client when a response is
     * received whose length cannot be determined accurately (therefore making
     * it impossible to know when a transfer completes).
     */
    public static final int STATUS_LENGTH_REQUIRED = 411;

    /**
     * This transfer was interrupted and cannot be resumed. This is the code for
     * the OBEX error "Precondition Failed", and it is also used in situations
     * where the client doesn't have an ETag at all.
     */
    public static final int STATUS_PRECONDITION_FAILED = 412;

    /**
     * This transfer was canceled
     */
    public static final int STATUS_CANCELED = 490;

    /**
     * This transfer has completed with an error. Warning: there will be other
     * status values that indicate errors in the future. Use isStatusError() to
     * capture the entire category.
     */
    public static final int STATUS_UNKNOWN_ERROR = 491;

    /**
     * This transfer couldn't be completed because of a storage issue.
     * Typically, that's because the file system is missing or full.
     */
    public static final int STATUS_FILE_ERROR = 492;

    /**
     * This transfer couldn't be completed because of no sdcard.
     */
    public static final int STATUS_ERROR_NO_SDCARD = 493;

    /**
     * This transfer couldn't be completed because of sdcard full.
     */
    public static final int STATUS_ERROR_SDCARD_FULL = 494;

    /**
     * This transfer couldn't be completed because of an unspecified un-handled
     * OBEX code.
     */
    public static final int STATUS_UNHANDLED_OBEX_CODE = 495;

    /**
     * This transfer couldn't be completed because of an error receiving or
     * processing data at the OBEX level.
     */
    public static final int STATUS_OBEX_DATA_ERROR = 496;

    /**
     * This transfer couldn't be completed because of an error when establishing
     * connection.
     */
    public static final int STATUS_CONNECTION_ERROR = 497;

}
 类似资料:
  • 我写了一个通过蓝牙发送/接收文件的应用程序,但我想把这个应用程序设置为默认的应用程序发送和接收文件! 移动设备向我的手机发送文件时的说明;我的应用程序获取文件,并与它一起工作之后,保存在我想要的时间和地点。

  • 我正在尝试在android设备中通过蓝牙发送文件。我做了发现,连接,并做了一个蓝牙插座。问题是当我在蓝牙套接字的输出流中写入字节数组时,接收方没有接收到任何东西,尽管它接受正在发送的东西。

  • 我制作了一个android应用程序来扫描蓝牙设备,并向我的服务器发送一个HTTP请求,这样我就可以检测它们是开着还是关着。我已经用我的带有蓝牙适配器的台式电脑测试过了,效果很好。当检测到电脑上的蓝牙功能时,它显示电脑是打开的,当我关闭电脑上的蓝牙功能时,它是关闭的。现在,我需要使用这个应用程序的设备有:雅伯投影仪、Bose SoundLink和JBL耳机,但我遇到了一些问题。 首先,投影仪似乎无法

  • 我正在编写一个Android应用程序,它应该可以通过蓝牙发现设备。 我没有得到任何例外,但设备只是没有找到,即使我的windows pc找到他们(并可以找到自己)。 我肯定他们是好的,但我两种方法都试过了。当然我也分别试过了。 下面是我的ListActivity,它搜索设备: 日志显示扫描后MLEDeviceListAdapter:0和扫描后isEmpty():true。 则从不调用该日志(onS

  • 我正在开发一个蓝牙4.0低能耗应用程序来快速扫描周围的广告专用设备(每秒10次或更多),并用RSSI获取他们的地址。根据Android蓝牙应用编程接口,发现是异步的,大约需要12秒。有什么办法可以设置这个时间吗?或者可以使用HCI或蓝牙堆栈直接与设备通信?

  • 我对Arduino Uno使用以下代码: 未设置要发送到的特定设备。 如果我想在笔记本电脑(运行Ubuntu 14.04)上接收数据,我只需调用: 和 在另一个终端实例中,它可以工作。 如何使用Android应用程序实现相同的行为?以下示例代码指定了设备。我找不到任何其他代码。此外,它仅在我在笔记本电脑上侦听这样的传入连接时才有效: 我希望我的Android应用程序与Arduino示例完全一样工作