蓝牙 bluetooth (四)OPP文件传输

在前面android — 蓝牙 bluetooth (一) 入门文章结尾中提到了会按四个方面来写这系列的文章,前面已写了蓝牙打开和蓝牙搜索,这次一起来看下蓝牙文件分享的流程,也就是蓝牙应用opp目录下的代码,作为蓝牙最基本的一个功能,这部分的代码在之前的版本中就已经有了,新旧版本代码对比很多类名都是一样的,这一部分新东西不多,写在这里帮助大家梳理下流程吧。

有没有这种感觉,智能手机的普及让我们提高了一点对蓝牙的关注,手机间使用蓝牙互传文件应该是最常用的应用之一,手机与电脑也可以通过蓝牙做同样的事情,大部分笔记本都支持蓝牙功能,本本上蓝牙芯片多数是broadcom的,也有其它厂商(比如东芝)不过数量不多,毕竟broadcom在BT这方面是老大。不过本本上蓝牙一般只支持蓝牙耳机听歌,并没实现对opp的支持,如果体验下手机与电脑的蓝牙文件传输怎么办呢,安装一个叫bluesoleil(中文名好像是千月)软件就可以了,这个软件对蓝牙功能的支持还是比较全的。可能需要卸载本本自带蓝牙驱动。扯淡结束,本文还是要关注手机间蓝牙opp的代码流程,这段的废话也许能帮助你提高下对蓝牙的体验。

蓝牙发送文件时发送端先来到这里packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppLauncherActivity.java,一个没有界面只是提取下文件信息的中转站,源码的注释写的很清楚了,两个分支action.equals(Intent.ACTION_SEND)和action.equals(Intent.ACTION_SEND_MULTIPLE)

if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) { //Check if Bluetooth is available in the beginning instead of at the end if (!isBluetoothAllowed()) { Intent in = new Intent(this, BluetoothOppBtErrorActivity.class); in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); in.putExtra("title", this.getString(R.string.airplane_error_title)); in.putExtra("content", this.getString(R.string.airplane_error_msg)); startActivity(in); finish(); return; } if (action.equals(Intent.ACTION_SEND)) { ……. Thread t = new Thread(new Runnable() { public void run() { BluetoothOppManager.getInstance(BluetoothOppLauncherActivity.this) .saveSendingFileInfo(type,fileUri.toString(), false); //Done getting file info..Launch device picker //and finish this activity launchDevicePicker(); finish(); } }); …… } else if (action.equals(Intent.ACTION_SEND_MULTIPLE)) { ……. }

最前面那个isBluetoothAllowed()会判断是否处于飞行模式,如果是会禁止发送的。在launchDevicePicker()里还会判断蓝牙是否已经打开,就是下面这个条件语句(!BluetoothOppManager.getInstance(this).isEnabled())。如果已经打开了蓝牙,如果蓝牙打开了就进入设备选择界面DeviceListPreferenceFragment(DevicePickerFragment)选择设备,这个跳转过程简单说明下,注意这个new Intent(BluetoothDevicePicker.ACTION_LAUNCH)里字符串,完整定义public static final String ACTION_LAUNCH = "android.bluetooth.devicepicker.action.LAUNCH";路径frameworks/base/core/java/android/bluetooth/BluetoothDevicePicker.java,你会在setting应用的manifest.xml里发现

<activity android:name=".bluetooth.DevicePickerActivity"android:theme="@android:style/Theme.Holo.DialogWhenLarge"android:label="@string/device_picker"android:clearTaskOnLaunch="true"><intent-filter><action android:name="android.bluetooth.devicepicker.action.LAUNCH" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity> 这样目标就指向了DevicePickerActivity,注意此时它的代码路径是packages/apps/Settings/src/com/android/settings/bluetooth/DevicePickerActivity.java,这个类代码很简单,只有一个onCreate并只在里加载了一个布局文件bluetooth_device_picker.xml,就是这个布局文件指明下一站在哪,看下面就知道怎么来到DevicePickerFragment了<fragmentandroid:id="@+id/bluetooth_device_picker_fragment"android:name="com.android.settings.bluetooth.DevicePickerFragment"android:layout_width="match_parent"android:layout_height="0dip"android:layout_weight="1" /> 到了这里,已经可看到配对过的蓝牙列表了,选择其中一个点击会来到这里,里面那个sendDevicePickedIntent是我们关心的,又发了一个广播,去找谁收了广播就好了 void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {mLocalAdapter.stopScanning();LocalBluetoothPreferences.persistSelectedDeviceInPicker(getActivity(), mSelectedDevice.getAddress());if ((btPreference.getCachedDevice().getBondState() ==BluetoothDevice.BOND_BONDED) || !mNeedAuth) {sendDevicePickedIntent(mSelectedDevice);finish();} else {super.onDevicePreferenceClick(btPreference);}}于千万年之中,于千万人之中,在时间无涯的荒野中,

蓝牙 bluetooth (四)OPP文件传输

相关文章:

你感兴趣的文章:

标签云: