有关Android Bluetooth - OBEX OPP文件传送

来源:互联网 发布:python::-1 编辑:程序博客网 时间:2024/04/30 20:02

Android developer站点相当详细地介绍了bluetooth API的使用,但它没有提到OBEX。事实上,Adroid早已支持OBEX 的文件传输等功能,比如在share文件时,就可以选择通过蓝牙share,其用到的就是OBEX协议,感兴趣的可以下载android的源程序看其如何实现OBEX OPP协议(访问grepcode站点可以很方便地查看下载源码)。

我根据前人通过查看源程序找到的OBEX文件传输方法写了个程序,在android 2.2上没问题。实现过程大致如下:

1. 使能Bluetooth

[java] view plaincopy
  1. BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  2.   
  3. if (!mBluetoothAdapter.isEnabled()) {  
  4.   Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  5.   startActivityForResult(enableIntent, REQUEST_ENABLE_BT);  
  6. }  

2. 获取可用的device列表(copy自BluetoothChat demo)

 

[java] view plaincopy
  1. // Get the local Bluetooth adapter  
  2. mBtAdapter = BluetoothAdapter.getDefaultAdapter();  
  3.   
  4. // Get a set of currently paired devices  
  5. Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();  
3. 查找新device

[java] view plaincopy
  1. <pre name="code" class="java">  a. 登记Bluetooth设备发现和查找结束广播receiver  
  2.        // Register for broadcasts when a device is discovered  
  3.         IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  4.         this.registerReceiver(mReceiver, filter);  
  5.   
  6.         // Register for broadcasts when discovery has finished  
  7.         filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);  
  8.         this.registerReceiver(mReceiver, filter);  
  9.   
  10.  <pre name="code" class="java"><pre name="code" class="java">   b. Receiver的消息处理代码  
  11.    <pre name="code" class="java">    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  12.         @Override  
  13.         public void onReceive(Context context, Intent intent) {  
  14.             String action = intent.getAction();  
  15.   
  16.             // When discovery finds a device  
  17.             if (BluetoothDevice.ACTION_FOUND.equals(action)) {  
  18.                 // Get the BluetoothDevice object from the Intent  
  19.                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  20.                 // If it's already paired, skip it, because it's been listed already  
  21.                 if (device.getBondState() != BluetoothDevice.BOND_BONDED) {  
  22.                     mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());  
  23.                 }  
  24.             // When discovery is finished, change the Activity title  
  25.             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {  
  26.     ...//此处省略500字   
  27.             }  
  28.         }  
  29.     };  
  30. </pre><br>  
  31. c. 触发新设备的扫描  
  32. <pre></pre>  
  33. <pre></pre>  
  34. <pre name="code" class="java">        if (mBtAdapter.isDiscovering()) {  
  35.             mBtAdapter.cancelDiscovery();  
  36.         }  
  37.   
  38.         // Request discover from BluetoothAdapter  
  39.         mBtAdapter.startDiscovery();  
  40. </pre><br>  
  41. <pre></pre>  
  42. <p>4. 选择paired设备,获取其MAC地址,发起文件传送请求</p>  
  43. <p></p>  
  44. <pre name="code" class="java">                  // Get the BLuetoothDevice object  
  45.                     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);  
  46.                     // Attempt to connect to the device  
  47.                     String filePath = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/2011-12-27_15-57-43_500.jpg";  
  48.                     ContentValues values = new ContentValues();  
  49.                     values.put(BluetoothShare.URI, Uri.fromFile(new File(filePath)).toString());  
  50.                     values.put(BluetoothShare.DESTINATION, device.getAddress());  
  51.                     values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);  
  52.                     Long ts = System.currentTimeMillis();  
  53.                     values.put(BluetoothShare.TIMESTAMP, ts);  
  54.                     Uri contentUri = this.getActivity().getContentResolver().insert(BluetoothShare.CONTENT_URI, values);  
  55.                         Log.d(TAG, "contentUri:"+contentUri);    
  56. </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>  
  57. <strong>注:此方法是基于Android 2.2的,由于不是发布的SDK,故后面版本的android有可能因为其OBEX OPP实现不同,文件发送的代码可能需要相应做改动。</strong>  
  58. <p></p>  
  59. <pre></pre>  
  60. <pre></pre>  
  61. <pre></pre>  
  62. <pre></pre>  
  63. <pre></pre>  
  64. <pre></pre>  
  65. <pre></pre>  
  66. <pre></pre>  
  67. </pre></pre></pre>  
0 0
原创粉丝点击