BluetoothA2dp蓝牙音箱的连接
1:权限
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2:打开蓝牙
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
finish();
} else if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
}
3:注册广播以及扫描
// 开始扫描
bluetoothAdapter.startDiscovery(); /*** 注册广播**/
IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(broadcastReceiver,filter);
IntentFilter filter2=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(broadcastReceiver,filter2); private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == BluetoothDevice.ACTION_FOUND) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
String s = device.getName() + "*" + device.getAddress();
if(device.getAddress().equals(blueId)){
bluetoothDevice=device;
Log.d(TAG, "onReceive: --空2");
}
datas.add(device.getName() + "*" + device.getAddress());
Log.d(TAG, "onReceive: ---"+device.getName()+"---"+device.getAddress()); }else if(device.getBondState()!=BluetoothDevice.BOND_BONDED){
String s=device.getName()+"*"+device.getAddress();
Log.d(TAG, "onReceive: ---"+device.getName()+"---"+device.getAddress());
if(device.getAddress().equals(blueId)){
bluetoothDevice=device;
Log.d(TAG, "onReceive: --空1");
}
if(datas.indexOf(s)==-1){
datas.add(device.getName()+"*"+device.getAddress());
}
}
}else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
Log.d(TAG, "onReceive: ---搜索完成");
Toast.makeText(MainActivity.this,"搜索完成!",Toast.LENGTH_SHORT).show();
}
}
};
4:配对
/***传入的参数是 需要配对的bluetoothdevice */
public void blePairing(BluetoothDevice bluetoothDevice){ this.bluetoothDevice=bluetoothDevice;
Method m = null;
try {
m = bluetoothDevice.getClass().getDeclaredMethod("createBond",new Class[]{});
m.setAccessible(true);
Boolean originalResult = null;
try {
originalResult = (Boolean) m.invoke(bluetoothDevice);
boolean result = originalResult.booleanValue();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} } catch (NoSuchMethodException e) {
e.printStackTrace();
} }
5:连接
连接 connectA2dp(bluetoothDevice); /**
* 连接 音箱device
* @param bluetoothDevice
*/
private void connectA2dp(BluetoothDevice bluetoothDevice) {
if (bluetoothA2dp == null || bluetoothDevice == null) {
return;
} setPriority(bluetoothDevice, 100);
try {
Method connectMethod = BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class);
connectMethod.invoke(bluetoothA2dp, bluetoothDevice);
} catch (Exception e) {
e.printStackTrace();
}
}
6: 连接需要的BluetoothA2dp对象需要通过监听得到
/***
* blueA2dp 监听
*/
private BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
if(profile == BluetoothProfile.A2DP){
bluetoothA2dp = null;
}
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if(profile == BluetoothProfile.A2DP){
bluetoothA2dp = (BluetoothA2dp) proxy;
}
}
};
7:code
http://pan.baidu.com/s/1bBuYPS
BluetoothA2dp蓝牙音箱的连接的更多相关文章
- win7系统电脑连接小米蓝牙音箱
一.买好蓝牙适配器,插到电脑上. 二.右下角工具栏找到蓝牙图标 三.右键这个图标,选择'显示Bluetooth设备' 四.找到小米蓝牙音箱 'NDZ-030-AA' 五.双击打开它,然后选择'服务'选 ...
- 专业语音芯片MT8516 华为AM08蓝牙音箱
天猫精灵和亚马逊专用的语音芯片哦!联发科! 华为AM08蓝牙音箱 WT51F5161T的8052 微处理器,RC内振12MHz,具有16Kx8 的flash,硬件IIC,SPI,CEC,IR,RTC, ...
- 利用MediaSession发送信息到蓝牙音箱
1.利用MediaSession发送信息到蓝牙音箱,如:播放音乐时接收的歌曲信息,但是每一首歌连续播放时,再次发送的重复信息会被丢弃.则利用MediaSession发现信息时,要保证信息的不重复性. ...
- 一分钟读懂低功耗蓝牙(BLE)连接数据包
一分钟读懂低功耗蓝牙(BLE)连接数据包 1.概述 BLE 连接过程中有三个重要的数据包:SCAN_REQ, SCAN_RSP 和 CONNECT_REQ. SCAN_REQ: 扫描请求,由主设备(M ...
- 蓝牙音箱BluetoothA2dp
package myapplication.com.mybuletooch; import android.support.v7.app.AppCompatActivity; import andro ...
- 低功耗蓝牙BLE之连接事件、连接参数和更新方法
转自:http://blog.csdn.net/zzfenglin/article/details/51304084 连接事件 在一个连接当中,主设备会在每个连接事件里向从设备发送数据包.一个连接事件 ...
- IOS BLE4.0蓝牙和外设连接和收发数据的流程
前言: 苹果在IOS 6系统之后开始支持BLE 4.0,iPhone4s,iPod 5,iPad 3等之后的机型开始内嵌BLE4.0硬件,因此在开发前请先确认你的开发环境符合上述要求,并且苹果在BLE ...
- Android蓝牙2.0连接以及数据接收发送
1.加入权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=&quo ...
- Filco圣手二代双模蓝牙机械键盘连接方法
转自:https://www.cnblogs.com/goldenSky/p/11437780.html 常规方法 确认键盘的电源接通. 同时按下「Ctrl」+「Alt」+「Fn」执行装置切换模式.配 ...
随机推荐
- 【转】虚拟化(一):虚拟化及vmware产品介绍
由于公司最近在做虚拟化监控,因此就需要把虚拟化方面的知识给学习总结一下,对于虚拟化的概念,摘自百度百科,如下: 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机 ...
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理 状压DP + 二进制 + 骚操作
#include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #defin ...
- 国密SSL证书申请免费试用
沃通提供国密SSL证书免费申请试用服务,一次申请可同时签发SM2/RSA双算法证书,试用周期1个月,用于测试国密SM2 SSL证书的运行效果和SM2/RSA双证书部署效果. 试用产品:SM2/RSA双 ...
- ZooKeeper 运维经验
转自:http://www.juvenxu.com/2015/03/20/experiences-on-zookeeper-ops/ ZooKeeper 运维经验 ZooKeeper 是分布式环境下非 ...
- 55.TF/IDF算法
主要知识点: TF/IDF算法介绍 查看es计算_source的过程及各词条的分数 查看一个document是如何被匹配到的 一.算法介绍 relevance score算法,简单来说 ...
- 支持移动触摸的jQuery图片Lightbox插件
简介 这是一款支持移动触摸设备的简洁jQuery图片Lightbox插件.该LightBox插件可以在移动手机和桌面设备中运行,它具有响应式,预加载图片,键盘支持等特点,非常实用.它的特点还有: 响应 ...
- C#学习笔记_04_流程控制
04_流程控制 程序的执行结构: 顺序结构 分支结构 循环结构 可以通过某些特定的控制语句来控制代码的执行结构 分支流程控制 if else 基本语法 可以只有if没有else,但是不能没有if只有e ...
- 0209利用innobackupex进行简单数据库的备份
利用innobackupex进行简单数据库的备份yum install perl-DBIyum install perl-DBD-MySQLyum install perl-Time-HiResyum ...
- JQuery的wrap用法
wrap是包裹元素的作用,比如我想在img外面包裹一个a标签时,可以这样写法: $(function(){ $('img').wrap(function(){ return '<a href=& ...
- TRIZ系列-创新原理-8-重量补偿原理
重量补偿原理的表述例如以下: 1)将某一物体与还有一种提供上升力的物体组合,以补偿其重量:2)通过与环境(利用空气动力,流体动力或其他力等)的相互作用.实现对物体的重量补偿: 重力使得我们能够稳稳的依 ...