有关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实现不同,文件发送的代码可能需要相应做改动。
- 有关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 蓝牙启动,扫描,使能
- eclipse3.3.2以link方式安装lomboz插件
- 设置文件夹访问权限,非文件夹属性
- oracle中delete、truncate、drop的区别
- 非常好用的验证码类 (调用简单)
- 5.19开发者日:逗斗竞技平台介绍
- 有关Android Bluetooth - OBEX OPP文件传送
- VS2003安装心得-拿出来与大家分享
- 启动ArcSDE服务,长时间未响应的解决办法
- C#.net 环境下使用OpenCV(一) 转
- 我是怎样使用BoundsChecker的
- 经典SQL语句大全
- C++中L和_T() 及编码讲解
- linux学习笔记(2)-安装ifort
- IronPython应用之Linux(SSH和SCP,AutoPyTest自动化应用)