Bluetooth OPP Profile
来源:互联网 发布:数据库的连接 编辑:程序博客网 时间:2024/04/30 01:57
用这个profile之前,要确保oppservice启动,在在BluetoothOppProvider的public Uri insert(Uri uri, ContentValues values) 方法中去启动。OPP可用来发送文件,也可用接收文件,这篇日志记录简单的opp发送文件和接收文件的过程。
手机发送文件,其他模块调用ChooserActivity,创建出共享的dialog。选择用蓝牙发送,将启动BluetoothOppLauncherActivity,在搜索完设备选择了一个设备后:
1)调用DevicePickerFragment类里的sendDevicePickedIntent方法,将BluetoothDevicePicker.ACTION_DEVICE_SELECTED广播给BluetoothOppReceiver处理:
// Insert transfer session record to database
mOppManager.startTransfer(remoteDevice);
BluetoothOppManager调用内部类InsertShareInfoThread将发送的文件加入数据库的线程启动。文件加入DB后,将由BluetoothOppProvider向其他APP提供数据。
code: insertSingleShare()
{
final Uri contentUri = mContext.getContentResolver().insert(BluetoothShare.CONTENT_URI,values);
}
监听数据库变化的:
mObserver = new BluetoothShareContentObserver();
getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
注:将文件加入数据库后,在BluetoothOppService的内部类BluetoothShareContentObserver会收到数据改变的事件,调用onChange()-----updateFromProvider()---updatethread;
updatethread里调用insertShare,这函数里面用到BluetoothOppTransfer,然后启动了BluetoothOppTransfer(mTransfer.start();)。
2)BluetoothOppTransfer的start里 Check Bluetooth status, Start handler thread, new a thread to connect to target device.......
(选择设备之后,和蓝牙耳机连接一样,会收到BOND_STATE_CHANGED和Property Changed,调用onPropertyChanged()在bluetootheventloop类中,获取UUID。然后在BluetoothOppTransfer的 mReceiver中处理BluetoothDevice.ACTION_UUID,此操作执行构建opush channel.)
在startConnectSession开始connect to target socket,channel没有创建成功就先doOpushSdp(做opush channel)。连接过程处理完毕,收到RFCOMM_CONNECTED连接后startObexSession。
具体的发送的实现在BluetoothOppObexClientSession实现。
接收文件的过程:
1)BluetoothServerSocket监听其他设备发过来的连接请求(socket监听到请求时,在opprfcommlistener里clientSocket = mBtServerSocket.accept();然后把消息发给bluetoothoppservice),在BluetoothOppService收到 BluetoothOppRfcommListener.MSG_INCOMING_BTOPP_CONNECTION:{createServerSession(transport);}开始一个server session(自动连接,已经授权)。
2)BluetoothServerSocket监听其他设备发过来的连接请求,BluetoothOppReceiver收到ACTION_INCOMING_FILE_CONFIRM,启动BluetoothOppIncomingFileConfirmActivity弹出对话框给用户确认是否接收文件,点击确定按钮:
// Update database
mUpdateValues = new ContentValues();
mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,
BluetoothShare.USER_CONFIRMATION_CONFIRMED);
this.getContentResolver().update(mUri, mUpdateValues, null, null);
数据库有变化时,处理跟发送文件一致,在insertShare,这函数里面用到BluetoothOppTransfer,然后启动了BluetoothOppTransfer(mServerTransfer.start();)。
通过调用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法来获取BluetoothServerSocket(UUID用于客户端与服务器端之间的配对)
调用BluetoothServerSocket的accept()方法监听连接请求,如果收到请求,则返回一个BluetoothSocket实例(此方法为block方法,应置于新线程中)
如果不想在accept其他的连接,则调用BluetoothServerSocket的close()方法释放资源(调用该方法后,之前获得的BluetoothSocket实例并没有close。但由于RFCOMM一个时刻只允许在一条channel中有一个连接,则一般在accept一个连接后,便close掉BluetoothServerSocket).BluetoothOppReceiveFileInfo处理接收文件的东西,包括存储位置(存储于卡上,没有则返回) if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
判断存储空间,文件名是否正确,有重复文件则用chooseUniquefilename()加一个随机数。
- Bluetooth OPP Profile
- Bluetooth Opp发送文件
- android 蓝牙 bluetooth OPP文件传输
- bluetooth profile
- BlueTooth: BlueTooth的Profile
- 有关Android Bluetooth - OBEX OPP文件传送
- Android Bluetooth opp package 学习笔记
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android4.4 bluetooth OPP 之BluetoothOppSendFileInfo
- android -- 蓝牙 bluetooth (四)OPP文件传输
- 有关Android Bluetooth - OBEX OPP文件传送
- 兔子-蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- 有关Android Bluetooth - OBEX OPP文件传送
- android -- 蓝牙 bluetooth (四)OPP文件传输
- android -- 蓝牙 bluetooth (四)OPP文件传输
- 关于Linux系统性能瓶颈定位分析(一),Nginx状态页测试(待续)
- OK6410裸机开发之LED灯
- compile/Link option setting in VC++6
- eclipse 代理设置
- 在Liunx环境下编译android程序
- Bluetooth OPP Profile
- STL中list结构体元素排序
- MIT OS 5. 小结@ Lab1
- android 4.1 "Mobile networks" 选项 不存在
- 找出一个字符串中的最长palindrome子串
- 共享内存映射之mmap()函数详解
- JAVA rmi 远程方法调用
- 文本情感分析中特征的选取与权重计算
- ExtJs 4.*、Guice3、MyBatis3、MySql架构的 弱权限管理框架 一