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 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...
随机推荐
- The user survey(用户调查)
在周末,我们找了一些人来进行了一个调查,鉴于选择困难,我们只找到了几个真正的小学生,没有找到家长,其余那些都是找大学生来做调查的,我们和他们说,让他们把自己的立场看成是小学生或家长.下面是我们整理出来 ...
- 软件工程实践2018第六次作业——现场UML作图
团队信息 学号 姓名 博客链接 124 王彬(组长) 点击这里 206 赵畅 点击这里 215 胡展瑞 点击这里 320 李恒达 点击这里 131 佘岳昕 点击这里 431 王源 点击这里 206 陈 ...
- BETA-2
前言 我们居然又冲刺了·二 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 了解OpenCV下的视频参数及其调用方法 初步编码 接下来的计划 文档工作 速 ...
- Python开发【第五篇】迭代器、生成器、递归函数、二分法
阅读目录 一.迭代器 1. 迭代的概念 #迭代器即迭代的工具(自定义的函数),那什么是迭代呢? #迭代:指一个重复的过程,每次重复都可以称之为一次迭代,并且每一次重复的结果是下一个迭代的初始值(例如: ...
- UIPickerView的使用
简介:UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPick ...
- Scrum 5.0(继4.0)
一,组员任务完成情况 首页设计初步完成但是需要优化界面,只能简单的输出信息和在首页进行登录.界面极其简单. 鸡汤版面设计有困难,问题在于用何种形式来管理用户的数据上传,但是经过小组间的讨论确定设计方向 ...
- Internet History, Technology and Security (Week 7)
Week 7 Technology: Application Protocols Welcome to Week 7 of IHTS. This week has less material than ...
- windows多线程(二) 等待线程返回
多线程编程中,有时我们需要等待某一线程完成了特定的操作后再继续做其他事情,要实现这个目的,可以使用Windows API函数WaitForSingleObject,或者WaitForMultipleO ...
- 理解Restful api的意义
RESTful API 只是API的设计规范或者是一套设计理论. 单就URL和Method这两个点,你可以这样理解: URL 是用来唯一标示一个互联网资源的,而 Method 是用来标识当前请求对该资 ...
- 【CF995F】Cowmpany Cowmpensation(动态规划,拉格朗日插值)
[CF995F]Cowmpany Cowmpensation(多项式插值) 题面 洛谷 CF 题解 我们假装结果是一个关于\(D\)的\(n\)次多项式, 那么,先\(dp\)暴力求解颜色数为\(0. ...