当前位置:   article > 正文

Android-Bluetooth_android bluetooth_service引用那个

android bluetooth_service引用那个

这篇文章将所有的android蓝牙的东西都集合在这里,主要分一下几个部分,所有分析都基于Bluedroid:
(1) Android Bluedroid蓝牙基本框架
(2) 主要目录及文件结构
(3) 蓝牙主要的支持的profile
(4) 蓝牙基本流程分析:蓝牙启动, 蓝牙搜索, 蓝牙连接, 蓝牙传输文件,蓝牙通话和播放音乐

(1) Android Bluedroid蓝牙基本框架

 

bluetooth.jpg

 

根据上图,代码调用过程一般基于下面的流程
APP->Framework---->(通过Binder)BluetoothService-->(通过JNI)调用Native Bluetooth->bluedroid->hci

(2) 模块的主要目录以及文件结构:
APP:
Settings

  1. packages/apps/Settings/src/com/android/settings/bluetooth
  2. 主要文件说明:
  3. BluetoothEnabler.java 界面上蓝牙开启、关闭的开关就是它了,
  4. BluetoothSettings.java 主界面,用于管理配对和连接设备
  5. LocalBluetoothManager.java 提供了蓝牙API上的简单调用接口,这里只是开始。
  6. CachedBluetoothDevice.java 描述蓝牙设备的类,对BluetoothDevice的再封装
  7. BluetoothPairingDialog.java 那个配对提示的对话框

Phone

  1. packages/apps/services/Telecom/src/com/android/server/telecom/BluetoothManager.java
  2. 这里是通话调用蓝牙Audio,
  3. connectBluetoothAudio
  4. disconnectBluetoothAudio

Framework:

  1. /frameworks/base/core/java/android/bluetooth/
  2. BluetoothA2dp.java A2DP的功能实现
  3. BluetoothAdapter.java 蓝牙action的定义,虚拟设备属性以及操作方法
  4. BluetoothAudioGateway.java 蓝牙语音网关
  5. BluetoothClass.java 蓝牙设备类型的定义
  6. BluetoothDevice.java 蓝牙设备属性
  7. BluetoothDevicePicker.java 定义远程蓝牙设备的特性,比如需要认证,设备类型
  8. BluetoothHeadset.java 定义蓝牙headset功能的属性以及接口
  9. BluetoothInputStream.java 蓝牙流接口的实现(输入流)
  10. BluetoothOutputStream.java 蓝牙流接口的实现(输出流)
  11. BluetoothServerSocket.java 蓝牙socket服务端具备的方法
  12. BluetoothSocket.java 蓝牙socket的封装
  13. BluetoothUuid.java 蓝牙uuid的定义以及uuid的解析

(3) 蓝牙主要的支持的profile
HFP/HSP
A2DP
AVRCP
PBAP
DUN
OPP
PAN

(4) 蓝牙基本流程分析:
a. 蓝牙开启:
首先是BluetoothManagerService的启动, 这个服务也是在SystemServer中启动的, 在开机后,SystemServer进程由zygote进程fork出来后,会启动一系列的service,这里面就有BluetoothManagerService:

  1. frameworks/base/services/java/com/android/server/SystemServer.java
  2. private void startOtherServices() {
  3. BluetoothManagerService bluetooth = null;
  4. //初始化一个BluetoothManagerService;
  5. bluetooth = new BluetoothManagerService(context);
  6. ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);
  7. }
  8. 看一下BluetoothManagerService的构造函数
  9. BluetoothManagerService(Context context) {
  10. loadStoredNameAndAddress(); //读取蓝牙打开默认名称和地址
  11. if (isBluetoothPersistedStateOn()) { //判断蓝牙是否打开
  12. mEnableExternal = true; //如果蓝牙打开,这个设为true,等boot完后,开启enable 蓝牙的过程
  13. }
  14. }
  15. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  16. else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
  17. synchronized(mReceiver) {
  18. if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
  19. //Enable
  20. if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
  21. sendEnableMsg(mQuietEnableExternal);
  22. }
  23. }
  24. }
  25. 广播中接收Boot是否完成, 完成后调用sendEnable来开启蓝牙,mEnableExternal这个值就在这里用到了。

