【发布时间】:2012-01-01 00:05:34
【问题描述】:
有什么方法可以使用 Android 的内部蓝牙将文件发送到其他设备?请举个例子。
【问题讨论】:
-
你为什么不先google一下?
标签: android
有什么方法可以使用 Android 的内部蓝牙将文件发送到其他设备?请举个例子。
【问题讨论】:
标签: android
奇怪的是,Android 没有明确的 OBEX api。无论如何,看看这个项目:
或者您也可以使用this 解决方案
BluetoothDevice device;
String filePath = Environment.getExternalStorageDirectory().toString() + "/file.jpg";
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, Uri.fromFile(new File(filePath)).toString());
values.put(BluetoothShare.DESTINATION, device.getAddress());
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
Uri contentUri = getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
(需要this class)
【讨论】:
这是一个你可以使用的小功能
/**
* Method to share data via bluetooth
* */
public void bluetoothFunctionality() {
String path = Environment.getExternalStorageDirectory() + "/"
+ Config.FILENAME;
File file = new File(path);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
}
此方法将使用默认设备蓝牙功能将文件发送到另一台设备。 在执行此操作之前,您必须先配对设备,这是限制。 要发送不同类型的文件,您只需在 set type 方法中更改 MIME 类型
在您的清单文件中,您必须添加两个权限,例如
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
【讨论】: