如何确认使用Android 4+通过蓝牙成功发送文件

时间:2014-09-18 18:41:58

标签: android bluetooth file-transfer

我写了一个应用程序,通过蓝牙将文件发送到笔记本电脑。我希望能够在确认文件发送成功后自动删除该文件。

我从BlueTooth分享了Toast消息,发送了该文件,但是如何从我的应用中检测到这个?

我可以使用回调吗?

以下是使用Android 4 +

发送文件的方法
 File filename = new File(path + "/" + itemValue);
           Uri uri = Uri.fromFile(filename);
           //send file via bluetooth
           Intent intent = new Intent(Intent.ACTION_SEND);
           intent.setType("text/*");
           //this causes us to send via bluetooth only
           intent.setClassName("com.android.bluetooth", "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
           intent.putExtra(Intent.EXTRA_STREAM, uri);
           startActivity(Intent.createChooser(intent, "Send file"));

1 个答案:

答案 0 :(得分:2)

查看来源,我看到Constants.javaHandoverService.java似乎表示传输完成后会发送广播。

/** intent action used to indicate the completion of a handover transfer */
public static final String ACTION_BT_OPP_TRANSFER_DONE =
        "android.btopp.intent.action.BT_OPP_TRANSFER_DONE";

/** intent extra used to indicate the success of a handover transfer */
public static final String EXTRA_BT_OPP_TRANSFER_STATUS =
        "android.btopp.intent.extra.BT_OPP_TRANSFER_STATUS";

public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;

HandoverService

if (action.equals(ACTION_BT_OPP_TRANSFER_DONE)) {
    int handoverStatus = intent.getIntExtra(EXTRA_BT_OPP_TRANSFER_STATUS,
            HANDOVER_TRANSFER_STATUS_FAILURE);
    if (handoverStatus == HANDOVER_TRANSFER_STATUS_SUCCESS) {

基本上,您需要为BroadcastReceiver注册ACTION_BT_OPP_TRANSFER_DONE,然后检查EXTRA_BT_OPP_TRANSFER_STATUS额外内容,看看它是成功还是失败。

由于这些似乎不是公共API的一部分,因此请注意,这可能会在将来的版本中发生变化。