android - 监控通过蓝牙接收的文件

标签 android bluetooth file-transfer

我现在正在尝试通过蓝牙在 Android 设备之间传输文件。我已经实现了我的发送方。我没有使用 InputStream/OutputStream。我正在使用 Intent.ACTION_SEND。发送方的一切都正常,但当涉及到接收方时,我面临两个问题。

  1. 弹出通知“您要接收此文件吗?”。有什么办法可以避免这件事吗?
  2. 我怎么知道接收方有文件传入以及文件传输已完成或已停止?

这两个问题好像可以用InputStream/OutputStream来解决,但是我真的不想用。也许监听蓝牙的监听器,或者 BluetoothAdapter/BluetoothDevice 中的一些函数可以做到这一点?

感谢您的帮助。我的代码如下:(在我的 MainActivity.java 中)

public void beginBT() {
    if (isSender) {
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/log.txt");

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

        if (!findBluetoothForIntent(intent)){
            Toast.makeText(this, "Bluetooth Not Found.", Toast.LENGTH_SHORT).show();
        } else {
            //intent will send the file via bluetooth
            startActivity(intent);
        }
    } else { //receiver side
        //make device be discoverable
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
    }
}

public boolean findBluetoothForIntent(Intent intent){
    List appsList = getPackageManager().queryIntentActivities(intent, 0);
    String packageName = null;
    String className = null;

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

    if (className != null) {
        intent.setClassName(packageName, className);
        return true;
    } else {
        return false;
    }
} 

最佳答案

回答我自己的问题总是那么有趣!!

  1. 关于弹出通知,我无能为力,除非我使用 InStream/OutStream。
  2. 对于接收端,使用 BroadcastReceiver 来监控设备的 Action 。这里我监控蓝牙的断开 Action 。因为当设备开始接收文件时会有一个连接 Action ,当它完成时,会有一个断开连接 Action 。

不知道下面的代码是否对任何人有帮助,:)

MainActivity.java

private static final String BT_DISCONNECTED = "android.bluetooth.device.action.ACL_DISCONNECTED";
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action == BT_DISCONNECTED) {
            //now file transmitting has finished, can do something to the file
            //if you know the file name, better to check if the file is actually there 
            // - make sure this disconnection not initiated by any other reason.
        }
    }

IntentFileter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter);

警告:记得在退出 Activity 或不需要时注销此接收器

关于android - 监控通过蓝牙接收的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296007/

相关文章:

java - 不确定为什么在声明数组时我在 Java/Android 中遇到段错误

Android 显示动画 WebP

sftp - PuTTY (PSFTP) 是否使用二进制模式将文件从某个服务器传输到 Windows 客户端?

c# - 通过 Internet 传输大文件的可靠且快速的方法

android - Material Design 和 appcompat 不适用于较旧的 API 版本

Android jpeg 图片回调到灰度Opencv Mat

Android蓝牙socket非阻塞通信教程

一旦连接或断开蓝牙设备,Android 移动应用程序总是会重新启动

android - 使用 Xamarin 创建 Android 蓝牙终端连接到 HC-06 模块

java - 套接字传输的文件: contents are empty