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. mysql中int、bigint、smallint 和 tinyint的区别与长度

    各种整形,总结留作参考. bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 ...

  2. DATEADD()

    定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的 ...

  3. Linux循环

    echo 'for循环' do echo $i done

  4. 一键安装LNMP(适合centos7)

    1.准备工作,下载源码包 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar ...

  5. MYSQL更换密码

    MYSQL5.7以下版本的数据库密码使用的是 mysql这个数据库里的user表的password这个字段, 修改密码只需: 1.update MySQL.user set password=pass ...

  6. MySQL主要命令(4)

    显示数据, 给列区别名: select coL_name as 别名     from table_name;

  7. 洛谷 P1463 [SDOI2005]反素数ant

    P1463 [SDOI2005]反素数ant 题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i< ...

  8. 获取Class对象方式

    在java中,每个class都有一个相应的Class对象,当编写好一个类,编译完成后,在生成的.class文件中,就产生一个Class对象,用来表示这个类的类型信息.获得Class实例的三种方式: 1 ...

  9. Struts简单介绍

    一.在介绍struts之前,先来了解一下什么是MVC框架吧. 1.MVC介绍 MVC全名是Model View Controller.是模型(model)-视图(view)-控制器(controlle ...

  10. 浅谈搜狐云景PAAS平台

    前言: 搜狐云景作为搜狐的paas平台,在2014年5月22日的云计算大会上正式公布了公測.初測,注冊用户必须先申请邀请码參与公測会赠送用户100元电子券,经过实名认证之后会再赠送100电子券.目測能 ...