蓝牙连接音响问题(android电视)
最近老大让我开发电视的蓝牙,由于android电视的蓝牙不稳定和设计上的各种各样的要求,需要在原有的基础上做一些更改,中间遇到了各种问题,在此总结一下。
我们首先要获取blueToothAdapter的对象
转发:http://www.cnblogs.com/hyylog/p/5796711.html;
mBluetoothAdapter = bluetoothManager.getAdapter();
1、蓝牙的打开:
mBluetoothAdapter.enable();
2、蓝牙关闭:
mBluetoothAdapter.disenable();
3、蓝牙是否可见:
这个地方系统隐藏了其方法,我们就要用万能公式(反射)来实现:
可见:
public static void setDiscoverableTimeout(int timeout) {
        BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
        try {
            Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
            setDiscoverableTimeout.setAccessible(true);
            Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);
            setScanMode.setAccessible(true);
            setDiscoverableTimeout.invoke(adapter, timeout);
            setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
当timeout设置为0的时候为永久可见
关闭:
public static void closeDiscoverableTimeout() {
        BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
        try {
            Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
            setDiscoverableTimeout.setAccessible(true);
            Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);
            setScanMode.setAccessible(true);
setDiscoverableTimeout.invoke(adapter, 1);
            setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE,1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
4、蓝牙搜索,停止:
开启搜索:mBluetoothAdapter.startDiscovery();
停止搜索:mBluetoothAdapter.cancelDiscovery();
5、蓝牙的配对、取消配对:
配对:
static public boolean pair(String strAddr, byte[] strPsw) {
		boolean result = false;
		BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.cancelDiscovery();
if (!bluetoothAdapter.isEnabled()) {
			bluetoothAdapter.enable();
		}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
			try {
				Log.d("mylog", "NOT BOND_BONDED");
				boolean flag1 = ClsUtils.setPin(device.getClass(), device,
						strPsw); 
				boolean flag2 = ClsUtils.createBond(device.getClass(), device);
				// remoteDevice = device;
result = true;
} catch (Exception e) {
				// TODO Auto-generated catch block
Log.d("mylog", "setPiN failed!");
				e.printStackTrace();
			} //
} else {
			Log.d("mylog", "HAS BOND_BONDED");
			try {
				ClsUtils.removeBond(device.getClass(), device);
				// ClsUtils.createBond(device.getClass(), device);
				boolean flag1 = ClsUtils.setPin(device.getClass(), device,
						strPsw);
				boolean flag2 = ClsUtils.createBond(device.getClass(), device);
result = true;
} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.d("mylog", "setPiN failed!");
				e.printStackTrace();
			}
		}
		return result;
	}
取消配对:
static public boolean removeBond(Class<? extends BluetoothDevice> btClass,
			BluetoothDevice btDevice) throws Exception {
		Method removeBondMethod = btClass.getMethod("removeBond");
		Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
		return returnValue.booleanValue();
	}
6、蓝牙的连接:
音响的连接主要是BluetoothA2dp协议,BluetoothHeadset协议主要用于耳机的,但是在实际的操作过程中如果只连接BluetoothA2dp对于广播的接受是不正常的且有时候也是不发声的,所以这两个协议要一起连接
首先要启动这两个协议的服务监听:
private BluetoothProfile.ServiceListener mA2dpProfileListener = new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.A2DP) {
                mBluetoothA2dp = (BluetoothA2dp) proxy;
	               try {
	            	   if(realConnect)
	            		   ClsUtils.connect(mBluetoothA2dp.getClass(),mBluetoothA2dp, mDevice);
	               } catch (Exception e) {
					mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothA2dp);
					e.printStackTrace();
				}
            }
        }
public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.A2DP) {
            	mBluetoothA2dp = null;
            }
        }
    };
/** 
     * @Fields mHeadsetProfileListener : BluetoothHeadset服务监听器 
     */ 
    private BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.HEADSET) {
            	mBluetoothHeadset = (BluetoothHeadset) proxy;
	            try {
	            	if(realConnect)
	            		ClsUtils.connect(mBluetoothHeadset.getClass(),mBluetoothHeadset, mDevice);
				} catch (Exception e) {
					mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
					e.printStackTrace();
				}
            }
        }
        public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.HEADSET) {
            	mBluetoothHeadset = null;
            }
        }
    };
连接:
mBluetoothAdapter.getProfileProxy(mContext, mA2dpProfileListener, BluetoothProfile.A2DP);
	    	mBluetoothAdapter.getProfileProxy(mContext, mHeadsetProfileListener, BluetoothProfile.HEADSET);
