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」执行装置切换模式.配 ...
随机推荐
- Vue select默认选中第一个
<td> <select v-model="selectWare"> <option selected="selected" va ...
- C#第十节课
类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thr ...
- 使用Arcgis进行画面(线)并计算大小(长度)。
在使用Arcgis API for JavaScript进行做地图开发的过程中,在地图进行画线.画面是经常使用的功能.本文主要介绍这一功能. 本文适用Arcgis API版本:Arcgis API f ...
- tween.js缓动(补间动画)
一.理解tween.js 如果看到上面的已经理解了,可以跳过下面的部分.下面为对Tween.js的解释 下面就介绍如何使用这个Tween了,首先b.c.d三个参数(即初始值,变化量,持续时间)在缓动开 ...
- Vue.js教程—1.介绍和安装
Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层, 采用自底向上增量开发的设计.Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定 ...
- 转载 - Catalan数(卡特兰数)
出处:http://blog.sina.com.cn/s/blog_6aefe4250101asv5.html 什么是Catalan数 说到Catalan数,就不得不提及Catalan序列,Catal ...
- ExtJs之Ext.ElementLoader.load
稍微书上代码不适合. var btns = Ext.select('input'); 需要更改为: var btns = Ext.select('input', true); 不然报错: [E] Ex ...
- Oracle-表更名、转存数据
--更名 ALTER TABLE T_LOGSRV_SERVICE RENAME TO T_LOGSRV_SERVICE_20170418_BAK; --创建同样的表 ;
- net_->ForwardBackward()的大致梳理
net_->ForwardBackward()方法在net.hpp文件中 Dtype ForwardBackward() { Dtype loss; Forward(&loss); Ba ...
- [Java]对字符串中的每一个单词个数进行统计
这是来自一道电面的题. 单词统计非常easy想到用Map来统计,于是想到了用HashMap. 可是我却没有想到用split来切割单词,想着用遍历字符的方式来推断空格.人家面试官就说了,假设单词之间不止 ...