Android Bluetooth Low power (BLE) very good tool class, get the number of steps of bracelet

Source: Internet
Author: User


Now the Internet of things to engage in a vigorous, millet bracelet and other series of products, below we will study the millet hand ring of the step function

Tool class

 Packagecom.zsl.bluetoothdemo.ble;ImportAndroid.bluetooth.BluetoothAdapter;ImportAndroid.bluetooth.BluetoothDevice;ImportAndroid.bluetooth.BluetoothGatt;ImportAndroid.bluetooth.BluetoothGattCallback;ImportAndroid.bluetooth.BluetoothManager;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.os.Handler;ImportJava.util.ArrayList;ImportJava.util.List;/** * Bluetooth Tool class * Created by ZSL on 15/5/25. * * Public  class Universalbluetoothle { //universalbluetoothle  Public StaticUniversalbluetoothle Universalbluetoothle;PrivateContext context;//bluetoothadapter PrivateBluetoothadapter Mbluetoothadapter;//bluetoothmanager PrivateBluetoothmanager Bluetoothmanager;//Turn on the Bluetooth request code  Public Static Final intRequest_enable_bluetooth =10010;//Whether the Bluetooth device is being scanned Private Booleanmscanning;//Set scan duration Private Static Final LongScan_period =10000;//Return of Bluetooth scanBluetoothadapter.lescancallback Lescancallback;//Bluetooth set other listList<bluetoothdevice> bluetoothdevicelist =NewArraylist<bluetoothdevice> (); Handler Mhandler =NewHandler (); Lescanlistenter Lescanlistenter;Private Universalbluetoothle(Context context) { This. Context = Context;//Get Bluetoothmanager  This. Bluetoothmanager = (Bluetoothmanager) context.getsystemservice (Context.bluetooth_service);//Get Bluetoothadapter  This. Mbluetoothadapter = Bluetoothmanager.getadapter ();//callback for Bluetooth searchLescancallback =NewBluetoothadapter.lescancallback () {@Override  Public void Onlescan(Bluetoothdevice device,intRssibyte[] scanrecord) {bluetoothdevicelist.add (device);//Return to all listsLescanlistenter.lescancallback (bluetoothdevicelist);    }        }; }/** * Get to Universalbluetoothle Object * * @param context * @return  */  Public StaticUniversalbluetoothleinistance(Context context) {if(Universalbluetoothle = =NULL) {Universalbluetoothle =NewUniversalbluetoothle (context); }returnUniversalbluetoothle; }/** * Check that Bluetooth is turned on and start the Bluetooth method */  Public void Openbbletooth() {//Determine if Bluetooth is turned on if(Mbluetoothadapter = =NULL|| !mbluetoothadapter.isenabled ()) {//Turn on BluetoothIntent enableintent =NewIntent (bluetoothadapter.action_request_enable);        Context.startactivity (enableintent); }    }/** * Start (true) or end (FALSE) Bluetooth scan * * @param Enable */ Private void Scanledevice(Final BooleanEnable) {if(Enable && mscanning = =false) {mhandler.postdelayed (NewRunnable () {@Override  Public void Run() {mscanning =false;                Mbluetoothadapter.stoplescan (Lescancallback);            }}, Scan_period); Mscanning =true;        Mbluetoothadapter.startlescan (Lescancallback); }Else{mscanning =false;        Mbluetoothadapter.stoplescan (Lescancallback); }    }/** * Start searching for Bluetooth devices * * @param lescanlistenter search for Bluetooth device callbacks (return to Device list) */  Public void Startscanledevice(FinalLescanlistenter lescanlistenter) {bluetoothdevicelist.clear (); This. Lescanlistenter=lescanlistenter; Scanledevice (true); }/** * Stop searching for devices * /  Public void Stopscanledevice() {if(Lescancallback = =NULL)return; Scanledevice (false); }/** * Search for Bluetooth callbacks */  Public  interface lescanlistenter { voidLescancallback (list<bluetoothdevice> bluetoothdevicelist); }/** * Get Bluetoothgatt * @param Device devices * @param AutoConnect is automatically linked * @param  Bluetoothgattcallback Callback * /  PublicBluetoothgattGetconnectgatt(Bluetoothdevice device,BooleanAutoconnect,bluetoothgattcallback bluetoothgattcallback) {returnDevice.connectgatt (context, AutoConnect, bluetoothgattcallback); }}

Initialization

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

Detects if Bluetooth is turned on and asks the system to turn on Bluetooth

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

Linked devices

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

Finally, a Gattcallback callback is implemented to fix


Take a look at  step function, perfect to get oh, there are millet bracelet can test Oh, the third is my millet bracelet.





Test APK | Click to download

Bithub | Welcome to Star


Android Bluetooth Low power (BLE) very good tool class, get the number of steps of bracelet


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.