有关Android Bluetooth - OBEX OPP文件传送
来源:互联网 发布:python 推荐算法 编辑:程序博客网 时间:2024/04/30 23:47
转自:http://blog.csdn.net/swingseagull/article/details/7594800
Android developer站点相当详细地介绍了bluetooth API的使用,但它没有提到OBEX。事实上,Adroid早已支持OBEX 的文件传输等功能,比如在share文件时,就可以选择通过蓝牙share,其用到的就是OBEX协议,感兴趣的可以下载android的源程序看其如何实现OBEX OPP协议(访问grepcode站点可以很方便地查看下载源码)。
我根据前人通过查看源程序找到的OBEX文件传输方法写了个程序,在android 2.2上没问题。实现过程大致如下:
1. 使能Bluetooth
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
- }
2. 获取可用的device列表(copy自BluetoothChat demo)
- // Get the local Bluetooth adapter
- mBtAdapter = BluetoothAdapter.getDefaultAdapter();
- // Get a set of currently paired devices
- Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
- <pre name="code" class="java"> a. 登记Bluetooth设备发现和查找结束广播receiver
- // Register for broadcasts when a device is discovered
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- this.registerReceiver(mReceiver, filter);
- // Register for broadcasts when discovery has finished
- filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
- this.registerReceiver(mReceiver, filter);
- <pre name="code" class="java"><pre name="code" class="java"> b. Receiver的消息处理代码
- <pre name="code" class="java"> private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- // When discovery finds a device
- if (BluetoothDevice.ACTION_FOUND.equals(action)) {
- // Get the BluetoothDevice object from the Intent
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- // If it's already paired, skip it, because it's been listed already
- if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
- mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
- }
- // When discovery is finished, change the Activity title
- } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
- ...//此处省略500字
- }
- }
- };
- </pre><br>
- c. 触发新设备的扫描
- <pre></pre>
- <pre></pre>
- <pre name="code" class="java"> if (mBtAdapter.isDiscovering()) {
- mBtAdapter.cancelDiscovery();
- }
- // Request discover from BluetoothAdapter
- mBtAdapter.startDiscovery();
- </pre><br>
- <pre></pre>
- <p>4. 选择paired设备,获取其MAC地址,发起文件传送请求</p>
- <p></p>
- <pre name="code" class="java"> // Get the BLuetoothDevice object
- BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
- // Attempt to connect to the device
- String filePath = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/2011-12-27_15-57-43_500.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 = this.getActivity().getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
- Log.d(TAG, "contentUri:"+contentUri);
- </pre>从<a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/android/bluetooth/opp/BluetoothShare.java?av=f">Android 2.2源程序</a>中拿<pre name="code" class="java">BluetoothShare程序</pre><br>
- <strong>注:此方法是基于Android 2.2的,由于不是发布的SDK,故后面版本的android有可能因为其OBEX OPP实现不同,文件发送的代码可能需要相应做改动。</strong>
- <p></p>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre></pre></pre>
1 0
- 有关Android Bluetooth - OBEX OPP文件传送
- 有关Android Bluetooth - OBEX OPP文件传送
- 有关Android Bluetooth - OBEX OPP文件传送
- Bluetooth Opp发送文件
- android 蓝牙 bluetooth OPP文件传输
- Android Bluetooth opp package 学习笔记
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- 笔记之Android Bluetooth OOP 传送文件流程小记
- 有关Android Bluetooth--Bluetooth基础
- 有关Android Bluetooth--Bluetooth基础
- Bluetooth OPP Profile
- android 4.2 bluetooth OPP 蓝牙启动,扫描,使能
- JavaLearning:synchronized同步的两种方式
- 挂载数据盘
- 使用FragmentTabHost+Fragment打造布局
- java解析/生成xml文件
- 我看数据仓库
- 有关Android Bluetooth - OBEX OPP文件传送
- 为RT-Thread实现一个傻瓜式的工程向导工具(三)
- 关于ASCII
- A list of Go projects
- 线段树hdu4893(多校联合)
- plsql启动时无法解析指定的连接标识符
- 在 mac 上使用 Git 和 GitHub 连接
- poj 2420 A Star not a Tree?
- 【布局相关】RelativeLayout用到的一些重要的属性