android连接蓝牙耳机,蓝牙耳机厂家:Android蓝牙连接的一些心得
蓝牙耳机厂家最近做一个项目,主要是给蓝牙发送指令的,boss要求能够最快速度的搜索到蓝牙,并且发送数据.
刚开始也遇到很多133,各种断开连接的问题.android蓝牙搜索有两种方式,一种startLeScan,另一种startDiscovery.因为项目要讲究搜索蓝牙的速度,一开始我一直用的startDiscovery,,但是这个搜索的速度有的手机太慢了,有的手机会达到5-6秒,不能满足需求,后面又尝试了startLeScan,发现搜索速度很快,基本上1秒以内都能搜索到需要的蓝牙.但是startLeScan这个方法,搜索到的蓝牙有的机型会拿不到蓝牙的名称,因为项目是根据蓝牙名称来匹配的,这就很尴尬,拿不到名称就匹配不了,搜到了也没用.
蓝牙耳机厂家觉得因为考虑到startLeScan搜到的速度很快,但只是小部分机型拿不到设备名称,所有就尝试了新的方法.先用旧的方法搜索蓝牙,在1000ms以内搜不到想要的蓝牙设备名称,就用startDiscovery方法来搜索.
private BluetoothAdapter mBtAdapter;
public void judgeBluth() {
if (mBtAdapter == null) mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBtAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableIntent);
}else{
checkGPSPermission();
}
}
@Override
public void GPSsure() {
if(mBtAdapter==null){
CommonUtils.showToast(this,"设备上没有发现有蓝牙设备");
return;
}
if(!mBtAdapter.isEnabled()){//开启蓝牙设备
mBtAdapter.enable();
return;
}
if(MyUtils.newInstance().getBluetoothDevice()!=null){
connect(MyUtils.newInstance().getBluetoothDevice());
}else{
mBtAdapter.startLeScan(mLeScanCallback);
mHandler.postDelayed(new Runnable() {//1000ms后没有搜索到,用新方法
@Override
public void run() {
if(!isGet){
mBtAdapter.stopLeScan(mLeScanCallback);
IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, mFilter);
// 注册搜索完时的receiver
IntentFilter mFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, mFilter1);
isNew = true;
mBtAdapter.startDiscovery();
}
}
},1000);
}
}
private volatile boolean isGet = false;
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
if(isGet)return;
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
if((device.getName()!=null&&bean.getBluetooth().equals(device.getName()))||(MyUtils.newInstance().getMac()!=null&&device.getAddress().equals(MyUtils.newInstance().getMac()))){
isGet = true;
mBtAdapter.stopLeScan(mLeScanCallback);
mHandler.post(new Runnable() { @Override public void run() { connect(device); } });
SharedPreferencesUtil.addBluetoothMac(SuccessActivity.this,device.getAddress());
break;
}
}
}
}
};
private boolean isNew = false;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(isGet)return;
String action = intent.getAction();
// 获得已经搜索到的蓝牙设备
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 搜索到的不是已经绑定的蓝牙设备
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
for (int i = 0; i < str.size(); i++) {
UserBean bean = str.get(i);
if((device.getName()!=null&&bean.getBluetooth().equals(device.getName()))||(MyUtils.newInstance().getMac()!=null&&device.getAddress().equals(MyUtils.newInstance().getMac()))){
isGet = true;
handlerSendString("搜到蓝牙新",System.currentTimeMillis());
if(mBtAdapter.isDiscovering()){
mBtAdapter.cancelDiscovery();
}
connect(device);
}
}
}
// 搜索完成
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
if(mBtAdapter==null)return;
if(mBtAdapter.isDiscovering()){
mBtAdapter.cancelDiscovery();//关闭搜索
}
if(!isGet){//未搜索到相关蓝牙
handlerSendString("未搜索到蓝牙",System.currentTimeMillis());
}
}
}
};
public boolean refreshDeviceCache(BluetoothGatt mBluetoothGatt) {
if (mBluetoothGatt != null) {
try {
Method localMethod = mBluetoothGatt.getClass().getMethod( "refresh", new Class[0]);
if (localMethod != null) {
boolean bool = ((Boolean) localMethod.invoke( mBluetoothGatt, new Object[0])).booleanValue();
return bool;
}
} catch (Exception localException) {
}
}
return false;}
搜索到蓝牙后就开始连接蓝牙了,这里连接我刚开始写的时候,出现很多问题,这里就不一一说了,有的时候会出现开始连接之后就直接断开连接,有的时候又连接不上,有的时候又会发现不了服务,所以我在连接断开的情况下尝试了重新连接.
public volatile int isConnect = 0;
private BluetoothGatt gatts;
public void connect(final BluetoothDevice device){
if(isConnect ==1||isConnect==2||isConnect==3){//防止重复连接的情况
return;
}
if(gatts!=null){
isConnect = 0;
gatts.close();
gatts = null;
}
handlerSendString("开始连接",System.currentTimeMillis());
gatts = device.connectGatt(this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
gatt.discoverServices();
handlerSendString("连接成功",System.currentTimeMillis());
isConnect = 1;
} else {
handlerSendString("断开连接"+"--code:"+newState,System.currentTimeMillis());
refreshDeviceCache(gatt);
gatt.close();
gatt=null;
if(isConnect == 2||isConnect ==0){//未发现蓝牙服务,连接直接断开,没有写入数据,重新连接
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
connect(device);
}
},100);
}
isConnect = 0;
}
}
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
isConnect = 2;
handlerSendString("发现服务",System.currentTimeMillis());
List services = gatt.getServices();
boolean is = true;
for (int i = services.size()-1; i >= 0; i--) {
BluetoothGattService service = services.get(i);
if(service.getUuid().toString().equals(MyUtils.service_uuid)){
List characteristics = service.getCharacteristics();
BluetoothGattCharacteristic characteristic = characteristics.get(characteristics.size()-1);
characteristic.setValue(MyUtils.a);
gatt.setCharacteristicNotification(characteristic,false);
handlerSendString("发送指令",System.currentTimeMillis());
gatt.writeCharacteristic(characteristic);
is = false;
break;
}
}
if(is){
handlerSendString("未检测到蓝牙服务",System.currentTimeMillis());
gatt.disconnect();
}
}
@Override
public void onCharacteristicWrite(final BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
isConnect = 3;
if(status == BluetoothGatt.GATT_SUCCESS){//写入成功
handlerSendString("写入成功",System.currentTimeMillis());
}else if (status == BluetoothGatt.GATT_FAILURE){
Log.e("onCharacteristicWrite中", "写入失败");
}else if (status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED){
Log.e("onCharacteristicWrite中", "没权限");
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
gatt.disconnect();
}
},200);
}
});
}
@Override
protected void onDestroy() {
if(isNew)unregisterReceiver(mReceiver);
super.onDestroy();
}
这些就是我这个项目的时候,尝试了多次的之后的代码,还有需要完善的地方希望有大神能多多指教.
最后蓝牙耳机厂家总结了一下,android蓝牙连接需要定位的,有的机型如果没有请求定位权限的话,会出现找不到蓝牙,找不到服务的情况.
android蓝牙连接机型不一样,稳定性也不一样,连接的速度也不一样,弄的我焦头烂额的.
android连接蓝牙耳机,蓝牙耳机厂家:Android蓝牙连接的一些心得相关推荐
- 三星java蓝牙_【三星IconX 2018蓝牙耳机使用总结】蓝牙|连接|操作|传输_摘要频道_什么值得买...
三星IconX 2018蓝牙耳机使用总结(蓝牙|连接|操作|传输) 耳机从入手到写这篇原创,使用了4周时间,各个功能基本都使用到了.首先是连接耳机,如果要实现耳机的全部功能,必须搭配原厂的app--S ...
- ktm390蓝牙连接安卓_车机蓝牙连接常见问题说明
汽车已成为了人民生活中的必需品,大家开车过程中或多或少会碰到汽车娱乐主机使用蓝牙时的闹心问题,从而影响开车的心情,今天小编给老铁们列出几条闹心问题做下说明. 问题1:蓝牙连接时无法播放苹果手机的微信语 ...
- 叮咚音响登录显示未连接服务器,音响唤醒及蓝牙连接 - 叮咚智能音箱联网教程_叮咚智能音箱联不上网解决办法...
2.音箱的误唤醒 若长时间不使用音箱,建议轻按音箱顶部的Zzz休眠键,让音箱进入休眠状态. 3.无法识别 1)用户语速过快,唤醒词"叮咚叮咚"和需要搜索的内容几乎连在一次说的,导致 ...
- 蓝牙聊天App设计3:Android Studio制作蓝牙聊天通讯软件(完结,蓝牙连接聊天,结合生活情景进行蓝牙通信的通俗讲解,以及代码功能实现,内容详细,讲解通俗易懂)
前言:蓝牙聊天App设计全部有三篇文章(一.UI界面设计,二.蓝牙搜索配对连接实现,三.蓝牙连接聊天),这篇文章是:三.蓝牙连接聊天. 课程1:Android Studio小白安装教程,以及第一个An ...
- Android 蓝牙连接(简单简单版)
该博客只是记录,不会详细说明(本人技术有限不会说) 一:声明蓝牙权限和定位权限 <!--蓝牙权限--> <uses-permission android:name="and ...
- Android 检查版本更新服务并下载,BLE蓝牙连接,BLE蓝牙连接1对多及通用工具
https://github.com/inksnow/InksLibrary 引用方法: 1. aar 应用 apply plugin: 'com.android.application' andro ...
- 【Android】Audio音频输出通道切换 - 蓝牙bluetooth、外放
参考: [Android]Audio音频输出通道切换 - 蓝牙.外放 Android Audio 音频输出通道切换 为什么 iOS 或 Android 设备连接蓝牙设备后不能通过蓝牙设备接电话? xq ...
- 【Android】Audio音频输出通道切换 - 蓝牙、外放
手机音频的输出有外放(Speaker).听筒(Telephone Receiver).有线耳机(WiredHeadset).蓝牙音箱(Bluetooth A2DP)等输出设备.在平时,电话免提.插拔耳 ...
- 微信小程序蓝牙连接错误分析及解决
当打开手机蓝牙后去连接蓝牙,会出现连接不上情况,报错10003,此时不管是重启小程序还是重新关闭打开蓝牙,都不能正常连接蓝牙设备.10003是微信蓝牙连接经常碰到的问题,微信官方给出的文档中就简单的描 ...
- iOS 作为Central蓝牙连接外围(上)
今天说一说iOS蓝牙相关的东西,本文背景是公司的蓝牙项目,项目要求是利用手机蓝牙模块与低功耗蓝牙卡进行通信,蓝牙卡信息解析由卡厂商提供,而我们先要做的就是建立手机与蓝牙卡的连接.难点主要集中在与蓝牙卡 ...
最新文章
- 安装好android的adt以后重启eclipse,但是没有创建AVD的图标
- 《计算机导论》微软ibm,3《计算机导论》第1章_认识计算机.pdf
- Spring-context-AnnotationConfigUtils类
- 有哪些朋友圈励志说说短句?
- deeplearning4j的学习
- 内核初始化流程start_kernel
- 【我评】——关于《中國化風格的淺析》
- SQL Server存储过程实例
- Python压缩图片到指定大小
- Unity3D:TCPSocket模块
- centOS下python用ffmpeg将MP3转换成WAV
- 微信服务号运营的八大策略
- 第三章总体均数的估计与假设检验(2)
- 柠檬班Python高级软件测试开发2022年
- 通百艺即无一长——蒂姆 · 哈福德
- Hbase - RIT机制
- DVWA通过攻略之SQL注入
- 人大金仓 过期 更换license
- 崔岩的笔记——动态时间规整算法(Dynamic Time Warping,DTW)
- C#中设计器的控件事件转到逻辑代码
热门文章
- Canal组件简介与vivo账号实践
- 显示省份简称,如内蒙古自治区:内蒙古
- 蓝天采集器Emlog文章远程免登录发布接口插件
- 计算机网络做网线,用网线直接把两台电脑联接,怎样做网线
- <android>音乐频谱显示效果 音乐播放动画 自定义view Visualizer 对接MediaPlayer 声音频率 动画效果
- nvidia control panel is not found drive/nvidia控制面板打不开/win10应用商店无法下载
- 继解决Spring data jpa 批量插入重写saveAll()后遇到符号不兼容问题
- asp.net危险废物管理系统VS开发sqlserver数据库web结构c#编程计算机网页项目
- consul服务注册常见bug
- BAT华为等一线大厂Java工程师必读书单