如何以编程方式在android上使用蓝牙发送文件?
Posted
技术标签:
【中文标题】如何以编程方式在android上使用蓝牙发送文件?【英文标题】:How to send file using bluetooth on android programatically? 【发布时间】:2011-06-22 17:24:26 【问题描述】:我需要将文件发送到计算机而不是另一个 android 应用程序。我查看了蓝牙 api,但它只允许作为客户端-服务器连接。就我而言,我不知道计算机上的 UUId 是什么。我需要看obex吗?我以前没用过。所以任何帮助都是有益的。
【问题讨论】:
我不认为 android 支持 obex。你可以看到 3.0 的新蓝牙 api。我认为它支持不安全的蓝牙连接 我需要实现与照片应用程序中的共享选项类似的功能。分享->蓝牙->选择设备并发送 你有吗,你是怎么做到的@Saqib 【参考方案1】:试试这个。 我可以使用此代码发送文件。
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, "file:///sdcard/refresh.txt");
values.put(BluetoothShare.DESTINATION, deviceAddress);
values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
BluetoothShare.java 的代码
import android.provider.BaseColumns;
import android.net.Uri;
/**
* Exposes constants used to interact with the Bluetooth Share manager's content
* provider.
*/
public final class BluetoothShare implements BaseColumns
private BluetoothShare()
/**
* The permission to access the Bluetooth Share Manager
*/
public static final String PERMISSION_ACCESS = "android.permission.ACCESS_BLUETOOTH_SHARE";
/**
* The content:// URI for the data table in the provider
*/
public static final Uri CONTENT_URI = Uri.parse("content://com.android.bluetooth.opp/btopp");
/**
* Broadcast Action: this is sent by the Bluetooth Share component to
* transfer complete. The request detail could be retrieved by app * as _ID
* is specified in the intent's data.
*/
public static final String TRANSFER_COMPLETED_ACTION = "android.btopp.intent.action.TRANSFER_COMPLETE";
/**
* This is sent by the Bluetooth Share component to indicate there is an
* incoming file need user to confirm.
*/
public static final String INCOMING_FILE_CONFIRMATION_REQUEST_ACTION = "android.btopp.intent.action.INCOMING_FILE_NOTIFICATION";
/**
* This is sent by the Bluetooth Share component to indicate there is an
* incoming file request timeout and need update UI.
*/
public static final String USER_CONFIRMATION_TIMEOUT_ACTION = "android.btopp.intent.action.USER_CONFIRMATION_TIMEOUT";
/**
* The name of the column containing the URI of the file being
* sent/received.
*/
public static final String URI = "uri";
/**
* The name of the column containing the filename that the incoming file
* request recommends. When possible, the Bluetooth Share manager will
* attempt to use this filename, or a variation, as the actual name for the
* file.
*/
public static final String FILENAME_HINT = "hint";
/**
* The name of the column containing the filename where the shared file was
* actually stored.
*/
public static final String _DATA = "_data";
/**
* The name of the column containing the MIME type of the shared file.
*/
public static final String MIMETYPE = "mimetype";
/**
* The name of the column containing the direction (Inbound/Outbound) of the
* transfer. See the DIRECTION_* constants for a list of legal values.
*/
public static final String DIRECTION = "direction";
/**
* The name of the column containing Bluetooth Device Address that the
* transfer is associated with.
*/
public static final String DESTINATION = "destination";
/**
* The name of the column containing the flags that controls whether the
* transfer is displayed by the UI. See the VISIBILITY_* constants for a
* list of legal values.
*/
public static final String VISIBILITY = "visibility";
/**
* The name of the column containing the current user confirmation state of
* the transfer. Applications can write to this to confirm the transfer. the
* USER_CONFIRMATION_* constants for a list of legal values.
*/
public static final String USER_CONFIRMATION = "confirm";
/**
* The name of the column containing the current status of the transfer.
* Applications can read this to follow the progress of each download. See
* the STATUS_* constants for a list of legal values.
*/
public static final String STATUS = "status";
/**
* The name of the column containing the total size of the file being
* transferred.
*/
public static final String TOTAL_BYTES = "total_bytes";
/**
* The name of the column containing the size of the part of the file that
* has been transferred so far.
*/
public static final String CURRENT_BYTES = "current_bytes";
/**
* The name of the column containing the timestamp when the transfer is
* initialized.
*/
public static final String TIMESTAMP = "timestamp";
/**
* This transfer is outbound, e.g. share file to other device.
*/
public static final int DIRECTION_OUTBOUND = 0;
/**
* This transfer is inbound, e.g. receive file from other device.
*/
public static final int DIRECTION_INBOUND = 1;
/**
* This transfer is waiting for user confirmation.
*/
public static final int USER_CONFIRMATION_PENDING = 0;
/**
* This transfer is confirmed by user.
*/
public static final int USER_CONFIRMATION_CONFIRMED = 1;
/**
* This transfer is auto-confirmed per previous user confirmation.
*/
public static final int USER_CONFIRMATION_AUTO_CONFIRMED = 2;
/**
* This transfer is denied by user.
*/
public static final int USER_CONFIRMATION_DENIED = 3;
/**
* This transfer is timeout before user action.
*/
public static final int USER_CONFIRMATION_TIMEOUT = 4;
/**
* This transfer is visible and shows in the notifications while in progress
* and after completion.
*/
public static final int VISIBILITY_VISIBLE = 0;
/**
* This transfer doesn't show in the notifications.
*/
public static final int VISIBILITY_HIDDEN = 1;
/**
* Returns whether the status is informational (i.e. 1xx).
*/
public static boolean isStatusInformational(int status)
return (status >= 100 && status < 200);
/**
* Returns whether the transfer is suspended. (i.e. whether the transfer
* won't complete without some action from outside the transfer manager).
*/
public static boolean isStatusSuspended(int status)
return (status == STATUS_PENDING);
/**
* Returns whether the status is a success (i.e. 2xx).
*/
public static boolean isStatusSuccess(int status)
return (status >= 200 && status < 300);
/**
* Returns whether the status is an error (i.e. 4xx or 5xx).
*/
public static boolean isStatusError(int status)
return (status >= 400 && status < 600);
/**
* Returns whether the status is a client error (i.e. 4xx).
*/
public static boolean isStatusClientError(int status)
return (status >= 400 && status < 500);
/**
* Returns whether the status is a server error (i.e. 5xx).
*/
public static boolean isStatusServerError(int status)
return (status >= 500 && status < 600);
/**
* Returns whether the transfer has completed (either with success or
* error).
*/
public static boolean isStatusCompleted(int status)
return (status >= 200 && status < 300) || (status >= 400 && status < 600);
/**
* This transfer hasn't stated yet
*/
public static final int STATUS_PENDING = 190;
/**
* This transfer has started
*/
public static final int STATUS_RUNNING = 192;
/**
* This transfer has successfully completed. Warning: there might be other
* status values that indicate success in the future. Use isSucccess() to
* capture the entire category.
*/
public static final int STATUS_SUCCESS = 200;
/**
* This request couldn't be parsed. This is also used when processing
* requests with unknown/unsupported URI schemes.
*/
public static final int STATUS_BAD_REQUEST = 400;
/**
* This transfer is forbidden by target device.
*/
public static final int STATUS_FORBIDDEN = 403;
/**
* This transfer can't be performed because the content cannot be handled.
*/
public static final int STATUS_NOT_ACCEPTABLE = 406;
/**
* This transfer cannot be performed because the length cannot be determined
* accurately. This is the code for the HTTP error "Length Required", which
* is typically used when making requests that require a content length but
* don't have one, and it is also used in the client when a response is
* received whose length cannot be determined accurately (therefore making
* it impossible to know when a transfer completes).
*/
public static final int STATUS_LENGTH_REQUIRED = 411;
/**
* This transfer was interrupted and cannot be resumed. This is the code for
* the OBEX error "Precondition Failed", and it is also used in situations
* where the client doesn't have an ETag at all.
*/
public static final int STATUS_PRECONDITION_FAILED = 412;
/**
* This transfer was canceled
*/
public static final int STATUS_CANCELED = 490;
/**
* This transfer has completed with an error. Warning: there will be other
* status values that indicate errors in the future. Use isStatusError() to
* capture the entire category.
*/
public static final int STATUS_UNKNOWN_ERROR = 491;
/**
* This transfer couldn't be completed because of a storage issue.
* Typically, that's because the file system is missing or full.
*/
public static final int STATUS_FILE_ERROR = 492;
/**
* This transfer couldn't be completed because of no sdcard.
*/
public static final int STATUS_ERROR_NO_SDCARD = 493;
/**
* This transfer couldn't be completed because of sdcard full.
*/
public static final int STATUS_ERROR_SDCARD_FULL = 494;
/**
* This transfer couldn't be completed because of an unspecified un-handled
* OBEX code.
*/
public static final int STATUS_UNHANDLED_OBEX_CODE = 495;
/**
* This transfer couldn't be completed because of an error receiving or
* processing data at the OBEX level.
*/
public static final int STATUS_OBEX_DATA_ERROR = 496;
/**
* This transfer couldn't be completed because of an error when establishing
* connection.
*/
public static final int STATUS_CONNECTION_ERROR = 497;
【讨论】:
..上面的代码在我的 Android 2.3.6 设备上不起作用..为此我是否需要在另一台设备上运行接收文件..否则它可能会无目的地发送运行任何程序。 我需要您立即回复我的 cmets。我尝试使用上述代码 android 版本 2.2/2.3 将文件共享到启用了蓝牙的笔记本电脑,但似乎无法共享文件。我没有知道到底是什么问题。帮我尽快解决。 此内容 uricontent://com.android.bluetooth.opp/btopp
不适合我。如何编写它
显然对我来说正在尝试发送文件但发生错误。只有当按“重试”才有效。但这来自应用程序之外。安卓 4.0.3。
见this 回答:Android 4.1 及以上版本不支持BluetoothShare 类【参考方案2】:
对于冰淇淋三明治,此代码不起作用,因此您必须使用此代码
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
Intent sharingIntent = new Intent(
android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent
.setComponent(new ComponentName(
"com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(sharingIntent);
else
ContentValues values = new ContentValues();
values.put(BluetoothShare.URI, uri.toString());
Toast.makeText(getBaseContext(), "URi : " + uri,
Toast.LENGTH_LONG).show();
values.put(BluetoothShare.DESTINATION, deviceAddress);
values.put(BluetoothShare.DIRECTION,
BluetoothShare.DIRECTION_OUTBOUND);
Long ts = System.currentTimeMillis();
values.put(BluetoothShare.TIMESTAMP, ts);
getContentResolver().insert(BluetoothShare.CONTENT_URI,
values);
【讨论】:
超级但在冰淇淋三明治中它重定向移动应用程序设置页面但不在冰淇淋三明治下面【参考方案3】:您可以使用 obex 库。貌似android没有提供obex库,不过我解决了,解决办法贴here。
进一步说明(如果您很忙,请从这里开始阅读)
-
我正在尝试创建一个 android 手机遥控器(以及类似于 telnet 服务器的东西),它有助于使用我的旧功能手机远程控制手机。
主要内容:蓝牙FTP客户端
我的第一个计划是让应用检查我的功能手机目录的文件列表。
但我不知道如何连接到我的功能手机的 ftp 服务器。
我在 Google 上搜索了很多关于如何通过蓝牙连接到 ftp 服务器的信息,但我发现 Bluetoorh FTP 服务器使用了OBEX Protocol
。
我在 SO 线程中找到了一份有用的资料(PDF 文件),并研究了 OBEX 连接请求、放置和获取操作。
所以我终于写了一些代码来尝试连接到Bluetooth FTP
服务器。我想给你看,但我弄丢了 :( 代码就像直接将字节序列写入输出流。
我也很难找出是什么 UUID 使应用程序连接为 FTP 客户端。但是我尝试了使用下面的代码检索到的每个 UUID。
String parcels="";
ParcelUuid[] uuids=mBtDevice.getUuids();
int i=0;
for (ParcelUuid p:uuids)
parcels += "UUID UUID" + new Integer(i).toString() + "=UUID.fromString((\"" + p.getUuid().toString() + "\"));\n\n";
++i;
似乎没有什么能让我得到我想要的答案。所以我搜索了更多,发现我不仅应该使用 UUID 00001106-0000-1000-8000-00805f9b34fb 连接到 OBEX FTP 服务器,还应该传输 target header **发送OBEX connect
请求时使用 UUID **F9EC7BC4-953C-11D2-984E-525400DC9E09。
下面的代码展示了如何作为客户端连接到蓝牙 FTP 服务器。
try
mBtSocket = mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001106-0000-1000-8000-00805f9b34fb"));
catch (Exception e)
//e.printStackTrace();
Thread thread=new Thread(new Runnable()
public void run()
UUID uuid=UUID.fromString("F9EC7BC4-953C-11D2-984E-525400DC9E09");
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
byte [] bytes=bb.array();
Operation putOperation=null;
Operation getOperation=null;
try
// connect the socket
mBtSocket.connect();
//I will explain below
mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));
HeaderSet headerset = new HeaderSet();
headerset.setHeader(HeaderSet.TARGET, bytes);
headerset = mSession.connect(headerset);
if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK)
mConnected = true;
else
mSession.disconnect(headerset);
...
然后您现在作为 FTP 客户端连接,并准备好使用 OBEX 操作来发送文件、请求文件、列出目录等。
-
但是我不想等待一个小时将我的命令发送到我的安卓手机。 (如果我像每个轮询方法一样增加轮询频率,那将是低效的。)
如果您很忙,请从这里开始阅读 主要内容:OBEX OPP
由于我上面提到的原因,我贪婪地寻找操纵 OPP 的方法,这是我从 OBEX 文档中发现的。
您可能希望通过蓝牙正常传输文件(无需定义您的协议并仅为它构建新的桌面应用程序)到您的计算机,对吗?然后发送到在您的桌面 Windows 计算机上本地运行的 OBEX OPP 收件箱服务是最佳解决方案。那么我们如何才能连接到 OPP(Obex Object Push)收件箱服务呢?
-
设置 OBEX 库
将
import javax.obex;
添加到您的源代码中。
如果您的编译器不支持 OBEX 库,请从 here 下载源代码并添加到您的项目中。
实施ObexTransport
当你使用它时,你应该向库提供一个实现ObexTransport
的类。它定义了库应该如何发送数据(例如通过 RFCOMM、TCP、...)。一个示例实现是here。这可能会导致一些运行时或编译错误,例如there's no method
。但是您可以通过将方法调用替换为return 4096
等常量而不是return mSocket.getMaxTransmitPacketSize();
来部分解决这些问题,这会超过public int getMaxTransmitPacketSize()
的if
语句。或者您可以尝试使用 reflection 来获取这些方法的运行时。
获取BluetoothSocket
使用mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001105-0000-1000-8000-00805f9b34fb" ));
获取蓝牙套接字并调用connect()
。
创建ClientSession
创建ObexTransport
实现的实例,并创建一个新的ClientSession
,如mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));
。
向您的计算机 OPP 收件箱服务发送 OBEX 连接请求。
HeaderSet headerset = new HeaderSet();
// headerset.setHeader(HeaderSet.COUNT,n);
headerset = mSession.connect(null);
if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK)
mConnected = true;
使用 ClientSession
发送 OBEX put 请求。
protected boolean Put(ClientSession session, byte[] bytes, String as, String type)
// TODO: Implement this method
//byte [] bytes;
String filename=as;
boolean retry=true;
int times=0;
while (retry && times < 4)
Operation putOperation=null;
OutputStream mOutput = null;
//ClientSession mSession = null;
//ArrayUtils.reverse(bytes);
try
// Send a file with meta data to the server
final HeaderSet hs = new HeaderSet();
hs.setHeader(HeaderSet.NAME, filename);
hs.setHeader(HeaderSet.TYPE, type);
hs.setHeader(HeaderSet.LENGTH, new Long((long)bytes.length));
Log.v(TAG,filename);
//Log.v(TAG,type);
Log.v(TAG,bytes.toString());
putOperation = session.put(hs);
mOutput = putOperation.openOutputStream();
mOutput.write(bytes);
mOutput.close();
putOperation.close();
catch (Exception e)
Log.e(TAG, "put failed", e);
retry = true;
times++;
continue;
//e.printStackTrace();
finally
try
if(mOutput!=null)
mOutput.close();
if(putOperation!=null)
putOperation.close();
catch (Exception e)
Log.e(TAG, "put finally" , e);
retry = true;
times++;
continue;
//updateStatus("[CLIENT] Connection Closed");
retry = false;
return true;
return false;
最后,断开连接。
private void FinishBatch(ClientSession mSession) throws IOException
mSession.disconnect(null);
try
Thread.sleep((long)500);
catch (InterruptedException e)
mBtSocket.close();
那么这里是一个包装类。
import android.bluetooth.*;
import android.util.*;
import java.io.*;
import java.util.*;
import javax.obex.*;
public class BluetoothOPPHelper
String address;
BluetoothAdapter mBtadapter;
BluetoothDevice device;
ClientSession session;
BluetoothSocket mBtSocket;
protected final UUID OPPUUID=UUID.fromString(("00001105-0000-1000-8000-00805f9b34fb"));
private String TAG="BluetoothOPPHelper";
public BluetoothOPPHelper(String address)
mBtadapter=BluetoothAdapter.getDefaultAdapter();
device=mBtadapter.getRemoteDevice(address);
try
mBtSocket = device.createRfcommSocketToServiceRecord(OPPUUID);
catch (IOException e)
throw new RuntimeException(e);
public ClientSession StartBatch(int n)
ClientSession mSession = null;
// TODO: Implement this method
boolean retry=true;
int times=0;
while (retry && times < 4)
//BluetoothConnector.BluetoothSocketWrapper bttmp=null;
try
mBtSocket.connect();
//bttmp = (new BluetoothConnector(device,false,BluetoothAdapter.getDefaultAdapter(),Arrays.asList(new UUID[]OPPUUID,OPPUUID, OPPUUID))).connect();//*/ device.createInsecureRfcommSocketToServiceRecord(OPPUUID);
/*if(mBtSocket.isConnected())
mBtSocket.close();
*/
catch (Exception e)
Log.e(TAG, "opp fail sock " + e.getMessage());
retry = true;
times++;
continue;
try
//mBtSocket=bttmp.getUnderlyingSocket();
// mBtSocket.connect();
BluetoothObexTransport mTransport = null;
mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));
HeaderSet headerset = new HeaderSet();
// headerset.setHeader(HeaderSet.COUNT,n);
headerset = mSession.connect(null);
if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK)
boolean mConnected = true;
else
Log.e(TAG, "SEnd by OPP denied;");
mSession.disconnect(headerset);
times++;
continue;
catch (Exception e)
Log.e(TAG, "opp failed;" , e);
retry = true;
times++;
continue;
//e.rintStackTrace();
retry=false;
return mSession;
protected boolean Put(ClientSession session, byte[] bytes, String as, String type)
// TODO: Implement this method
//byte [] bytes;
String filename=as;
boolean retry=true;
int times=0;
while (retry && times < 4)
Operation putOperation=null;
OutputStream mOutput = null;
//ClientSession mSession = null;
//ArrayUtils.reverse(bytes);
try
// Send a file with meta data to the server
final HeaderSet hs = new HeaderSet();
hs.setHeader(HeaderSet.NAME, filename);
hs.setHeader(HeaderSet.TYPE, type);
hs.setHeader(HeaderSet.LENGTH, new Long((long)bytes.length));
Log.v(TAG,filename);
//Log.v(TAG,type);
Log.v(TAG,bytes.toString());
putOperation = session.put(hs);
mOutput = putOperation.openOutputStream();
mOutput.write(bytes);
mOutput.close();
putOperation.close();
catch (Exception e)
Log.e(TAG, "put failed", e);
retry = true;
times++;
continue;
//e.printStackTrace();
finally
try
if(mOutput!=null)
mOutput.close();
if(putOperation!=null)
putOperation.close();
catch (Exception e)
Log.e(TAG, "put finally" , e);
retry = true;
times++;
continue;
//updateStatus("[CLIENT] Connection Closed");
retry = false;
return true;
return false;
protected boolean Put(ClientSession s, OPPBatchInfo info)
return Put(s,info.data,info.as,info.type);
private void FinishBatch(ClientSession mSession) throws IOException
mSession.disconnect(null);
try
Thread.sleep((long)500);
catch (InterruptedException e)
mBtSocket.close();
public boolean flush() throws IOException
if (sendQueue.isEmpty())
return true;
try
Thread.sleep((long)2000);
catch (InterruptedException e)
ClientSession session=StartBatch(sendQueue.size());
if (session == null)
return false;
while (!sendQueue.isEmpty())
if (Put(session, sendQueue.remove()) == false)
Log.e(TAG, "Put failed");
FinishBatch(session);
return true;
Queue<OPPBatchInfo> sendQueue;
public boolean AddTransfer(String as,String mimetype,byte[] data)
return sendQueue.add(new OPPBatchInfo(as,mimetype,data));
class OPPBatchInfo
String as;
String type;
byte[] data;
public OPPBatchInfo(String as,String type,byte[] data)
this.as=as;
this.data=data;
this.type=type;
【讨论】:
我试着查看你的 Github 关于这个。你还在那个 Github 上工作吗?似乎大部分代码都被注释掉了,BluetoothOPPHelper 根本不存在。【参考方案4】:您需要通过 OBEX 实现 FTP。一旦您实施了标准协议和配置文件,您的 Android FTP 实施将与几乎任何蓝牙 FTP 服务器进行互操作。您还需要实施 OPP 以获得最大的互操作性。 OBEX 协议实现起来并不难,规范免费提供。
【讨论】:
【参考方案5】:我知道这个问题很老了,但对于任何必须处理这个问题的人来说:
使用这个库,您可以通过 OBEX 发送文件,并通过 RFCOMM 发送命令: https://github.com/ddibiasi/Funker
一旦连接到您的目标设备,您就可以操作它的文件系统。
以下示例发送文件:
val rxOBEX = RxObex(device)
rxOBEX
.putFile("rubberduck.txt", "text/plain", "oh hi mark".toByteArray(), "example/directory") // Name of file, mimetype, bytes of file, directory
.subscribeBy(
onComplete =
Log.d(TAG, "Succesfully sent a testfile to device")
,
onError = e ->
Log.e(TAG, "Received error!")
)
该库建立在 Rx 之上,因此所有调用都是非阻塞的。
【讨论】:
以上是关于如何以编程方式在android上使用蓝牙发送文件?的主要内容,如果未能解决你的问题,请参考以下文章