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蓝牙音箱的连接的更多相关文章

  1. win7系统电脑连接小米蓝牙音箱

    一.买好蓝牙适配器,插到电脑上. 二.右下角工具栏找到蓝牙图标 三.右键这个图标,选择'显示Bluetooth设备' 四.找到小米蓝牙音箱 'NDZ-030-AA' 五.双击打开它,然后选择'服务'选 ...

  2. 专业语音芯片MT8516 华为AM08蓝牙音箱

    天猫精灵和亚马逊专用的语音芯片哦!联发科! 华为AM08蓝牙音箱 WT51F5161T的8052 微处理器,RC内振12MHz,具有16Kx8 的flash,硬件IIC,SPI,CEC,IR,RTC, ...

  3. 利用MediaSession发送信息到蓝牙音箱

    1.利用MediaSession发送信息到蓝牙音箱,如:播放音乐时接收的歌曲信息,但是每一首歌连续播放时,再次发送的重复信息会被丢弃.则利用MediaSession发现信息时,要保证信息的不重复性. ...

  4. 一分钟读懂低功耗蓝牙(BLE)连接数据包

    一分钟读懂低功耗蓝牙(BLE)连接数据包 1.概述 BLE 连接过程中有三个重要的数据包:SCAN_REQ, SCAN_RSP 和 CONNECT_REQ. SCAN_REQ: 扫描请求,由主设备(M ...

  5. 蓝牙音箱BluetoothA2dp

    package myapplication.com.mybuletooch; import android.support.v7.app.AppCompatActivity; import andro ...

  6. 低功耗蓝牙BLE之连接事件、连接参数和更新方法

    转自:http://blog.csdn.net/zzfenglin/article/details/51304084 连接事件 在一个连接当中,主设备会在每个连接事件里向从设备发送数据包.一个连接事件 ...

  7. IOS BLE4.0蓝牙和外设连接和收发数据的流程

    前言: 苹果在IOS 6系统之后开始支持BLE 4.0,iPhone4s,iPod 5,iPad 3等之后的机型开始内嵌BLE4.0硬件,因此在开发前请先确认你的开发环境符合上述要求,并且苹果在BLE ...

  8. Android蓝牙2.0连接以及数据接收发送

    1.加入权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=&quo ...

  9. Filco圣手二代双模蓝牙机械键盘连接方法

    转自:https://www.cnblogs.com/goldenSky/p/11437780.html 常规方法 确认键盘的电源接通. 同时按下「Ctrl」+「Alt」+「Fn」执行装置切换模式.配 ...

随机推荐

  1. BZOJ 3036: 绿豆蛙的归宿 期望 + 拓扑排序

    随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿.给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点. 到达每一个顶点时,如果有K条离 ...

  2. Node.js标准的回调函数

    Node.js标准的回调函数:第一个参数代表错误信息,第二个参数代表结果. function (err, data) 当正常读取时,err参数为null,data参数为读取到的String.当读取发生 ...

  3. linux下关闭键盘的重复击键

    https://askubuntu.com/questions/576421/disable-keyboard-repeat-from-command-line You can use the xse ...

  4. 使用VS Code断点调试PHP

    vs code 使用一款杰出的轻量级代码编辑器,其中的插件工具不胜枚举而且还在不断增加.使用 vs code 调试 php 代码更是方便简洁,下面我们来一起看一下. 1. 安装 XDebug 扩展 调 ...

  5. 【codeforces 798B】Mike and strings

    [题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...

  6. 防火墙对FTP的影响

    http://www.5iadmin.com/post/919.html FTP 常用命令 http://www.cnblogs.com/emanlee/archive/2012/07/09/2583 ...

  7. AT+CSMP 设置短消息文本参数

    AT+CSMP 设置短消息文本参数 2009-05-18 12:03 AT+CSMP 设置短消息文本参数(text模式下) 设置:AT+CSMP=<fo>,<vp/scts>, ...

  8. 关于Bubblesort算法

    Java中的经典算法之冒泡排序(Bubble Sort) 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2 ...

  9. 程序错误通常有2种,一种是运行时错误,这种错误ide会抛出异常,我们可以根据异常寻找问题.

    1.程序错误通常有2种,一种是运行时错误,这种错误ide会抛出异常,我们可以根据异常寻找问题. 2.可以根据异常寻找问题. 3.可以根据异常寻找问题

  10. 动态内存管理---new&amp;delete

    动态内存管理 动态对象(堆对象)是程序在执行过程中在动态内存中用new运算符创建的对象. 因为是用户自己用new运算符创建的.因此也要求用户自己用delete运算符释放,即用户必须自己管理动态内存. ...