android 低功耗蓝牙使用
参考链接: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 低功耗蓝牙使用的更多相关文章
- 【转】Android低功耗蓝牙应用开发获取的服务UUID
原文网址:http://blog.csdn.net/zhangjs0322/article/details/39048939 Android低功耗蓝牙应用程序开始时获取到的蓝牙血压计所有服务的UUID ...
- Android低功耗蓝牙(BLE)使用详解
代码地址如下:http://www.demodashi.com/demo/13390.html 与普通蓝牙相比,低功耗蓝牙显著降低了能量消耗,允许Android应用程序与具有更严格电源要求的BLE设备 ...
- 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释
转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体 ...
- Android低功耗蓝牙(BLE)开发的一点感受
最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...
- Android 低功耗蓝牙BLE 开发注意事项
基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...
- Android低功耗蓝牙(蓝牙4.0)——BLE开发(上)
段时间,公司项目用到了手机APP和蓝牙设备的通讯开发,这里也正好对低功耗蓝牙(蓝牙4.0及以后标准)的开发,做一个总结. 蓝牙技术联盟在2010年6月30号公布了蓝牙4.0标准,4.0标准在蓝牙3.0 ...
- Android低功耗蓝牙总结
这里只列出重点原理内容,更加细节的内容请阅读前面文章 首先要搞清楚一点,我们在 Android 中通过 SDK 获得的蓝牙广播包是经过底层的 SDK 给我们处理过的,是一个长度为 62 的字节数组.这 ...
- Android 低功耗蓝牙的多设备连接与数据接收,简单实现
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- Android BLE蓝牙详细解读
代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...
随机推荐
- CSS+JS笔记
CSS篇: 1.a标签去掉下划线 a { text-decoration:none; }
- LeetCode题解:(114) Flatten Binary Tree to Linked List
题目说明 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...
- exFAT移动硬盘写保护怎么去掉
cmd命令提示符下运行chkdsk命令: 比如在E盘,则输入的命令如下: E:(冒号不可少,输入后回车) CHKDSK /F /X (回车) 等命令执行完了,即可去掉exFAT移动硬盘写的保护.
- BugScan插件编写高(gǎo)级(jī)教程
声明:本文最先发布在:http://q.bugscan.net/t/353 转载请注明出处 有问题可以和我交流 邮件(Medici.Yan@gmail.com) 个人博客地址:http://www.c ...
- 浅析Web数据存储-Cookie、UserData、SessionStorage、WebSqlDatabase
Cookie 它是标准的客户端浏览器状态保存方式,可能在浏览器诞生不久就有Cookie了,为什么需要Cookie 这个东东?由于HTTP协议没有状态,所以需要一个标志/存储来记录客户浏览器当前的状态, ...
- Java线程池(一):初识
1.什么是线程池? 简单粗暴的理解就是:装着一个或多个线程的容器,我们称这个容器为线程池. 在现实世界中,有着各种各样的“池”,例如游泳池.花池等等.那花池来说,里面种满了各种各样的鲜花,花池本身要做 ...
- Python编写登陆接口
实现功能: 输入用户名和密码 认证成功后显示欢迎信息 输错三次后锁定 # 提示输入用户名和密码 # 验证用户名和密码 # 如果错误,则输出用户名或密码错误 # 如果成功,则输出 欢迎,XXX! #!/ ...
- 测试人员如何"提问"
本文打算谈谈QA如何高质量的“提问” 写这些的初衷其实比较简单,作为一个测试老鸟,加入了一些很有质量的测试圈子,也在不同的公司带过不少新人,常常会碰到低效率的“提问”,主要表现如下: 1.问题 ...
- BZOJ3270 博物馆(高斯消元+概率期望)
将两个人各自所在点视为状态,新建一个图.到达某个终点的概率等于其期望次数.那么高斯消元即可. #include<iostream> #include<cstdio> #incl ...
- unable to create new native thread
一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题:运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclipse ...