有关Android Bluetooth - OBEX OPP文件传送

来源:互联网 发布:gas甩棍淘宝 编辑:程序博客网 时间:2024/04/30 03:13

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();
3. 查找新device

  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); 
   b. Receiver的消息处理代码   
    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字             }        }    };

c. 触发新设备的扫描
        if (mBtAdapter.isDiscovering()) {            mBtAdapter.cancelDiscovery();        }        // Request discover from BluetoothAdapter        mBtAdapter.startDiscovery();

4. 选择paired设备,获取其MAC地址,发起文件传送请求

                // 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);  
从Android 2.2源程序中拿
BluetoothShare程序

注:此方法是基于Android 2.2的,由于不是发布的SDK,故后面版本的android有可能因为其OBEX OPP实现不同,文件发送的代码可能需要相应做改动。

	
				
		
原创粉丝点击