b. 点击蓝牙开关开启蓝牙流程
Settings菜单点击菜单开启蓝牙:
APP层路径packages/apps/Settings/src/com/android/settings/bluetooth
BluetoothSettings.java

  1. public void onActivityCreated(Bundle savedInstanceState) {
  2. mSwitchBar = activity.getSwitchBar(); //界面上的swtichbar控件
  3. mBluetoothEnabler = new BluetoothEnabler(activity, mSwitchBar); //传送给BluetoothEnabler, 通过BluetoothEnable控制
  4. mBluetoothEnabler.setupSwitchBar();
  5. }

BluetoothEnabler.java

  1. private final LocalBluetoothAdapter mLocalAdapter; //localBluetoothAdapter
  2. public BlutoothEnable(){ //构造函数,定义了localBluetoothManager和LocalBluetoothAdapter
  3. LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
  4. mLocalAdapter = manager.getBluetoothAdapter();
  5. }
  6. public void resume(Context context) {
  7. // Bluetooth state is not sticky, so set it manually
  8. handleStateChanged(mLocalAdapter.getBluetoothState()); //设置初始状态
  9. mSwitchBar.addOnSwitchChangeListener(this); //设置switch监听
  10. }
  11. public void onSwitchChanged(Switch switchView, boolean isChecked) {
  12. if (mLocalAdapter != null) {
  13. mLocalAdapter.setBluetoothEnabled(isChecked); //
  14. }
  15. }

LocalBluetoothAdapter.java

  1. public void setBluetoothEnabled(boolean enabled) {
  2. private final BluetoothAdapter mAdapter;
  3. boolean success = enabled? mAdapter.enable(): mAdapter.disable(); 调用BluetoothAdapter的enable/disable
  4. }

通过调用BluetoothAdapter的函数,就调用到了framework的接口:
LocalBluetoothAdapter.java

  1. private final IBluetoothManager mManagerService;
  2. public static synchronized BluetoothAdapter getDefaultAdapter() {
  3. IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b); //这边会返回一个managerService的代理
  4. }
  5. public boolean enable() {
  6. return mManagerService.enable(ActivityThread.currentPackageName()); //通过代理调用BluetoothManagerService的enable.
  7. }

BluetoothManagerService

  1. class BluetoothManagerService extends IBluetoothManager.Stub{
  2. private final BluetoothHandler mHandler;
  3. private IBluetooth mBluetooth;
  4. public boolean enable(String callingPackage) {
  5. //这里会获取蓝牙的权限
  6. synchronized(mReceiver) {
  7. //通过发送消息的方式enable
  8. sendEnableMsg(false);
  9. }
  10. }
  11. private void sendEnableMsg(boolean quietMode) {
  12. //这边回去发送消息, quietMode决定打开后是否需要AutoConnect,这边是false
  13. mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
  14. quietMode ? 1 : 0, 0));
  15. }
  16. //Handler是BluetoothManagerService的内部类,这边处理接收的命令
  17. private class BluetoothHandler extends Handler {
  18. public void handleMessage(Message msg) {
  19. case MESSAGE_ENABLE:
  20. //调用handleEnable函数处理
  21. handleEnable(msg.arg1 == 1);
  22. break;
  23. case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
  24. mBluetooth = IBluetooth.Stub.asInterface(service);
  25. break;
  26. }
  27. }
  28. private void handleEnable(boolean quietMode) {
  29. synchronized(mConnection) {
  30. try {
  31. //传进来是false,所以走enable,即开启会重新连接
  32. if (!mQuietEnable) {
  33. //调用mBluetooth的enable,mBluetooth上面有定义,是IBluetooth的代理, server端在package/apps/Bluetooth里面
  34. if(!mBluetooth.enable()) {
  35. }
  36. }
  37. else {
  38. if(!mBluetooth.enableNoAutoConnect()) {
  39. }
  40. }
  41. } catch (RemoteException e) {
  42. Log.e(TAG,"Unable to call enable()",e);
  43. }
  44. }
  45. }
  46. }

