使用蓝牙 OBEX 对象推送配置文件 (OPP) 发送文件
有没有办法使用 OBEX 使用 android 蓝牙 API 发送文件?
我需要将文件发送到仅支持 OBEX OPP 的打印机。
我可以使用 android 意图 ACTION_SEND 将文件发送到打印机,没有任何问题,但我需要以编程方式发送它。
我可以使用 OBEX OPP UUID (1105) 和方法 createRfcommSocketToServiceRecord() 连接到蓝牙打印机,但是那么我应该遵循 obex 规范使用 OBEX 发送文件.. 它不像将字节写入输出套接字那么简单..
但如果意图 ACTION_SEND 可以处理这个问题,为什么没有任何 api 供开发人员发送文件?
我还检查了一些第三方库,例如 BlueCove,但我仍然没有 让它工作..(Nexus One 和 Galaxy Tab 抛出异常,说本地库 bluecove_armv71 不可用.. LG Optimus One 说 bluecove_armv61 不可用..)
我被卡住了,有什么想法吗?
工作解决方案
对于任何试图将文件发送到蓝牙设备但运气不佳的人,我提供了一个使用内容提供程序的工作解决方案(感谢 KPBird):
获取 java 类BluetoothShare来自此处
然后,以下代码发送SD卡上的文件到蓝牙设备:
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);
更新
有些人在使用上述解决方案时遇到问题,它已在以下设备上进行了测试:
适用于:
- LG Optimus One (Android 2.1)
- HTC Desire (Android 2.2)
- Google Nexus一台(Android 2.2 和 2.3)
- Samsung Galaxy S2 (Android 4.0.3)
- HTC Amaze (Android 2.3.4)
在以下设备上存在问题:
- LG P500 (Android 2.3.3)
- Galaxy TAB P500 (Android 2.2)
- Google Nexus S (Android 4.1. 2)
如果您能够在上面未列出的设备上测试此代码段,请提供您测试的设备的详细信息(品牌、名称、Android 版本等),和测试结果,在下面的评论中,我将更新问题,包括您的数据。
Is there any way to send a file using the android bluetooth API using OBEX?
I need to send a file to a printer that supports OBEX OPP only.
I can send the file using the android intent ACTION_SEND to the printer with no problems, but I'd need to send it programmatically..
I can connect to the bluetooth printer using OBEX OPP UUID (1105) with the method createRfcommSocketToServiceRecord(), but then I should follow obex specifications to send a file using OBEX.. it isn't as simple as writing bytes to the output socket..
But if the intent ACTION_SEND can handle this, why there isn't any api for developers to send the files?
I also checked some third party libs like BlueCove, but I still didn't
get it to work.. (Nexus One & Galaxy Tab throw an exception saying that native library bluecove_armv71 isn't available.. and LG Optimus One says that bluecove_armv61 isn't available..)
i'm stuck, any ideas?
WORKING SOLUTION
For anyone trying to send a file to a bluetooth device with no luck, I provide a working solution using content providers (thanks to KPBird):
Grab the java class BluetoothShare from here
Than, the following code sends a file on the SD card to a bluetooth device:
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);
UPDATE
Some people are experiencing problems with the solution above, it has been tested on the following devices:
Works on:
- LG Optimus One (Android 2.1)
- HTC Desire (Android 2.2)
- Google Nexus One (Android 2.2 and 2.3)
- Samsung Galaxy S2 (Android 4.0.3)
- HTC Amaze (Android 2.3.4)
Has issues on:
- LG P500 (Android 2.3.3)
- Galaxy TAB P500 (Android 2.2)
- Google Nexus S (Android 4.1.2)
If you are able to test this snippet on devices which are not listed above, please provide the details of the devices (brand, name, android version, etc..) with which you tested it, and the test results, in a comment below, I will update the question including your data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我编写了一个示例应用程序来测试提供的答案。不幸的是,它对我不起作用,所以我认为这个问题没有得到完全回答。
I've written a sample application to test the answer provided. Unfortunately it doesn't work for me, so I think this question is not completely answered.
我认为Android对OPP有支持。android/packages/apps/Bluetooth/...../opp/*.java中有一个路径
如果您的版本没有这个,我找到了一个有用的链接,将有助于在 Android 中使用 OBEX OPP。这里需要修改Android框架。
http://i -miss-erin.blogspot.in/2009/10/how-to-have-obex-function-in-android.html
i think there is support in Android for OPP.There is a path in android/packages/apps/Bluetooth/...../opp/*.java
If your version doesnot have this, i found a useful link that will help to use OBEX OPP in android. Android framework modification is needed here.
http://i-miss-erin.blogspot.in/2009/10/how-to-have-obex-function-in-android.html
没有用于访问 OBEX 的公共 API。
关于为什么没有 API - Google 最清楚:)
There are no public APIs for accessing OBEX.
On why there are no APIs - Google knows best :)
有人说它在某些三星手机上运行良好,但在其他三星手机上不起作用。然后我用“BluetoothShare”写了一个简单的测试程序,并让我的朋友借我手机来测试...
因此,我认为有些手机不可以与这些BluetoothShare代码兼容...
Someone said that it works fine in some Samsung phones, but not work in other Samsung phones. Then I used "BluetoothShare" to write a simple test program, and ask my friends to borrow me phone to test...
Therefore, I think that some phones are not compatible with these BluetoothShare codes...