android 藍芽低功耗(BLE)非常棒的工具類,擷取小米手環的步數

來源:互聯網
上載者:User

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回調,搞定

看看小米的記步功能吧,完美擷取哦,有小米手環的可以測試哦,第三個是我的小米手環。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.