之前调用IBluetooth的service端的部分:
AdapterService.java

  1. static { //这里添加了so库,并定义了一些 native函数
  2. System.load("/system/lib/libbluetooth_jni.so");
  3. classInitNative();
  4. }
  5. private static class AdapterServiceBinder extends IBluetooth.Stub {
  6. public boolean enable() {
  7. return service.enable(); //这边又要看这个service,这个service即是 AdapterService
  8. }
  9. }
  10. boolean enable() {
  11. return enable (false);
  12. }
  13. public synchronized boolean enable(boolean quietMode) {
  14. //这边会发送一个消息到状态机AdapaterState
  15. Message m =
  16. mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);
  17. mAdapterStateMachine.sendMessage(m);
  18. }
  19. void processStart() {
  20. //启动蓝牙的部分, 如果是第一次开启的话, 走下面的路径, 继续想state状态机发送AdapterState.STARTED状态
  21. if (!mProfilesStarted && supportedProfileServices.length >0) {
  22. //Startup all profile services
  23. setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON);
  24. }else {
  25. debugLog("processStart() - Profile Services alreay started");
  26. mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.STARTED));
  27. }
  28. }

AdapterState.java 状态机文件,负责跟AdapterService的沟通

  1. private class OffState extends State {
  2. public boolean processMessage(Message msg) {
  3. switch(msg.what) {
  4. case USER_TURN_ON:
  5. adapterService.processStart(); //adpaterService调用processStart函数
  6. break;
  7. }
  8. }

最后,看一下状态机里面的函数

  1. private class PendingCommandState extends State {
  2. public boolean processMessage(Message msg) {
  3. boolean ret = adapterService.enableNative(); //这边就调用到jni的地方了
  4. }

看一下简单的流程图, 大体一致,但也稍微有不同的地方

 

Bluetooth_on.jpg

 

这边只到了JNI,下面JNI如何调用CPP的呢~
这边调用packages/apps/Bluetooth/jni下面的jni文件:
com_android_bluetooth_btservice_AdapterService.cpp

  1. static jboolean enableNative(JNIENv* env, jobject obj){
  2. int ret = sBluetoothInterface->enable();
  3. }

在然后好像真的没有了, 后面进入到hal,调用Bluedroid协议栈了
hardware/libhardware/include/hardware/bluetooth.h

  1. typedef struct{
  2. int(*enable)(void);
  3. }bt_interface_t;

Bluedroid协议栈位于external/bluetooth/bluedroid
bluetooth.c

  1. static int enable(void){
  2. return btif_enable_bluetooth();
  3. }

btif_core.c

  1. bt_status_t btif_enable_bluetooth(void)
  2. {
  3. bte_main_enable();
  4. }

bte_main.c
void bte_main_enable(){
BTE_Init();
GKI_create_task(); //创建GKI task
bte_hci_enable();
GKI_run();
}

c. 蓝牙搜索:
APP测:
BluetoothSettings.java

  1. //蓝牙状态改变监听
  2. public void onBluetoothStateChanged(int bluetoothState) {
  3. super.onBluetoothStateChanged(bluetoothState);
  4. updateContent(bluetoothState);
  5. }
  6. private void updateContent(int bluetoothState) {
  7. case BluetoothAdapter.STATE_ON: //蓝牙开启完成后,开始扫描
  8. if (!mInitialScanStarted) {
  9. startScanning();
  10. }
  11. break;
  12. case BluetoothAdapter.STATE_TURNING_ON: //蓝牙开启中,将scanStart设置为false
  13. mInitialScanStarted = false;
  14. break;
  15. }
  16. 这边的BluetoothAdapter.STATE有4个状态:
  17. STATE_OF = 10
  18. STATE_TURNING_ON = 11;
  19. STATE_ON = 12;
  20. STATE_TURNING_OFF = 13;
  21. private void startScanning() {
  22. mLocalAdapter.startScanning(true);//调用localAdapter的startScanning函数
  23. }

LocalBluetoothAdapter.java

  1. void startScanning(boolean force) {
  2. // If we are playing music, don't scan unless forced.
  3. //这里回去判断a2dp profile,如果正在a2dp存在,则不能扫描
  4. A2dpProfile a2dp = mProfileManager.getA2dpProfile();
  5. if (a2dp != null && a2dp.isA2dpPlaying()) {
  6. return;
  7. }
  8. A2dpSinkProfile a2dpSink = mProfileManager.getA2dpSinkProfile();
  9. if ((a2dpSink != null) && (a2dpSink.isA2dpPlaying())){
  10. return;
  11. }
  12. if (mAdapter.startDiscovery()) { //调用framwork的接口
  13. mLastScan = System.currentTimeMillis();
  14. }
  15. }

Framework:
BluetoothAdapter.java

  1. private IBluetooth mService; //这么快就用到IBluetooth了。。。
  2. BluetoothAdapter(IBluetoothManager managerService) {
  3. try {
  4. //构造函数里直接就获取mService,这个mService跟managerService是一样一样的,是IBluetooth的客户端代理,跟Service(AdapterService)通讯
  5. mService = managerService.registerAdapter(mManagerCallback);
  6. } catch (RemoteException e) {Log.e(TAG, "", e);}
  7. }
  8. public boolean startDiscovery() {
  9. if (mService != null) return mService.startDiscovery(); //开始扫描
  10. }

还是来看一下mService是如何获取的
BluetoothManagerService.java

  1. public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
  2. synchronized(mConnection) {
  3. return mBluetooth; //就是返回BluetoothManagerService的mBluetooth
  4. }
  5. }

Service (Service的目录在packages/apps/bluetooth里面)
AdapterService.java

  1. private static class AdapterServiceBinder extends IBluetooth.Stub {
  2. public boolean startDiscovery() {
  3. AdapterService service = getService();
  4. return service.startDiscovery();
  5. }
  6. }
  7. 这里有个内部类AdapterServiceBinder,是远程通讯的服务端:
  8. 直接调用了AdapterService的startDiscovery函数
  9. boolean startDiscovery() {
  10. enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
  11. "Need BLUETOOTH ADMIN permission");
  12. //do not allow new connections with active multicast
  13. A2dpService a2dpService = A2dpService.getA2dpService();
  14. if (a2dpService != null &&
  15. a2dpService.isMulticastOngoing(null)) {
  16. Log.i(TAG,"A2dp Multicast is Ongoing, ignore discovery");
  17. return false;
  18. }
  19. return startDiscoveryNative();
  20. }
  21. 这边也是简单粗暴的,直接返回startDiscoveryNative(),调用JNI函数

