【问题标题】:monitor file receiving via bluetooth通过蓝牙监听文件接收
【发布时间】:2014-10-07 09:27:24
【问题描述】:

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

  1. 弹出通知说“你想接收这个文件吗?”。有什么办法可以避免这件事吗?
  2. 我怎么知道有文件进来,文件传输在接收端完成或停止?

看来这两个问题可以用InputStream/OutputStream解决,但是我真的不想用。也许监听蓝牙的监听器,或者蓝牙适配器/蓝牙设备中的某些功能可以做到这一点?

感谢您的帮助。我的代码如下:(在我的 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;
    }
} 

【问题讨论】:

    标签: android bluetooth file-transfer


    【解决方案1】:

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

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

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

    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);
    

    警告:请记住在退出活动时取消注册此接收器,或者只是在不需要时取消注册

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多