android 藍芽低功耗(BLE)非常棒的工具類,擷取小米手環的步數
現在物聯網搞的轟轟烈烈的,小米的手環等一系列產品,下面我們就來研究一下小米手環的記步功能
工具類
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 { //UniversalBluetoothLE public static UniversalBluetoothLE universalBluetoothLE; private Context context; //BluetoothAdapter private BluetoothAdapter mBluetoothAdapter; //BluetoothManager private BluetoothManager bluetoothManager; //開啟藍芽的請求碼 public static final int REQUEST_ENABLE_BLUETOOTH = 10010; //是否正在掃描藍牙裝置 private boolean mScanning; //設定掃描時間長度 private static final long SCAN_PERIOD = 10000; //藍芽掃描的返回 BluetoothAdapter.LeScanCallback leScanCallback; //藍芽設別的list List bluetoothDeviceList = new ArrayList(); Handler mHandler = new Handler(); LeScanListenter leScanListenter; private UniversalBluetoothLE(Context context) { this.context = context; //得到BluetoothManager this.bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); //得到BluetoothAdapter this.mBluetoothAdapter = bluetoothManager.getAdapter(); //藍芽搜尋的回調 leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public 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() { @Override public 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 bluetoothDeviceList); } /** * 得到BluetoothGatt * @param device 裝置 * @param autoConnect 是否自動連結 * @param bluetoothGattCallback 回調 */ public BluetoothGatt getConnectGatt(BluetoothDevice device,boolean autoConnect,BluetoothGattCallback bluetoothGattCallback){ return device.connectGatt(context, autoConnect, bluetoothGattCallback); }}
初始化
//在onCreate中//初始化UniversalBluetoothLEuniversalBluetoothLE = UniversalBluetoothLE.inistance(MainActivity.this);
檢測是否開啟藍芽並且請求系統開啟藍芽
//檢測是否開啟藍芽並且請求系統開啟藍芽universalBluetoothLE.openBbletooth();
連結裝置
mBluetoothGatt=universalBluetoothLE.getConnectGatt(device,true,mGattCallback);mBluetoothGatt.connect();
最後再實現一個GattCallback回調,搞定
看看小米的記步功能吧,完美擷取哦,有小米手環的可以測試哦,第三個是我的小米手環。