Android4.4 (MT8685) source Bluetooth resolution--ble Search

Source: Internet
Author: User

BLE: All called Bluetooth low energy. Bluetooth Specification 4.0 One of the most important features is low power consumption. BLE allows Bluetooth devices to be powered by a button battery to maintain a continuous operation for several years. It is clear that BLE makes Bluetooth devices a very bright application in markets such as clocks, remote control, healthcare and motion sensors.

Google has added support for Bluetooth 4.0 starting with Android 4.3. This article is a demo for the portal analysis of the BLE search process.

Package Com.dy.ble;import Android.annotation.suppresslint;import Android.app.activity;import Android.bluetooth.bluetoothadapter;import Android.bluetooth.bluetoothdevice;import Android.os.Bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Mainactivity extends Activity {private static final String TAG = "BLE";p rivate button scanbtn;private Bluetoo Thadapter bluetoothadapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main); bluetoothadapter = Bluetoothadapter.getdefaultadapter (); if (! Bluetoothadapter.isenabled ()) {bluetoothadapter.enable ();} SCANBTN = (Button) This.findviewbyid (R.id.btn_scan); Scanbtn.setonclicklistener (new Onclicklistener () {@SuppressLint ("Newapi") @Overridepublic void OnClick (View arg0) {if (bluetoothadapter.isenabled ()) {Bluetoothadapter.startlescan ( callback);}});} @SuppressLint ("Newapi") Private Bluetoothadapter.lescAncallback callback = new Bluetoothadapter.lescancallback () {@Overridepublic void Onlescan (bluetoothdevice device, int Rssi, byte[] scanrecord) {log.d (TAG, "onlescan device =" + Device + ", Rssi =" + Rssi + "Scanrecord =" + Scanrecord);}} ;}

Click on the button will start scanning, scan to the device, will trigger Onlescan this callback method, and can be retrieved from the parameters of the Bluetooth device information. The Startlescan method in Bluetoothadapter is analyzed below.

public boolean Startlescan (Lescancallback callback) {        return Startlescan (null, callback);    }

This invokes a method of the same name,

public boolean Startlescan (uuid[] serviceuuids, Lescancallback callback) {if (DBG) log.d (TAG, "Startlescan ():" +        Serviceuuids);            if (callback = = null) {if (DBG) log.e (TAG, "Startlescan:null callback");        return false; } synchronized (mlescanclients) {if (Mlescanclients.containskey (callback)) {if (DBG) Log                . E (TAG, "LE Scan has already started");            return false;                } try {Ibluetoothgatt Igatt = Mmanagerservice.getbluetoothgatt ();                    if (Igatt = = null) {if (DBG) log.e ("Bluetoothadapterreceiver", "Igatt = = null");                BLE is not supported return false;                } uuid uuid = Uuid.randomuuid ();                Gattcallbackwrapper wrapper = new Gattcallbackwrapper (this, callback, serviceuuids);                Igatt.registerclient (New Parceluuid (UUID), wrapper); if (wrapper.scanstarted () {if (DBG) log.e ("Bluetoothadapterreceiver", "wrapper.scanstarted () ==true");                    Mlescanclients.put (callback, wrapper);                return true;            }} catch (RemoteException e) {log.e (TAG, "", e);    }} return false; }

This method requires Bluetooth_admin permissions, the first parameter is an array of UUID for various Bluetooth services, UUID is the abbreviation of "universally unique Identifier", meaning of universal unique identification code. For Bluetooth devices, each service has a universal, independent, unique UUID corresponding to it. That is, at the same time, at the same place, it is impossible to have two different services with the same UUID designation . The second parameter is the Lescancallback object that was passed in earlier.

Android4.4 (MT8685) source Bluetooth resolution--ble Search

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.