JNI
JNI的文件需要到packages/apps/bluetooth/jni下面,adapterservice对应的jni文件为
com_android_bluetooth_btservice_AdapterService.cpp

  1. static jboolean startDiscoveryNative(JNIEnv* env, jobject obj) {
  2. int ret = sBluetoothInterface->start_discovery();
  3. }

HAL
hal主要看hanrdware/libhardware/include/hardware/bluetooth.h

int (*start_discovery)(void);

//Bluedroid协议栈部分
bluetooth.c

  1. static int statrt_discovery(void){
  2. retun btif_dm_start_discovery();
  3. }

btif_dm.c

  1. 这个函数start device discovery/inquiry
  2. bt_status_t btif_dm_start_discovery(void){
  3. /*find nearby devices*/
  4. BTA_DmSearch(...);
  5. }

d. 蓝牙扫描结果返回
扫描结果反馈到上层则是从协议栈->hal->bluetoothService->APP
external/bluetooth/bluedroid/btif/src/
btif_dm.c

  1. static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
  2. {
  3. case BTA_DM_INQ_RES_EVT:
  4. /* Callback to notify upper layer of device */
  5. //调用注册的callback, device_found_cb
  6. HAL_CBACK(bt_hal_cbacks, device_found_cb,num_properties, properties);
  7. break;
  8. }

HAL
bluetooth.h

  1. //
  2. typedef void (*device_found_callback)(int num_properties,
  3. bt_property_t *properties);

JNI
com_android_bluetooth_btservice_AdapterService.cpp

  1. static void device_found_callback(int num_properties, bt_property_t *properties) {
  2. if (sJniCallbacksObj) {
  3. callbackEnv->CallVoidMethod(sJniCallbacksObj, method_deviceFoundCallback, addr);
  4. }
  5. }

