有没有办法使用Android内部蓝牙将文件发送到其他设备?请提供示例。
/**
* 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);
}
这种方法将使用默认的设备蓝牙功能将文件发送到另一个设备。在执行此操作之前,您必须首先配对设备,这是一种限制。要发送不同类型的文件,只需在设置类型方法中更改MIME类型即可。
在您的清单文件中,您必须添加两个权限,例如:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
Android没有明确的OBEX api,这有些奇怪。无论如何,看一下这个项目:
或者你可以使用这个解决方案
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);