在Android程序中可以实现自动扫描蓝牙、配对蓝牙、建立数据通道。
蓝牙分不同类型,可以参考(http://gqdy365.iteye.com/admin/blogs/2229304)

可以入下面方法获取蓝牙设备支持的类型:

  1. BluetoothDevice device;
  2. Arrays.toString(device.getUuids());

我的蓝牙音箱支持的类型有:

  1. 0000111e-0000-1000-8000-00805f9b34fb:Handsfree
  2. 0000110b-0000-1000-8000-00805f9b34fb:AudioSink
  3. 0000110e-0000-1000-8000-00805f9b34fb:AVRemoteControl
  4. 00001203-0000-1000-8000-00805f9b34fb:GenericFileTransfer

这篇文字只讨论如何与蓝牙耳机(蓝牙音箱)连接。

蓝牙耳机一般都支持A2DP(蓝牙立体声,用于音乐播放)、HFP协议(通话),参考:http://gqdy365.iteye.com/admin/blogs/2231553

所以下面操作要同时操作A2DP和HFP,两个都连接成功,才算连接成功;

一、A2DP的操作可以分三步:

1、扫描蓝牙设备:

注册并监听广播:

  1. BluetoothAdapter.ACTION_DISCOVERY_STARTED
  2. BluetoothDevice.ACTION_FOUND
  3. BluetoothAdapter.ACTION_DISCOVERY_FINISHED

启动扫描:

  1. BluetoothAdapter.getDefaultAdapter().startDiscovery();

对扫描的结果按类型进行筛选,只保留我们需要的蓝牙耳机:

  1. if(device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
  2. || device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE){
  3. //蓝牙耳机
  4. }

2、配对指定的蓝牙设备:

这个跟配对普通蓝牙一样,方法如下:

  1. public static  boolean createBond(BluetoothDevice btDevice){
  2. boolean result = false;
  3. try{
  4. Method m = btDevice.getClass().getDeclaredMethod("createBond",new Class[]{});
  5. m.setAccessible(true);
  6. Boolean originalResult = (Boolean) m.invoke(btDevice);
  7. result = originalResult.booleanValue();
  8. }catch(Exception ex){
  9. }
  10. return result;
  11. }

等配对完成之后就是要建立数据连接;

3、建立数据连接:

if you SDK between 11 and 16.call a2dp.connectSink(btDevice) or a2dp.connect(btDevice)

  1. private static IBluetoothA2dp getIBluetoothA2dp() {
  2. IBluetoothA2dp ibta = null;
  3. try {
  4. final Class serviceManager = Class.forName("android.os.ServiceManager");
  5. final Method getService = serviceManager.getDeclaredMethod("getService", String.class);
  6. final IBinder iBinder = (IBinder) getService.invoke(null, "bluetooth_a2dp");
  7. final Class iBluetoothA2dp = Class.forName("android.bluetooth.IBluetoothA2dp");
  8. final Class[] declaredClasses = iBluetoothA2dp.getDeclaredClasses();
  9. final Class c = declaredClasses[0];
  10. final Method asInterface = c.getDeclaredMethod("asInterface", IBinder.class);
  11. asInterface.setAccessible(true);
  12. ibta = (IBluetoothA2dp) asInterface.invoke(null, iBinder);
  13. } catch (final Exception e) {
  14. Log.e("Error " + e.getMessage());
  15. }
  16. return ibta;
  17. }

参考:http://stackoverflow.com/questions/8467178/working-around-a2dp-and-hfp-limitations-of-android-pre-honeycomb

如果API大于16需要用如下的方法:

  1. private void initA2dpService(){
  2. //      Intent i = getExplicitIntent(mContext,new Intent(IBluetoothA2dp.class.getName()));//5.0以上系统需要显示intent
  3. //详细参考http://blog.csdn.net/l2show/article/details/47421961
  4. Intent i = new Intent(IBluetoothA2dp.class.getName());
  5. boolean success = mContext.bindService(i, mConnection, Context.BIND_AUTO_CREATE);
  6. if (success) {
  7. } else {
  8. }
  9. }
  10. public ServiceConnection mConnection = new ServiceConnection() {
  11. @Override
  12. public void onServiceConnected(ComponentName name, IBinder service) {
  13. try {
  14. mA2dpService = IBluetoothA2dp.Stub.asInterface(service);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. @Override
  20. public void onServiceDisconnected(ComponentName name) {
  21. // TODO Auto-generated method stub
  22. }
  23. };
  24. public Intent getExplicitIntent(Context context, Intent implicitIntent) {
  25. // Retrieve all services that can match the given intent
  26. PackageManager pm = context.getPackageManager();
  27. List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
  28. // Make sure only one match was found
  29. if (resolveInfo == null || resolveInfo.size() != 1) {
  30. return null;
  31. }
  32. // Get component info and create ComponentName
  33. ResolveInfo serviceInfo = resolveInfo.get(0);
  34. String packageName = serviceInfo.serviceInfo.packageName;
  35. String className = serviceInfo.serviceInfo.name;
  36. ComponentName component = new ComponentName(packageName, className);
  37. // Create a new intent. Use the old one for extras and such reuse
  38. Intent explicitIntent = new Intent(implicitIntent);
  39. // Set the component to be explicit
  40. explicitIntent.setComponent(component);
  41. return explicitIntent;
  42. }

建立连接:mA2dpService.connect(device);

断开连接:mA2dpService.disconnect(device);

参考:http://stackoverflow.com/questions/14705167/how-connect-paired-bluetooth-a2dp-device-on-android-4-2-using-reflection

http://blog.csdn.net/qs_csu/article/details/45114251

二、HFP操作:

下面只针对4.0及以上版本;

1、初始化:

  1. private void initOrCloseBtCheck(boolean init){
  2. if(init){
  3. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  4. mBluetoothAdapter.getProfileProxy(mContext, new ServiceListener() {
  5. public void onServiceConnected(int profile, BluetoothProfile proxy) {
  6. if (profile == BluetoothProfile.HEADSET) {
  7. mBluetoothHeadset = (BluetoothHeadset) proxy;
  8. }
  9. }
  10. public void onServiceDisconnected(int profile) {
  11. if (profile == BluetoothProfile.HEADSET) {
  12. mBluetoothHeadset = null;
  13. }
  14. }
  15. },BluetoothProfile.HEADSET);
  16. }else{
  17. mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET,mBluetoothHeadset);
  18. }
  19. }

建立连接:

  1. Method m = mBluetoothHeadset.getClass().getDeclaredMethod("connect",BluetoothDevice.class);
  2. m.setAccessible(true);
  3. //连接Headset
  4. boolean successHeadset = (Boolean)m.invoke(mBluetoothHeadset, device);

断开连接:

  1. Method m = mBluetoothHeadset.getClass().getDeclaredMethod("disconnect",BluetoothDevice.class);
  2. m.setAccessible(true);
  3. m.invoke(mBluetoothHeadset, device);

三、状态判断:

蓝牙耳机连接成功:

  1. mA2dpService.getConnectionState(device) == BluetoothA2dp.STATE_DISCONNECTED && mBluetoothHeadset.getConnectionState(device) == BluetoothProfile.STATE_DISCONNECTED

断开成功:

    1. (mA2dpService.getConnectionState(device) == BluetoothA2dp.STATE_CONNECTED || mA2dpService.getConnectionState(device) == BluetoothA2dp.STATE_PLAYING)
    2. && mBluetoothHeadset.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED

Android实现主动连接蓝牙耳机的更多相关文章

  1. Android 检测是否连接蓝牙耳机

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  2. Android连接蓝牙耳机播放音乐

    参考: Android实现主动连接蓝牙耳机 具体实现: private static final String TAG = "BluetoothA2DPTest"; private ...

  3. Android怎样监听蓝牙耳机的按键事件

    Android怎样监听蓝牙耳机的按键事件 写在前面: 直接想要代码非常easy,你直接把滚动栏拉到最底端就能够看到.假设想要十分地了解为什么,那就依照我规划的一步一步来理解.下面測试环境以手头上有的「 ...

  4. 71.Android之长连接实现

    转载:http://blog.csdn.net/qq_23547831/article/details/51690047 本文中我们将讲解一下App的长连接实现.一般而言长连接已经是App的标配了,推 ...

  5. Android自己主动化測试之Monkeyrunner用法及实例

    眼下android SDK里自带的现成的測试工具有monkey 和 monkeyrunner两个.大家别看这俩兄弟名字相像,但事实上是完全然全不同的两个工具,应用在不同的測试领域.总的来说,monke ...

  6. Android如何监听蓝牙耳机的按键事件(转)

    源: Android如何监听蓝牙耳机的按键事件 写在前面: 直接想要代码很简单,你直接把滚动条拉到最底端就可以看到.如果想要十分地了解为什么,那就按照我规划的一步一步来理解.以下测试环境以手头上有的「 ...

  7. Android蓝牙A2DP连接实现

    代码地址如下:http://www.demodashi.com/demo/14624.html 开发环境: 开发工具:Androidstudio 适配机型:honor8(Android6.0), 坚果 ...

  8. [转]Android TCP长连接 心跳机制及实现

    背景知识 智能手机上的长连接心跳和在Internet上的长连接心跳有什么不同 Android系统的推送和iOS的推送有什么区别 几种推送的实现方式 协议 1XMPP简介 2 MQTT简介 3移动端消息 ...

  9. Android自己主动化測试解决方式

    如今,已经有大量的Android自己主动化測试架构或工具可供我们使用,当中包含:Activity Instrumentation, MonkeyRunner, Robotium, 以及Robolect ...

随机推荐

  1. hdu5288 OO’s Sequence 二分 多校联合第一场

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  2. android framework 02

    Android底层开发1.安装Ubuntu系统2.Ubuntu配置开发环境: sudo apt-get install git-core gnupg flex bison gperf zip sudo ...

  3. 实用的 Python 包 —— 使用 win32 的剪贴板

    1. usage >> import win32clipboard >> win32clipboard.OpenClipboard() >> win32clipbo ...

  4. Spark MLlib协同过滤算法

    算法说明 协同过滤(Collaborative Filtering,简称CF,WIKI上的定义是:简单来说是利用某个兴趣相投.拥有共同经验之群体的喜好来推荐感兴趣的资讯给使用者,个人透过合作的机制给予 ...

  5. Day5网络流

    算法 无源汇上下界可行流 先强制流过l的流量 从s到每个正权点连流量为l的流量 从每个负权点向t连-l的流量 如果容量为0,则不连边 有源汇上下界最大流 去掉下界 先求出可行流 再求S到T的最大流 有 ...

  6. LuoguP2762 太空飞行计划问题(最大权闭合子图,最小割)

    题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的全部仪器的 ...

  7. Ansible学习记录三:配置文件

    0.配置文件 两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下. ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径.模块 ...

  8. spring bean中的properties元素内的ref和value的区别;* 和 ** 的区别

    spring bean中的properties元素内的ref和value的区别 至于使用哪个是依据你所用的属性类型决定的. <bean id="sqlSessionFactory&qu ...

  9. Windows学习总结(2)——30+ Windows命令提示符快捷键汇总

    即便你平时经常用到 Windows 命令提示符,可能也会对本文将提到的快捷键数量感到惊讶.其实我们可以使用快捷键来简化命令提示符中的选择操作,或对文本进行重复操作,下面我们会列出完整列表. 大家都知道 ...

  10. cocos2d-x 一些3效果的类及创建參数

    CCShaky3D::create(时间,晃动网格大小,晃动范围,Z轴是否晃动); //创建一个3D晃动的效果 CCShakyTiles3D::create(时间,晃动网格大小,晃动范围,Z轴是否晃动 ...