参考链接:http://blog.csdn.net/xubin341719/article/details/38584469

1.android 手机的低功耗蓝牙,又称BLE ;BLE在andriod 4.3 以上才支持,又称蓝牙4.0,区别于经典蓝牙,BLE 低功耗,手机是否支持低功耗蓝牙,主要取决于手机硬件,所以使用前,需要先进行判断,是否支持低功耗蓝牙

2.蓝牙的使用,

  1.判断mobile 是否有低功耗蓝牙,返回值boolean

mainActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

  2.获取bluetooth的adapter,adapter为null,证明设备无蓝牙

 mDefaultAdapter = BluetoothAdapter.getDefaultAdapter();

  3.设备是否已经开启蓝牙

mDefaultAdapter.isEnabled()

  4.打开蓝牙,开启蓝牙有两种方式,第一种,通过intent开启,会给用户以提示。

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mainActivity.startActivityForResult(intent, OPEN_BLUETOOTH_REQUEST_CODE); //在activity中获取开启蓝牙的反馈
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
switch (requestCode){
case OpenBluetoothPresenter.OPEN_BLUETOOTH_REQUEST_CODE:
if(requestCode==RESULT_OK){ //result_ok 是activity中的常量,为-1,说明操作成功了
mOpenBluetoothPresenter.scanDevice();
}else{
ToastUtils.showMessage(this,getString(R.string.connect_device_need_open_bluetooth));
} break;
} }

  

  第二种方式,这种方式会直接开启蓝牙,但是不能监听到用户是否真的开启了蓝牙;但是可以通过广播监听蓝牙状态的变化,建议非系统应用,采用第一种开启方式

 mDefaultAdapter.enable();

  5.扫描设备,因为我所使用的低功耗设备,而且只需要扫描低功耗设备,我采用的是startLeScan(),改方法过时了,建议采用bluetoothAdapterScanner.startScan,但是这个函数,要求sdk>23,而我需要支持的18,所以仍采用startLeScan

 mDefaultAdapter.startLeScan(this);

  6.扫描的结果,回调OnLEScan里面,改方法是实现接口:BluetoothAdapter.LeScanCallback

@Override
public void onLeScan (BluetoothDevice device, int rssi, byte[] scanRecord) {
boolean isDeviceFinded = false;
String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0 /*&& deviceName.startsWith("M")*/) {
if(devices==null){
devices = new ArrayList<>();
devices.add(device);
}else{
for (BluetoothDevice bluetoothDevice : devices) {
if(bluetoothDevice.getName().equals(deviceName)){
isDeviceFinded = true;
break;
}
} if(!isDeviceFinded){
devices.add(device);
} openBluetoothView.notifyDeviceList(devices);
}
}
}

  7,停止搜索

 mDefaultAdapter.stopLeScan(OpenBluetoothPresenter.this);

  8.连接设备,连接设备是通过bluetoothdevice的address进行连接,但是连接过程中,还是要进行一系列的判断,进行优化

if (mDefaultAdapter == null || address == null) {
ToastUtils.showMessage(mainActivity,mainActivity.getString(R.string.no_connect_device));
return;
} BluetoothDevice remoteDevice = mDefaultAdapter.getRemoteDevice(address);
if (remoteDevice == null) {
ToastUtils.showMessage(mainActivity,mainActivity.getString(R.string.connect_fail_no_device));
return;
} remoteDevice.connectGatt(mainActivity,false,this);

  9.连接的结果,会返回到BluetoothGattCallback里面,BluetoothGAttCallback是一个抽象类,里面的函数意义,:

/**
* 回调指示,
*
* @param gatt gatt客户端
* @param status 连接或者断开操作的状态,返回是BlutoothGatt 操作是否成功
* @param newState 新连接的状态,断开,或者连接bluetprofile state_connect,state_disconnect
*/
@Override
public void onConnectionStateChange (BluetoothGatt gatt, int status, int newState) {
Log.e(TAG, "onConnectionStateChange: status" + status + " status" + BluetoothGatt.GATT_SUCCESS + newState + ">>" + BluetoothProfile.STATE_CONNECTED);
} /**
* 连接设备的services,characteristrics,desriptors 更新的时候调用
*
* @param gatt gatt客户端
* @param status 连接设备已经被探索成功,是BluetoothGatt.Gatt_successfuly
*/
@Override
public void onServicesDiscovered (BluetoothGatt gatt, int status) {
Log.e(TAG, "onServicesDiscovered: " + status + " sss" + BluetoothGatt.GATT_SUCCESS);
} /**
* 读到特征值得结果
*
* @param gatt 客户端
* @param characteristic 特征值
* @param status 读的操作完成后,status 是BluetoothGatt.Gatt_Success
*/
@Override
public void onCharacteristicRead (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.e(TAG, "onCharacteristicRead: " + status);
} /**
* 设备的特征值变化的时候,该函数被触发
*
* @param gatt 客户端
* @param characteristic 变换后的特征值
*/
@Override
public void onCharacteristicChanged (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onCharacteristicChanged: 调用了");
}
/**
* 在调用mBluetoothGatt.writeCharacteristic()函数之后,接收该函数操作的结果
* @param gatt 客户端
* @param characteristic 特征值,这个特征值是从远程设备返回的之前写入的值,应用程序应该进行对比,如果不正确,进行操作
* @param status 如果写入成功,这个返回的BluetoothGatt 的Gatt_success
*/
@Override
public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}

  10.remoteDevice connectGatt()会返回一个GAtt对象,gatt connect方法会重新连接一个已经连接过的连接

 BluetoothGatt mBluetoothGatt = remoteDevice.connectGatt(mainActivity, false, this);