断开:
@Override
	public void disconnectBlueTooth(BluetoothDevice device) {
		mDevice = device;
    	if(mBluetoothA2dp!=null){
	    	try {
				ClsUtils.disconnect(mBluetoothA2dp.getClass(),mBluetoothA2dp, device);
				mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothA2dp);
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    	if(mBluetoothHeadset!=null){
		   try {
			   ClsUtils.disconnect(mBluetoothHeadset.getClass(),mBluetoothHeadset, device);
			   mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
	}
其次获得A2dp和headset对象也用上述的监听方法
7、每次打开蓝牙获取已经连接的蓝牙设备:
int a2dp = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP);
		int headset = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
		int health = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEALTH);
		int flag = -1;
		if (a2dp == BluetoothProfile.STATE_CONNECTED) {
			flag = a2dp;
		} 
		else if (headset == BluetoothProfile.STATE_CONNECTED) {
			flag = headset;
		} 
		else if (health == BluetoothProfile.STATE_CONNECTED) {
			flag = health;
		}
if (flag != -1) {
			bluetoothAdapter.getProfileProxy(mcContext, new ServiceListener() {
@Override
				public void onServiceDisconnected(int profile) {
				}
				@Override
				public void onServiceConnected(int profile, BluetoothProfile proxy) {
					List<BluetoothDevice> mDevices = proxy.getConnectedDevices();
				}
			}, flag);
		}else{
			listener.disConnected();
		}
蓝牙连接音响问题(android电视)的更多相关文章
- Android蓝牙连接自动测试工具
		蓝牙连接自动测试工具 1.需求产生 开发不按着需求走都是耍流氓且浪费时间.此工具的需求产生是研发人员在开发产品时涉及到蓝牙驱动和安卓蓝牙两个东西.但是呢,蓝牙不太稳定,那么工作来了.就需要研发人员一边 ... 
- window10 蓝牙怎么连接音响或蓝牙耳机
		window10 蓝牙怎么连接音响或蓝牙耳机 1.在电脑上依次点击win图标右键-->设置,打开系统设置窗口. 2.点击“设备”,在窗口左侧选择“蓝牙”,右侧检查并开启电脑的蓝牙设备开关, 3. ... 
- Android一对多蓝牙连接示例APP
		一对多蓝牙连接示例,基于Google BluetoothChat修改,实现一对多聊天(一个服务端.多个客户端),类似聊天室. 主要功能: 客户端的发出的消息所有终端都能收到(由服务端转发) 客户端之间 ... 
- Android Studio 蓝牙开发实例——基于Android 6.0
		因项目需要做一个Android 的蓝牙app来通过手机蓝牙传输数据以及控制飞行器,在此,我对这段时间里写的蓝牙app的代码进行知识梳理和出现错误的总结. 该应用的Compile Sdk Version ... 
- 基于swift语言iOS8的蓝牙连接(初步)
		看过一些蓝牙App的事例,大体上对蓝牙的连接过程进行了了解.但是开始真正自己写一个小的BLE程序的时候就举步维艰了.那些模棱两可的概念在头脑中瞬间就蒸发了,所以还是决定从最基本的蓝牙连接过程进行.这里 ... 
- iOS关于蓝牙连接的简单介绍与使用
		下面是两台iPhone6连接同一台蓝牙设备的结果: **成功连接**** peripheral: <CBPeripheral: 0x1700f4500, identifier = 50084F6 ... 
- win 10 Hbuilder1.2.1连接Genymotion 调试Android 软件
		这里记录一下 Hbuilder1.2.1连接Genymotion 调试Android 软件 的过程: 步骤一:把Genymotion 的 adb.exe 路径配置到 Hbuilder 中 在 菜单栏 ... 
- IOS蓝牙连接 初步简单封装使用
		最近写一个蓝牙项目 初步实现一下蓝牙设备连接交互,后期继续完善.... 1.连接蓝牙相关操作 BlueToothManger.h // // BlueToothManger.h // SmartRob ... 
- iOS蓝牙连接流程介绍-1
		蓝牙连接流程介绍 1.1-程序员找女朋友流程介绍 0.程序员找女朋友参与者 1.你 2.受害者(女性同胞) (1)她的性格1 性格的特点 (2)她的性格2 分析性格的特点 1.寻找女性 寻尽身边一 ... 
随机推荐
- POJ 2139 Six Degrees of Cowvin Bacon
			水题,Floyd. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ... 
- poj3190区间类贪心+优先队列
			题意:每个奶牛产奶的时间为A到B,每个奶牛产奶时要占用一间房子,问n头奶牛产奶共需要多少房子,并输出每头奶牛用哪间房子 分析:这题就是一个裸的贪心,将奶牛按开始时间进行排序即可,但考虑一下数据范围,我 ... 
- 设计模式笔记之四:MVP+Retrofit+RxJava组合使用
			本博客转自郭霖公众号:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650236866&idx=1&sn=da66 ... 
- 调用图灵机器人API实现Android智能机器人
			非常感谢CSDN博客上的鸿洋哥,他贴出的源码是我所做的工作的基础,鸿洋哥博客链接http://blog.csdn.net/lmj623565791/article/details/38498353 下 ... 
- (简单)  HDU 3308  LCIS,线段树+区间合并。
			Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ... 
- MySQL临时表与派生表(简略版)
			MySQL临时表与派生表 当主查询中包含派生表,或者当select 语句中包含union字句,或者当select语句中包含一个字段的order by 子句(对另一个字段的group by 子句)时,M ... 
- 【转】Linux强大命令 Awk 20分钟入门介绍
			什么是Awk Awk是一种小巧的编程语言及命令行工具.(其名称得自于它的创始人Alfred Aho.Peter Weinberger 和 Brian Kernighan姓氏的首个字母).它非常适合服务 ... 
- $smary模板缓存
			<?php //引入配置文件 $fillname="../cache/testhuancun.html"; //设置一个缓存时间 $time=; //判断如果缓存文件不存在的 ... 
- 使用SSH搭建用户注册登录系统
			[转]http://blog.sina.com.cn/s/blog_a6a6b3cd01017c57.html 什么是SSH? SSH对应 struts spring hibernatestruts ... 
- Spring.NET 的IOC(依赖注入)
			(1) ioc,意思是Inversion of control,(反转控制),控制反转,就是交换控制权的意思.现在一般不使用这个词,一般使用dependency injection(依赖注入).依赖 ... 
