如何确认使用Android 4+通过蓝牙成功发送了文件 [英] How can I confim that a file was sent successfully via bluetooth using Android 4+
问题描述
我编写了一个通过蓝牙将文件发送到笔记本电脑的应用程序.在确认文件已成功发送后,我希望能够自动删除该文件.
I have written an app that sends a file to a laptop via Bluetooth. I would like to be able to delete that file automatically after some confirmation that the file was sent successfully.
我从蓝牙共享中收到一条Toast消息,说明文件已发送,但是如何从我的应用程序中检测到该消息?
I get a Toast message from BlueTooth Share that the file was sent, but how can I detect this from my app?
是否有可以用于此目的的回调?
Is there a callback that I can use for this?
这是我使用Android 4+发送文件的方法
Here is my method for sending the file using 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"));
推荐答案
查看源代码,我看到 HandoverService.java
似乎表明当转移完成.
Looking through the source, I see Constants.java
and HandoverService.java
which seem to indicate that a Broadcast is sent when the transfer is completed.
/** 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) {
因此,基本上,您需要为ACTION_BT_OPP_TRANSFER_DONE
注册一个BroadcastReceiver
,然后检查EXTRA_BT_OPP_TRANSFER_STATUS
额外内容,看看它是成功还是失败.
So basically, you need to register a BroadcastReceiver
for ACTION_BT_OPP_TRANSFER_DONE
, and then check for the EXTRA_BT_OPP_TRANSFER_STATUS
extra and see if it was success or failure.
由于这些似乎不是公共API的一部分,因此请注意,将来的发行版中可能会对此进行更改.
Since these don't appear to be part of the public API, be warned that this could change in a future release.
这篇关于如何确认使用Android 4+通过蓝牙成功发送了文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!