本文介绍了蓝牙不发送文件到其他设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据已经问,但我没有找到任何解决方案。蓝牙应用程序,我现在用的是 bluetoothShare.class
。
我的源$ C $下将文件发送到目标设备
MainActvity.class:
设置< BluetoothDevice类>设备= btAdapter
.getBondedDevices();
最后弦乐btDeviceName = selected_deviceName;
BluetoothDevice类设备= NULL;
对于(BluetoothDevice类itDevice:设备){
如果(btDeviceName.equals(itDevice.getName())){
设备= itDevice;
}
}
如果(设备!= NULL){
ContentValues值=新ContentValues();
values.put(BluetoothShare.URI,uri.toString());
values.put(BluetoothShare.MIMETYPE,为image / jpeg);
values.put(BluetoothShare.DESTINATION,
device.getAddress());
values.put(BluetoothShare.DIRECTION,
BluetoothShare.DIRECTION_OUTBOUND);
龙TS = System.currentTimeMillis的();
values.put(BluetoothShare.TIMESTAMP,TS);
最后乌里contentUri = getApplicationContext()
.getContentResolver()。插入(
BluetoothShare.CONTENT_URI,价值观);
Log.v(TAG,插入contentUri:+ contentUri
+来设备:+ device.getName());
Toast.makeText(getApplicationContext(),成功,
Toast.LENGTH_LONG).show();
} 其他 {
textStatus
.setText(蓝牙远程设备没有找到);
}
} 其他 {
textStatus.setText(蓝牙未激活);
}
}
其他 {
Toast.makeText(getApplicationContext(),没有设备发现,
Toast.LENGTH_LONG).show();
}
和blueToothShare.class:
包process.bluetooth.sendfile.opp;
进口android.net.Uri;
进口android.provider.BaseColumns;
公共final类BluetoothShare实现BaseColumns {
私人BluetoothShare(){
}
公共静态最后弦乐PERMISSION_ACCESS =android.permission.ACCESS_BLUETOOTH_SHARE;
公共静态最终乌里CONTENT_URI =开放的我们
.parse(内容://com.android.bluetooth.opp/btopp);
公共静态最后弦乐TRANSFER_COMPLETED_ACTION =android.btopp.intent.action.TRANSFER_COMPLETE;
公共静态最后弦乐INCOMING_FILE_CONFIRMATION_REQUEST_ACTION =android.btopp.intent.action.INCOMING_FILE_NOTIFICATION;
公共静态最后弦乐USER_CONFIRMATION_TIMEOUT_ACTION =android.btopp.intent.action.USER_CONFIRMATION_TIMEOUT;
公共静态最后弦乐URI =URI;
公共静态最后弦乐FILENAME_HINT =暗示;
公共静态最后弦乐_DATA =_data;
公共静态最后弦乐MIMETYPE =mime类型;
公共静态最后弦乐方向=方向;
公共静态最后弦乐DESTINATION =目的地;
公共静态最后弦乐能见度=知名度;
公共静态最后弦乐USER_CONFIRMATION =确认;
公共静态最后弦乐状态=状态;
公共静态最后弦乐TOTAL_BYTES =TOTAL_BYTES;
公共静态最后弦乐CURRENT_BYTES =current_bytes;
公共静态最后弦乐TIMESTAMP =时间戳;
公共静态最终诠释DIRECTION_OUTBOUND = 0;
公共静态最终诠释DIRECTION_INBOUND = 1;
公共静态最终诠释USER_CONFIRMATION_PENDING = 0;
公共静态最终诠释USER_CONFIRMATION_CONFIRMED = 1;
公共静态最终诠释USER_CONFIRMATION_AUTO_CONFIRMED = 2;
公共静态最终诠释USER_CONFIRMATION_DENIED = 3;
公共静态最终诠释USER_CONFIRMATION_TIMEOUT = 4;
公共静态最终诠释VISIBILITY_VISIBLE = 0;
公共静态最终诠释VISIBILITY_HIDDEN = 1;
公共静态布尔isStatusInformational(INT状态){
返程(状态> = 100安培;&安培;身份< 200);
}
公共静态布尔isStatusSuspended(INT状态){
返程(状态== STATUS_PENDING);
}
公共静态布尔isStatusSuccess(INT状态){
返程(状态> = 200安培;&安培;身份< 300);
}
公共静态布尔isStatusError(INT状态){
返程(状态> = 400安培;&安培;身份< 600);
}
公共静态布尔isStatusClientError(INT状态){
返程(状态> = 400安培;&安培;身份< 500);
}
公共静态布尔isStatusServerError(INT状态){
返程(状态> = 500安培;&安培;身份< 600);
}
公共静态布尔isStatusCompleted(INT状态){
返程(状态> = 200安培;&安培;身份< 300)
|| (状态> = 400安培;&安培;身份< 600);
}
公共静态最终诠释STATUS_PENDING = 190;
公共静态最终诠释STATUS_RUNNING = 192;
公共静态最终诠释STATUS_SUCCESS = 200;
公共静态最终诠释STATUS_BAD_REQUEST = 400;
公共静态最终诠释STATUS_FORBIDDEN = 403;
公共静态最终诠释STATUS_NOT_ACCEPTABLE = 406;
公共静态最终诠释STATUS_LENGTH_REQUIRED = 411;
公共静态最终诠释STATUS_ preCONDITION_FAILED = 412;
公共静态最终诠释STATUS_CANCELED = 490;
公共静态最终诠释STATUS_UNKNOWN_ERROR = 491;
公共静态最终诠释STATUS_FILE_ERROR = 492;
公共静态最终诠释STATUS_ERROR_NO_SDCARD = 493;
公共静态最终诠释STATUS_ERROR_SDCARD_FULL = 494;
公共静态最终诠释STATUS_UNHANDLED_OBEX_ code = 495;
公共静态最终诠释STATUS_OBEX_DATA_ERROR = 496;
公共静态最终诠释STATUS_CONNECTION_ERROR = 497;
}
解决方案
BluetoothShare类不支持安卓4.1及以上。您可以使用下面的意图编码在Android版本4.1将文件发送及以上
意向意图=新的意图();
intent.setAction(Intent.ACTION_SEND);
intent.setComponent(新的组件名(
com.android.bluetooth
com.android.bluetooth.opp.BluetoothOppLauncherActivity));
intent.setType(为image / jpeg);
文件=新的文件(文件路径);
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(文件));
startActivity(意向);
以及在Android的v某些设备2.2 / 2.3不能通过bluetoothShare类发送的文件。
It is already asked but i didn't find any solution. For bluetooth application i am using the bluetoothShare.class
.
My source code for sending the file to the target device
MainActvity.class:
Set<BluetoothDevice> devices = btAdapter
.getBondedDevices();
final String btDeviceName = selected_deviceName;
BluetoothDevice device = null;
for (BluetoothDevice itDevice : devices) {
if (btDeviceName.equals(itDevice.getName())) {
device = itDevice;
}
}
if (device != null) {
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, uri.toString());
values.put(BluetoothShare.MIMETYPE, "image/jpeg");
values.put(BluetoothShare.DESTINATION,
device.getAddress());
values.put(BluetoothShare.DIRECTION,
BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
final Uri contentUri = getApplicationContext()
.getContentResolver().insert(
BluetoothShare.CONTENT_URI, values);
Log.v(TAG, "Insert contentUri: " + contentUri
+ " to device: " + device.getName());
Toast.makeText(getApplicationContext(), "Success",
Toast.LENGTH_LONG).show();
} else {
textStatus
.setText("Bluetooth remote device not found");
}
} else {
textStatus.setText("Bluetooth not activated");
}
}
else {
Toast.makeText(getApplicationContext(), "No devices found",
Toast.LENGTH_LONG).show();
}
and the blueToothShare.class:
package process.bluetooth.sendfile.opp;
import android.net.Uri;
import android.provider.BaseColumns;
public final class BluetoothShare implements BaseColumns {
private BluetoothShare() {
}
public static final String PERMISSION_ACCESS = "android.permission.ACCESS_BLUETOOTH_SHARE";
public static final Uri CONTENT_URI = Uri
.parse("content://com.android.bluetooth.opp/btopp");
public static final String TRANSFER_COMPLETED_ACTION = "android.btopp.intent.action.TRANSFER_COMPLETE";
public static final String INCOMING_FILE_CONFIRMATION_REQUEST_ACTION = "android.btopp.intent.action.INCOMING_FILE_NOTIFICATION";
public static final String USER_CONFIRMATION_TIMEOUT_ACTION = "android.btopp.intent.action.USER_CONFIRMATION_TIMEOUT";
public static final String URI = "uri";
public static final String FILENAME_HINT = "hint";
public static final String _DATA = "_data";
public static final String MIMETYPE = "mimetype";
public static final String DIRECTION = "direction";
public static final String DESTINATION = "destination";
public static final String VISIBILITY = "visibility";
public static final String USER_CONFIRMATION = "confirm";
public static final String STATUS = "status";
public static final String TOTAL_BYTES = "total_bytes";
public static final String CURRENT_BYTES = "current_bytes";
public static final String TIMESTAMP = "timestamp";
public static final int DIRECTION_OUTBOUND = 0;
public static final int DIRECTION_INBOUND = 1;
public static final int USER_CONFIRMATION_PENDING = 0;
public static final int USER_CONFIRMATION_CONFIRMED = 1;
public static final int USER_CONFIRMATION_AUTO_CONFIRMED = 2;
public static final int USER_CONFIRMATION_DENIED = 3;
public static final int USER_CONFIRMATION_TIMEOUT = 4;
public static final int VISIBILITY_VISIBLE = 0;
public static final int VISIBILITY_HIDDEN = 1;
public static boolean isStatusInformational(int status) {
return (status >= 100 && status < 200);
}
public static boolean isStatusSuspended(int status) {
return (status == STATUS_PENDING);
}
public static boolean isStatusSuccess(int status) {
return (status >= 200 && status < 300);
}
public static boolean isStatusError(int status) {
return (status >= 400 && status < 600);
}
public static boolean isStatusClientError(int status) {
return (status >= 400 && status < 500);
}
public static boolean isStatusServerError(int status) {
return (status >= 500 && status < 600);
}
public static boolean isStatusCompleted(int status) {
return (status >= 200 && status < 300)
|| (status >= 400 && status < 600);
}
public static final int STATUS_PENDING = 190;
public static final int STATUS_RUNNING = 192;
public static final int STATUS_SUCCESS = 200;
public static final int STATUS_BAD_REQUEST = 400;
public static final int STATUS_FORBIDDEN = 403;
public static final int STATUS_NOT_ACCEPTABLE = 406;
public static final int STATUS_LENGTH_REQUIRED = 411;
public static final int STATUS_PRECONDITION_FAILED = 412;
public static final int STATUS_CANCELED = 490;
public static final int STATUS_UNKNOWN_ERROR = 491;
public static final int STATUS_FILE_ERROR = 492;
public static final int STATUS_ERROR_NO_SDCARD = 493;
public static final int STATUS_ERROR_SDCARD_FULL = 494;
public static final int STATUS_UNHANDLED_OBEX_CODE = 495;
public static final int STATUS_OBEX_DATA_ERROR = 496;
public static final int STATUS_CONNECTION_ERROR = 497;
}
解决方案
BluetoothShare class not supported android 4.1 and above. you can use the following intent coding to send file in android version 4.1 and above
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
intent.setType("image/jpeg");
file = new File(filepath);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
and also some devices in Android v 2.2/2.3 not send the file via bluetoothShare class.
这篇关于蓝牙不发送文件到其他设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!