Service

  1. //deviceFoundCallback
  2. RemoteDevices.java
  3. void deviceFoundCallback(byte[] address) {
  4. Intent intent = new Intent(BluetoothDevice.ACTION_FOUND); //send broadcast
  5. intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
  6. intent.putExtra(BluetoothDevice.EXTRA_CLASS,
  7. new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass)));
  8. intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi);
  9. intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName);
  10. mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM);
  11. }
  12. 这里调用发送广播,Action_Found给到上层

APP(Settings)
BluetoothEventManager.java

  1. BluetoothEventManager(LocalBluetoothAdapter adapter,
  2. CachedBluetoothDeviceManager deviceManager, Context context) {
  3. mLocalAdapter = adapter;
  4. mDeviceManager = deviceManager;
  5. mAdapterIntentFilter = new IntentFilter();
  6. mProfileIntentFilter = new IntentFilter();
  7. mHandlerMap = new HashMap<String, Handler>();
  8. mContext = context;
  9. // Bluetooth on/off broadcasts
  10. addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler());
  11. // Discovery broadcasts
  12. addHandler(BluetoothAdapter.ACTION_DISCOVERY_STARTED, new ScanningStateChangedHandler(true));
  13. addHandler(BluetoothAdapter.ACTION_DISCOVERY_FINISHED, new ScanningStateChangedHandler(false));
  14. addHandler(BluetoothDevice.ACTION_FOUND, new DeviceFoundHandler());
  15. addHandler(BluetoothDevice.ACTION_DISAPPEARED, new DeviceDisappearedHandler());
  16. addHandler(BluetoothDevice.ACTION_NAME_CHANGED, new NameChangedHandler());
  17. // Pairing broadcasts
  18. addHandler(BluetoothDevice.ACTION_BOND_STATE_CHANGED, new BondStateChangedHandler());
  19. addHandler(BluetoothDevice.ACTION_PAIRING_CANCEL, new PairingCancelHandler());
  20. // Fine-grained state broadcasts
  21. addHandler(BluetoothDevice.ACTION_CLASS_CHANGED, new ClassChangedHandler());
  22. addHandler(BluetoothDevice.ACTION_UUID, new UuidChangedHandler());
  23. // Dock event broadcasts
  24. addHandler(Intent.ACTION_DOCK_EVENT, new DockEventHandler());
  25. mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter);
  26. setDefaultBtName();
  27. }
  28. 这边注册广播接收器
  29. addHandler(BluetoothDevice.ACTION_FOUND, new DeviceFoundHandler());
  30. private class DeviceFoundHandler implements Handler {
  31. public void onReceive(Context context, Intent intent,
  32. BluetoothDevice device) {
  33. if (cachedDevice == null) {
  34. cachedDevice = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, device);
  35. Log.d(TAG, "DeviceFoundHandler created new CachedBluetoothDevice: "
  36. + cachedDevice);
  37. // callback to UI to create Preference for new device
  38. dispatchDeviceAdded(cachedDevice);
  39. }
  40. }
  41. }
  42. DeviceFoundHandler接收广播,并调用dispatchDeviceAdded(cachedDevice);刷新上层显示
  43. private void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) {
  44. synchronized (mCallbacks) {
  45. for (BluetoothCallback callback : mCallbacks) {
  46. callback.onDeviceAdded(cachedDevice);
  47. }
  48. }
  49. }
  50. 调用callback.onDeviceAdded(cachedDevice); 回调返给上层

ListFragment DeviceListPreferenceFragment.java

  1. public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
  2. 、、、
  3. }
  4. DeviceListPreferenceFragment继承BluetoothCallback接口,重写onDeviceAdded.

蓝牙连接:

