如何在Android上以编程方式使用蓝牙发送文件?
我需要将文件发送到计算机而不是另一个 Android 应用程序。我查看了蓝牙 api,但它只允许作为客户端-服务器连接。就我而言,我不知道计算机上的 UUId 是什么。我需要看obex吗?我以前没用过它。所以任何帮助都是有益的。
I need to send file to a computer instead of another android application. I have looked at the bluetooth api, but it only allow connection as client-server. In my case I dont know what UUId would be on the computer. Do I need to look at obex. I haven't used it before. So any help would be benficial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个。
我可以使用此代码发送文件。
BluetoothShare.java代码
Try this.
I can send a file using this code.
Code of BluetoothShare.java
对于冰淇淋三明治,此代码不起作用,因此您必须使用此代码
For Ice Cream Sandwich this code is not working so you have to use this code
您可以使用 obex 库。看来android没有提供obex库,但我解决了问题,解决方案发布此处。
进一步说明(如果您很忙,请从此处开始阅读)
主要内容:蓝牙FTP客户端
我的第一个计划是让应用程序检查我的功能手机目录中的文件列表。
但我不知道如何连接到功能手机的 ftp 服务器。
我在 google 上搜索了很多关于如何通过蓝牙连接到 ftp 服务器的信息,但我只能找到 Bluetoorh FTP 服务器使用
OBEX 协议
。我在 SO 线程中找到了有用的材料(PDF 文件),并研究了 OBEX 连接请求、放置和获取操作。
所以我最终编写了一些尝试连接到
蓝牙FTP
服务器的代码。我想向你展示它们,但我丢失了它:(这些代码就像直接将字节序列写入输出流。我也很难找出 UUID 使应用程序连接为但我尝试了使用下面的代码检索的每个 UUID。
似乎没有什么能让我得到我想要的答案。所以我在谷歌上进行了更多搜索,发现我不仅应该使用 UUID 00001106-0000-1000-8000-00805f9b34fb 来连接到 OBEX FTP 服务器,而且还应该传输目标标头 **发送 OBEX 连接请求时使用 UUID **F9EC7BC4-953C-11D2-984E-525400DC9E09 。
下面的代码显示了如何作为客户端连接到蓝牙 FTP 服务器。
<前><代码>尝试
{
mBtSocket = mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001106-0000-1000-8000-00805f9b34fb"));
}
捕获(异常 e)
{
//e.printStackTrace();
}
线程线程=new Thread(new Runnable() {
公共无效运行()
{
UUID uuid=UUID.fromString("F9EC7BC4-953C-11D2-984E-525400DC9E09");
ByteBuffer bb = ByteBuffer.wrap(新字节[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
byte[] bytes=bb.array();
操作 putOperation=null;
操作 getOperation=null;
尝试
{
// 连接套接字
mBtSocket.connect();
//下面我会解释
mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));
HeaderSet headerset = new HeaderSet();
headerset.setHeader(HeaderSet.TARGET, 字节);
headerset = mSession.connect(headerset);
if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK)
{
mConnected = true;
}
别的
{
mSession.disconnect(头集);
}
...
然后,您现在已作为 FTP 客户端进行连接,并准备好使用 OBEX 操作来发送文件、请求文件、列出目录等。
如果你很忙,请从这里开始阅读
主要内容:OBEX OPP
由于我上面提到的原因,我贪婪地寻找从OBEX文档中发现的操作OPP的方法。
您可能希望正常地通过蓝牙将文件传输到您的计算机(无需定义协议并为其构建新的桌面应用程序),对吧?然后发送到在桌面 Windows 计算机上本机运行的 OBEX OPP 收件箱服务是最佳解决方案。那么我们如何连接OPP(Obex Object Push)收件箱服务呢?
将
import javax.obex;
添加到源代码中。如果您的编译器不支持 OBEX 库,请从 这里。
使用时,您应该向库提供一个实现
ObexTransport
的类。它定义了库应如何发送数据(例如通过 RFCOMM、TCP 等)。示例实现为 在这里。这可能会导致一些运行时或编译错误,例如没有方法
。但您可以通过将方法调用替换为return 4096
等常量来部分修复这些问题,而不是return mSocket.getMaxTransmitPacketSize();
,从而超越if
的注释public int getMaxTransmitPacketSize()
的语句。或者您可以尝试使用反射来运行这些方法。使用 mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001105-0000-1000-8000-00805f9b34fb" )); 获取蓝牙套接字并调用 connect() 。
创建
ObexTransport
实现的实例,并创建一个新的ClientSession
,例如mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));
。将 OBEX 连接请求发送到您的计算机 OPP 收件箱服务。
使用
ClientSession
发送 OBEX put 请求。最后,断开连接。
这是一个包装类。
You can use the obex library. It seemed that android didn't provide the obex library, but I solved the problem and the solution is posted here.
Further Explanation (please start reading from here if you're busy)
Main content :Bluetooth FTP client
My first plan was to make the app check the list of files of my feature phone's directory.
But I didn't know how to connect to my feature phone's ftp server.
I googled a lot about how to connect to a ftp server via bluetooth but I could only find that Bluetoorh FTP server used the
OBEX Protocol
.I found a useful material (PDF file) in a SO thread and studied about OBEX connect requests, put and get operations.
So I finally wrote some codes that tries to connect to the
Bluetooth FTP
server. I want to show them to you, but I lost it :( The codes were like just directly writting byte sequences to the output stream.I also had difficult time finding out what UUID makes the app connect as FTP client. But I tried every UUIDs retrieved using the code below.
Nothing seemed to bring me to the answer I wanted. So I googled more and found out that I not only should I use UUID 00001106-0000-1000-8000-00805f9b34fb to connect to OBEX FTP server, but also shold I transmit target header ** with UUID **F9EC7BC4-953C-11D2-984E-525400DC9E09 when sending
OBEX connect
request.The code below shows how to connect to a bluetooth FTP server as a client.
Then you are now connected as FTP client and are ready to use OBEX operations to send files, request for files, list directories, etc.
Start reading from here if you are busy
Main content: OBEX OPP
For the reason I mentioned above, I greedly searched for ways to manipulate OPP which I discovered from the OBEX documentation.
You may want to transfer files via bluetooth normally (without defining your protocol and building a new desktop application just for it) to your computer, right? Then sending to OBEX OPP inbox service that is running natively on your desktop windows computer is the best solution. So how can we connect to the OPP (Obex Object Push) inbox service?
Add
import javax.obex;
to your source code.If your compiler doesn't support OBEX library, download sources and add to your project from here.
ObexTransport
You should provide a class that implements
ObexTransport
to the library when you use it. It defines how the library should send data (like by RFCOMM, TCP,...). A sample implementation is here. This can cause some runtime or compilation errors such asthere's no method
. But you can partially fix those by replacing the method calls to constants likereturn 4096
instead ofreturn mSocket.getMaxTransmitPacketSize();
, outcommenting theif
statements ofpublic int getMaxTransmitPacketSize()
. Or you can try using reflection to get those methods runtime.BluetoothSocket
Get a bluetooth socket using
mBtDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(" 00001105-0000-1000-8000-00805f9b34fb" ));
And callconnect()
.ClientSession
Create a instance of your
ObexTransport
implementation, and create a newClientSession
likemSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));
.Send OBEX connect request to your computer OPP inbox service.
Send OBEX put requests using the
ClientSession
.Finally, disconnect.
Then here is a wrapper class.
您需要通过 OBEX 实施 FTP。一旦您实现了标准协议和配置文件,您的 Android FTP 实现将与几乎任何蓝牙 FTP 服务器进行互操作。您还需要实施 OPP 以获得最大的互操作性。 OBEX 协议实施起来并不困难,而且规范是免费提供的。
You need to implement FTP over OBEX. Once you implement the standard protocol and profile, your Android FTP implementation will inter-operate with virtually any Bluetooth FTP server. You'll also need to implement OPP for maximum inter-operability. The OBEX protocol is not so difficult to implement and the specs is freely available.
我知道这个问题已经很老了,但对于仍然需要处理这个问题的任何人来说:
使用这个库,您可以通过 OBEX 发送文件并通过 RFCOMM 发送命令:
https://github.com/ddibiasi/Funker
连接到目标设备后,您可以对其进行操作文件系统。
以下示例发送一个文件:
该库基于 Rx 构建,因此所有调用都是非阻塞的。
I know this question is old, but for anyone having to deal with this still:
With this library you can send files via OBEX and commands via RFCOMM:
https://github.com/ddibiasi/Funker
Once connected to your target device, you can manipulate its filesystem.
The following example sends a file:
The library is built on Rx, so all calls are non blocking.