//如果与远程设备的连接断开,调用该函数会重新进行连接,如果设备不在连接范围内,
//会等设备出现在连接范围内后,调用一次,进行连接
mBluetoothGatt.connect();

  11.向蓝牙发送指令,通过的是串口服务

 //当write操作完成后,会回调BluetoothGattCallback的onCharacteristicWrite函数,回报操作的结果,
//返回true,写入成功
boolean b = mBluetoothGatt.writeCharacteristic(rxServiceChar);

  12.开启Characteritics特征

 //开启或者关闭指定的特征值,Boolean 参数即为开启或关闭操作,该函数返回值,是操作是否成功
//返回的结果会在BluetoothGattCallback里面的onCharacteriticsChang调用
mBluetoothGatt.setCharacteristicNotification(characteristic,true);

  13.获取远程设备的所有服务

mBluetoothGatt.getServices();

  14.断开连接

public void disconnect(){
if(mDefaultAdapter==null||mBluetoothGatt==null){
Log.d(TAG, "disconnect: mDefaultAdapter或者 mBluetoothGatt为null,无法关闭");
return;
} mBluetoothGatt.disconnect();
}

  15.清除对象

 public void close(){
if(mBluetoothGatt == null){
Log.d(TAG, "close: mBluetoothGatt 位null,已经close");
return;
} mOldBluetoothAddress = null;
mBluetoothGatt.close();
mBluetoothGatt = null;
}

  

android 低功耗蓝牙使用的更多相关文章

  1. 【转】Android低功耗蓝牙应用开发获取的服务UUID

    原文网址:http://blog.csdn.net/zhangjs0322/article/details/39048939 Android低功耗蓝牙应用程序开始时获取到的蓝牙血压计所有服务的UUID ...

  2. Android低功耗蓝牙(BLE)使用详解

    代码地址如下:http://www.demodashi.com/demo/13390.html 与普通蓝牙相比,低功耗蓝牙显著降低了能量消耗,允许Android应用程序与具有更严格电源要求的BLE设备 ...

  3. 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体 ...

  4. Android低功耗蓝牙(BLE)开发的一点感受

    最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...

  5. Android 低功耗蓝牙BLE 开发注意事项

    基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...

  6. Android低功耗蓝牙(蓝牙4.0)——BLE开发(上)

    段时间,公司项目用到了手机APP和蓝牙设备的通讯开发,这里也正好对低功耗蓝牙(蓝牙4.0及以后标准)的开发,做一个总结. 蓝牙技术联盟在2010年6月30号公布了蓝牙4.0标准,4.0标准在蓝牙3.0 ...

  7. Android低功耗蓝牙总结

    这里只列出重点原理内容,更加细节的内容请阅读前面文章 首先要搞清楚一点,我们在 Android 中通过 SDK 获得的蓝牙广播包是经过底层的 SDK 给我们处理过的,是一个长度为 62 的字节数组.这 ...

  8. Android 低功耗蓝牙的多设备连接与数据接收,简单实现

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  9. Android BLE蓝牙详细解读

    代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...

随机推荐

  1. unix网络编程——I/O多路复用之epoll

    1. 基本概念 当程序进行IO时,如果数据尚未准备好,那么IO将处于阻塞状态.当某个进程有多个打开的文件,比如socket,那么其后的所有准备好读写的文件将受到阻塞的影响而不能操作.不借助线程,单一进 ...

  2. 由于MDK5.0A没有STM32F103程序错误 stm32f10x.h(298): error: #67: expected a "}"

    转自:http://blog.163.com/lby147612@126/blog/static/17041045220150130438428/ 由于MDK4.72A没有STM32F030,所以升级 ...

  3. CentOS7 修改 启动级别

    1. centos7 之前应该使用init 的启动脚本 不支持并行 速度比较慢, centos7 开始使用systemd 的模式 提高了开机的性能 所以之前的init 脚本修改 启动级别应该就无效了 ...

  4. HttpURLConnection、HttpClient和Session

    原文地址:http://www.cnblogs.com/kross/p/3615695.html 一直没弄懂Session,cookies什么的登陆验证到底是怎么回事,昨天分别用HttpURLConn ...

  5. [细品java]ThreadLocal源码学习

    ThreadLocal是线程局部变量,其中保存了特定于该线程的值.每个线程都拥有一份独立的副本值,即每个线程修改变量值不影响其他线程该变量的副本值.这些特定于线程的值保存在Thread对象中,当线程终 ...

  6. 笔记:delphi 与 Query

    以下不保存证正确 Query用SQL语言执行过的,没有必要Cancel.Post,因为其会对数据库直接操作:执行Update.Insert.Delete请用SQL语句: 用Table使用对当前记录直接 ...

  7. Lottie开源库实现Android动画效果

    Lottie简介 Lottie是一个支持Android.iOS.React Native,并由Adobe After Effects制作aep格式的动画,然后经由bodymovin插件转化渲染为jso ...

  8. MT【117】立体几何里的一道分类讨论题

    评:最后用到了中间的截面三角形两边之和大于第三边.能不能构成三棱锥时考虑压扁的"降维"打击是常见的方式.

  9. 20165218 《网络对抗技术》Exp0 Kali安装 Week1

    Kali Linux安装 下载 在Kali官网下载Kali Linux 64 Bit版本 打开VM,选择文件->新建虚拟机,一直点击下一步,注意这里选择稍后安装操作系统 版本中找不到Kali,可 ...

  10. mysqlbinlog- 处理二进制日志文件的实用工具 学习笔记

    参考 MySQL 5.5官方简体中文参考手册完美版  8.6 节 调用: shell> mysqlbinlog [option] log-files... mysqlbinlog支持下面选项: ...