蓝牙OPP文件传输:
起点自函数packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppLauncherActivity.java

  1. if (action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_SEND_MULTIPLE)) {
  2. if (!isBluetoothAllowed()) {
  3. Intent in = new Intent(this, BluetoothOppBtErrorActivity.class);
  4. in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  5. in.putExtra("title", this.getString(R.string.airplane_error_title));
  6. in.putExtra("content", this.getString(R.string.airplane_error_msg));
  7. startActivity(in);
  8. finish();
  9. return;
  10. }
  11. if (action.equals(Intent.ACTION_SEND)) {
  12. Thread t = new Thread(new Runnable() {
  13. public void run() {
  14. BluetoothOppManager.getInstance(BluetoothOppLauncherActivity.this)
  15. .saveSendingFileInfo(type,stream.toString(), false);
  16. //Done getting file info..Launch device picker and finish this activity
  17. launchDevicePicker();
  18. finish();
  19. }
  20. });
  21. t.start();
  22. return;
  23. }
  24. }
  25. isBluetoothAllowed()判断Bluetooth是否允许,如果不允许,弹出Error.
  26. 如果返回true,则开启个线程,
  27. launchDevicePicker 加载蓝牙列表
  28. private final void launchDevicePicker() {
  29. if (!BluetoothOppManager.getInstance(this).isEnabled()) {
  30. if (V) Log.v(TAG, "Prepare Enable BT!! ");
  31. Intent in = new Intent(this, BluetoothOppBtEnableActivity.class);
  32. in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  33. startActivity(in);
  34. } else {
  35. if (V) Log.v(TAG, "BT already enabled!! ");
  36. Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
  37. in1.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
  38. in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
  39. in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
  40. BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
  41. in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
  42. Constants.THIS_PACKAGE_NAME);
  43. in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
  44. BluetoothOppReceiver.class.getName());
  45. if (V) {Log.d(TAG,"Launching " +BluetoothDevicePicker.ACTION_LAUNCH );}
  46. startActivity(in1);
  47. }
  48. }
  49. BluetoothOppManager.getInstance(this).isEnabled()判断蓝牙是否打开,如果打开,则跳转到列表

DevicePickerFragment.java

  1. @Override
  2. void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
  3. mLocalAdapter.stopScanning();
  4. LocalBluetoothPreferences.persistSelectedDeviceInPicker(
  5. getActivity(), mSelectedDevice.getAddress());
  6. if ((btPreference.getCachedDevice().getBondState() ==
  7. BluetoothDevice.BOND_BONDED) || !mNeedAuth) {
  8. sendDevicePickedIntent(mSelectedDevice);
  9. finish();
  10. } else {
  11. super.onDevicePreferenceClick(btPreference);
  12. }
  13. }
  14. private void sendDevicePickedIntent(BluetoothDevice device) {
  15. mDeviceSelected = true;
  16. Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
  17. intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
  18. if (mLaunchPackage != null && mLaunchClass != null) {
  19. intent.setClassName(mLaunchPackage, mLaunchClass);
  20. }
  21. getActivity().sendBroadcast(intent);
  22. }
  23. 这边发送一个selectd的广播,搜关键字查询广播接收。

BluetoothOppReceiver.java

  1. public void onReceive(Context context, Intent intent) {
  2. else if (action.equals(BluetoothDevicePicker.ACTION_DEVICE_SELECTED)) {
  3. // Insert transfer session record to database
  4. mOppManager.startTransfer(remoteDevice);
  5. }
  6. }
  7. public void startTransfer(BluetoothDevice device) {
  8. if (V) Log.v(TAG, "Active InsertShareThread number is : " + mInsertShareThreadNum);
  9. InsertShareInfoThread insertThread;
  10. insertThread = new InsertShareInfoThread(device, mMultipleFlag, mMimeTypeOfSendingFile,
  11. mUriOfSendingFile, mNameOfSendingFile, mMimeTypeOfSendingFiles, mUrisOfSendingFiles,
  12. mIsHandoverInitiated);
  13. }
  14. insertThread.start();
  15. }
  16. private class InsertShareInfoThread extends Thread {
  17. public void run() {
  18. insertSingleShare();
  19. }
  20. }
  21. 上面步骤即是接收广播后的操作~最终调用insertSingleShare来操作,后面的步骤略掉。

蓝牙接电话与听音乐:



作者:唐僧不爱洗头_f7b5
链接:https://www.jianshu.com/p/b68b281f9020
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/326550
推荐阅读
相关标签
  

闽ICP备14008679号