有关Android Bluetooth - OBEX OPP文件传送

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实现不同,文件发送的代码可能需要相应做改动。









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值