android 蓝牙低功耗(BLE)非常棒的工具类,获取小米手环的步数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android 蓝牙低功耗(BLE)非常棒的工具类,获取小米手环的步数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4002字,纯文字阅读大概需要6分钟。
内容图文
现在物联网搞的轰轰烈烈的,小米的手环等一系列产品,下面我们就来研究一下小米手环的记步功能
工具类
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.
*/publicclassUniversalBluetoothLE {//UniversalBluetoothLEpublicstatic UniversalBluetoothLE universalBluetoothLE;
private Context context;
//BluetoothAdapterprivate BluetoothAdapter mBluetoothAdapter;
//BluetoothManagerprivate BluetoothManager bluetoothManager;
//打开蓝牙的请求码publicstaticfinalint REQUEST_ENABLE_BLUETOOTH = 10010;
//是否正在扫描蓝牙设备privateboolean mScanning;
//设置扫描时长privatestaticfinallong SCAN_PERIOD = 10000;
//蓝牙扫描的返回
BluetoothAdapter.LeScanCallback leScanCallback;
//蓝牙设别的list
List<BluetoothDevice> bluetoothDeviceList = new ArrayList<BluetoothDevice>();
Handler mHandler = new Handler();
LeScanListenter leScanListenter;
privateUniversalBluetoothLE(Context context) {
this.context = context;
//得到BluetoothManagerthis.bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
//得到BluetoothAdapterthis.mBluetoothAdapter = bluetoothManager.getAdapter();
//蓝牙搜索的回调
leScanCallback = new BluetoothAdapter.LeScanCallback() {
@OverridepublicvoidonLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
bluetoothDeviceList.add(device);
//返回所有列表
leScanListenter.leScanCallBack(bluetoothDeviceList);
}
};
}
/**
* 获得到UniversalBluetoothLE对象
*
* @param context
* @return
*/publicstatic UniversalBluetoothLE inistance(Context context) {
if (universalBluetoothLE == null) {
universalBluetoothLE = new UniversalBluetoothLE(context);
}
return universalBluetoothLE;
}
/**
* 检查蓝牙是否打开并且启动打开蓝牙的方法
*/publicvoidopenBbletooth() {
//判断蓝牙是否开启if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//打开蓝牙
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivity(enableIntent);
}
}
/**
* 开始(true)或结束(false)蓝牙扫描
*
* @param enable
*/privatevoidscanLeDevice(finalboolean enable) {
if (enable && mScanning == false) {
mHandler.postDelayed(new Runnable() {
@Overridepublicvoidrun() {
mScanning = false;
mBluetoothAdapter.stopLeScan(leScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(leScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(leScanCallback);
}
}
/**
* 开始搜索蓝牙设备
*
* @param leScanListenter 搜索蓝牙设备的回调(返回设备列表)
*/publicvoidstartScanLeDevice(final LeScanListenter leScanListenter) {
bluetoothDeviceList.clear();
this.leScanListenter=leScanListenter;
scanLeDevice(true);
}
/**
* 停止搜索设备
*/publicvoidstopScanLeDevice() {
if (leScanCallback == null)
return;
scanLeDevice(false);
}
/**
* 搜索蓝牙的回调
*/publicinterfaceLeScanListenter {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
原文:http://blog.csdn.net/yy1300326388/article/details/45973255
内容总结
以上是互联网集市为您收集整理的android 蓝牙低功耗(BLE)非常棒的工具类,获取小米手环的步数全部内容,希望文章能够帮你解决android 蓝牙低功耗(BLE)非常棒的工具类,获取小米手环的步数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。