现在物联网搞的轰轰烈烈的,小米的手环等一系列产品,下面我们就来研究一下小米手环的记步功能


工具类

package com.zsl.bluetoothdemo.ble;import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import java.util.ArrayList;
import java.util.List;/*** 蓝牙的工具类* Created by zsl on 15/5/25.*/public class UniversalBluetoothLE {//UniversalBluetoothLEpublic static UniversalBluetoothLE universalBluetoothLE;private Context context;//BluetoothAdapterprivate BluetoothAdapter mBluetoothAdapter;//BluetoothManagerprivate BluetoothManager bluetoothManager;//打开蓝牙的请求码public static final int REQUEST_ENABLE_BLUETOOTH = 10010;//是否正在扫描蓝牙设备private boolean mScanning;//设置扫描时长private static final long SCAN_PERIOD = 10000;//蓝牙扫描的返回BluetoothAdapter.LeScanCallback leScanCallback;//蓝牙设别的listList<BluetoothDevice> bluetoothDeviceList = new ArrayList<BluetoothDevice>();Handler mHandler = new Handler();LeScanListenter leScanListenter;private UniversalBluetoothLE(Context context) {this.context = context;//得到BluetoothManagerthis.bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);//得到BluetoothAdapterthis.mBluetoothAdapter = bluetoothManager.getAdapter();//蓝牙搜索的回调leScanCallback = new BluetoothAdapter.LeScanCallback() {@Overridepublic void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {bluetoothDeviceList.add(device);//返回所有列表leScanListenter.leScanCallBack(bluetoothDeviceList);}};}/*** 获得到UniversalBluetoothLE对象** @param context* @return*/public static UniversalBluetoothLE inistance(Context context) {if (universalBluetoothLE == null) {universalBluetoothLE = new UniversalBluetoothLE(context);}return universalBluetoothLE;}/*** 检查蓝牙是否打开并且启动打开蓝牙的方法*/public void openBbletooth() {//判断蓝牙是否开启if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {//打开蓝牙Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);context.startActivity(enableIntent);}}/*** 开始(true)或结束(false)蓝牙扫描** @param enable*/private void scanLeDevice(final boolean enable) {if (enable && mScanning == false) {mHandler.postDelayed(new Runnable() {@Overridepublic void run() {mScanning = false;mBluetoothAdapter.stopLeScan(leScanCallback);}}, SCAN_PERIOD);mScanning = true;mBluetoothAdapter.startLeScan(leScanCallback);} else {mScanning = false;mBluetoothAdapter.stopLeScan(leScanCallback);}}/*** 开始搜索蓝牙设备** @param leScanListenter 搜索蓝牙设备的回调(返回设备列表)*/public void startScanLeDevice(final LeScanListenter leScanListenter) {bluetoothDeviceList.clear();this.leScanListenter=leScanListenter;scanLeDevice(true);}/*** 停止搜索设备*/public void stopScanLeDevice() {if (leScanCallback == null)return;scanLeDevice(false);}/*** 搜索蓝牙的回调*/public interface LeScanListenter {void leScanCallBack(List<BluetoothDevice> bluetoothDeviceList);}/*** 得到BluetoothGatt* @param device 设备* @param autoConnect 是否自动链接* @param bluetoothGattCallback 回调*/public BluetoothGatt getConnectGatt(BluetoothDevice device,boolean autoConnect,BluetoothGattCallback bluetoothGattCallback){return device.connectGatt(context, autoConnect, bluetoothGattCallback);}}

初始化

//在onCreate中
//初始化UniversalBluetoothLE
universalBluetoothLE = UniversalBluetoothLE.inistance(MainActivity.this);

检测是否打开蓝牙并且请求系统打开蓝牙

//检测是否打开蓝牙并且请求系统打开蓝牙
universalBluetoothLE.openBbletooth();

链接设备

mBluetoothGatt=universalBluetoothLE.getConnectGatt(device,true,mGattCallback);
mBluetoothGatt.connect();

最后再实现一个GattCallback回调,搞定

看看小米的记步功能吧,完美获取哦,有小米手环的可以测试哦,第三个是我的小米手环。

测试apk | 点击下载


bithub | 欢迎Star

android 蓝牙低功耗(BLE)非常棒的工具类,获取小米手环的步数相关推荐

  1. 【Android应用开发】Android 蓝牙低功耗 (BLE) ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)

    转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359 参考 :  -- 官方文档 : https://develope ...

  2. 蓝牙协议分析(3)_蓝牙低功耗(BLE)协议栈介绍

    原文链接:蓝牙协议分析(3)_蓝牙低功耗(BLE)协议栈介绍 系列索引:蓝牙协议分析(1)_基本概念 蓝牙协议分析(2)_协议架构 目录 1. 前言 2. Why 3. How和What 4. Phy ...

  3. 【转】 Android快速开发系列 10个常用工具类 -- 不错

    原文网址:http://blog.csdn.net/lmj623565791/article/details/38965311 转载请标明出处:http://blog.csdn.net/lmj6235 ...

  4. Android快速开发系列 10个常用工具类

    目录(?)[+] 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基 ...

  5. android文件读取工具类,Android 下读取Assets Properties操作封装工具类

    Android 下读取Assets Properties操作封装工具类 发布时间:2018-06-03作者:laosun阅读(2081) 为了方便使用,首先创建BaseApplication类,如下所 ...

  6. Android快速开发不可或缺的11个工具类(下载)

    Android快速开发不可或缺的11个工具类(下载) 源码简介 Android快速开发不可或缺的11个辅助类,其中10个来自张鸿洋的博客,1个是我平时积攒的,复制粘贴到你的项目里,添加上包名就可以直接 ...

  7. android 软键盘工具类,Android开发之弹出软键盘工具类简单示例

    本文实例讲述了Android开发之弹出软键盘工具类.分享给大家供大家参考,具体如下: package com.maobang.imsdk.util; import android.content.Co ...

  8. java工具类获取文件扩展名与content-type、http与content-type映射关系

    java工具类获取文件扩展名与content-type.http与content-type映射关系 大家好,我是酷酷的韩~ 一.对应关系参考地址: http://doc.chacuo.net/cont ...

  9. c# 低功耗蓝牙_C#建立从笔记本电脑内部蓝牙4.0到蓝牙低功耗(BLE)外设的流

    我正在尝试编写一个连接到蓝牙低功耗设备(BLE)的程序,然后在更新或给定间隔内读取特征. 我的外设是德州仪器CC2540 BLE设备. 然而,这使用了加密狗,我的任务是使用内部蓝牙4.0调制解调器(稍 ...

最新文章

  1. 基于手势识别的鼠标控制实现
  2. 接到三无产品的测试需求时怎么办
  3. apache2.2 虚拟主机配置
  4. 事件控制块的清空与状态查询
  5. 一台电脑上同启动两个Tomcat的方式,windows/Linux配置
  6. 为什么说项目管理是每个人必备的底层能力?
  7. python 循环语句 for while range
  8. asp.net 通过IHttpHandler开发接口
  9. 附加:中半部分sql语句 区/县(数据表)
  10. 计算机网络(第八版)谢希仁
  11. ubuntu下安装wine1.8+QQ音乐
  12. azkaban报Error Chunking during uploading files to db
  13. Python_数据分析_关联规则和王者荣耀数据分析实战
  14. ssm毕设项目大学生比赛信息管理系统38iiq(java+VUE+Mybatis+Maven+Mysql+sprnig)
  15. SII-Slave Information Interface
  16. [损失设计]2.Softmax Loss
  17. 使用Wechaty实现微信机器人操作
  18. 聊聊从逻辑门到操作系统的计算机
  19. java2460601000_oracle定时任务
  20. pscad 与 matlab 接口,PSCAD与MATLAB的接口问题

热门文章

  1. vue单文件props写法_详解Vue 单文件组件的三种写法
  2. jsp获取静态服务器文件路径,11、统一处理异常、处理静态资源访问、项目中的绝对地址跟相对地址问题...
  3. WLST 命令和变量
  4. Context都没弄明白,还怎么做Android开发?
  5. linux全局搜索文件
  6. 在安装虚拟机时,”intel vt-x 处于禁用状态“ 如何解决
  7. 出现 Cannot read property 'xxxxxx' of null 问题思路
  8. 视频里的音乐怎么提取成mp3
  9. 推荐几个出论文的好方向!
  10. MATLAB点云处理(十四):圆柱体拟合(RANSAC | MSAC)