Android Bluetooth Low-Power (BLE) is a great tool to get the steps of bracelet, androidble

Source: Internet
Author: User

Android Bluetooth Low-Power (BLE) is a great tool to get the steps of  bracelet, androidble

Now the Internet of Things is booming, and  wristband and other products, next we will study the progress Logging Function of wristband.

Tool

Package com. zsl. bluetoothdemo. ble; import android. bluetooth. export thadapter; import android. bluetooth. export thdevice; import android. bluetooth. export thgatt; import android. bluetooth. export thgattcallback; import android. bluetooth. bluetoothManager; import android. content. context; import android. content. intent; import android. OS. handler; import java. util. arrayList; import java. util. list;/*** Bluetooth tool class * Creat Ed by zsl on 15/5/25. */public class UniversalBluetoothLE {// UniversalBluetoothLE public static UniversalBluetoothLE universalBluetoothLE; private Context context; // define thadapter private volume thadapter mBluetoothAdapter; // define thmanager private volume thmanager; // open the Bluetooth Request Code public static final int REQUEST_ENABLE_BLUETOOTH = 10010; // whether the bluetooth device private boolean mScanning is being scanned ;/ /Set the scan duration private static final long SCAN_PERIOD = 10000; // The Bluetooth scan returns BluetoothAdapter. leScanCallback leScanCallback; // set the Bluetooth list to <symbol thdevice> symbol thdevicelist = new ArrayList <symbol thdevice> (); Handler mHandler = new Handler (); LeScanListenter leScanListenter; private Context (context Context) {this. context = context; // get thmanager this. descrithmanager = (descrithman Ager) context. getSystemService (Context. export th_service); // obtain export thadapter this. mblustmthadapter = effecthmanager. getAdapter (); // callback leScanCallback = new javasthadapter for Bluetooth search. leScanCallback () {@ Override public void onLeScan (descrithdevice device, int Arg, byte [] scanRecord) {descrithdevicelist. add (device); // return to all the list leScanListenter. leScanCallBack (bluetoothDeviceList) ;};}/ *** get UniversalBluetoothLE object ** @ param context * @ return */public static universalw.thle inistance (Context context) {if (universal=thle = null) {universal=thle = new universal=thle (context );} return universalstmthle;}/*** how to check whether Bluetooth is enabled and enable Bluetooth */public void openBbletooth () {// determine whether to enable if (mblustmthadapter = null |! Mblustmthadapter. isEnabled () {// enable Bluetooth Intent enableIntent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE); context. startActivity (enableIntent) ;}}/*** start (true) or end (false) Bluetooth scan ** @ param enable */private void scanLeDevice (final boolean enable) {if (enable & mScanning = false) {mHandler. postDelayed (new Runnable () {@ Override public void run () {mScanning = false; mbluw.thadapter. stopLeScan (leScanCallback) ;}}, SCAN_PERIOD); mScanning = true; mbluw.thadapter. startLeScan (leScanCallback);} else {mScanning = false; mbluw.thadapter. stopLeScan (leScanCallback) ;}}/*** start searching for Bluetooth devices ** @ param leScanListenter search for Bluetooth device callbacks (back to device list) */public void startScanLeDevice (final LeScanListenter leScanListenter) {descrithdevicelist. clear (); this. leScanListenter = leScanListenter; scanLeDevice (true);}/*** stop searching device */public void stopScanLeDevice () {if (leScanCallback = null) return; scanLeDevice (false );} /*** search for Bluetooth callback */public interface LeScanListenter {void leScanCallBack (List <symbol thdevice> symbol thdevicelist );} /*** obtain whether thgatt * @ param device * @ param autoConnect automatically links * @ param gatethgattcallback callback */public descrithgatt getConnectGatt (descrithdevice device, boolean autoConnect, descrithgattcallback) {return device. connectGatt (context, autoConnect, bluetoothGattCallback );}}

Initialization

// In onCreate // initialize universalw.thleuniversal=thle = universal=thle. inistance (MainActivity. this );

Checks whether Bluetooth is enabled and requests the system to enable Bluetooth.

// Check whether Bluetooth is enabled and request the system to enable Bluetooth universalstmthle. openBbletooth ();

Connected Device

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

Finally, implement a GattCallback callback.

Let's take a look at  step-by-step function. You can test the bracelet. The third is my bracelet.


Test apk | click to download

Bithub | welcome to Star


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.