【发布时间】:2014-10-07 09:27:24
【问题描述】:
我现在正尝试通过蓝牙在 Android 设备之间传输文件。我已经实现了我的发送方。我没有使用 InputStream/OutputStream。我正在使用 Intent.ACTION_SEND。发送方的一切工作正常,但在接收方方面,我面临两个问题。
- 弹出通知说“你想接收这个文件吗?”。有什么办法可以避免这件事吗?
- 我怎么知道有文件进来,文件传输在接收端完成或停止?
看来这两个问题可以用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