Android Low power Bluetooth (Bluetooth 4.0)--ble Development (top)

Source: Internet
Author: User

Period of time, the company project used mobile phone app and Bluetooth device communication development, here is also the low power Bluetooth (Bluetooth 4.0 and later standard) development, do a summary.

The Bluetooth Technology Alliance released the Bluetooth 4.0 standard on June 30, 2010, and the 4.0 standard adds support for low-power Bluetooth (BLE) on the basis of the Bluetooth 3.0+HS standard. Compared to the original normal Bluetooth and high-speed Bluetooth, ble is the most characteristic of low power consumption, low latency, fast search and connection speed, but the data transmission speed compared to traditional Bluetooth low. Next, we will introduce the BLE from the concept of BLE and two aspects of the code.

First of all, the basic concept:

1.BLE Related Concepts

1.1 GATT, Service, characteristic, descriptor

BLE development, often related to four concepts: GATT, Service, characteristic, descriptor

    • GATT: Is the profile General specification of Bluetooth 4.0, the profile of BLE application is based on GATT. The GATT defines a service framework specification that includes definitions and specifications for the Bluetooth service and service characteristics (characteristic), and the features of read-write, notification, and so on. GATT can be understood as a BLE framework, we implement the BLE function above the GATT.
    • Service: is a collection of data and behaviors that complete a particular function. In GATT, a service may contain service references as well as mandatory or optional characteristic.
    • characteristic: Also known as eigenvalues, a characteristic definition contains the characteristic itself, a value and a description (descriptor) of the declaration. Characteristic is the basic unit to complete the specific function of ble.
    • Descriptor : descriptor defines the specific meaning of the data in the characteristic.

Overall, A Bluetooth 4.0 terminal can contain multiple service, one service can contain multiple characteristic, one characteristic contains one value and multiple descriptor, and one descriptor contains a value

1.2 Central and surrounding (periphery)

In the BLE protocol, there are two roles, the perimeter (periphery) and the Center (central):

The periphery is the data provider, the central is the data usage/processor; In the iOS SDK, you can use an iOS device as a perimeter or as a center; but in the Android SDK, Android phones can only use and process data as a central part; Where does the data come from? From BLE devices, many wearable devices now use BLE to provide data.

A central can be connected to multiple peripherals at the same time, but only one central area is connected at a time.

So the question is, how to define the perimeter and the center?

About the perimeter and the center, there are four classes involved:

    • Bluetoothgattserver: providing data as a perimeter
    • Bluetoothgattservercallback: return to the surrounding state
    • Bluetoothgatt: using and processing data as a central
    • Bluetoothgattcallback: Returns the state of the center and the data provided by the perimeter. 、

Relationship:

2. Develop BLE

With these basic concepts in view, we are officially starting to develop BLE.

BLE's Android development is mainly the following several steps:

1. Add Permissions

2. Scan ble device

3. Connect BLE device

4. Data communication

So now, start with a step-by-step finish.

2.1 Adding permissions:

Adding the required privileges to ble in Androidmanifest.xml is actually two:

<!---<android:name= " Android.permission.BLUETOOTH "/><!---<   android:name= "Android.permission.BLUETOOTH_ADMIN"/>

2.2 Check if the device supports Bluetooth and turn on Bluetooth

Check if the device supports Bluetooth

     /**     * Check if the device supports Bluetooth      *    /publicvoid  isble (Context Context) {        // phone hardware supports Bluetooth        if (!  Context.getpackagemanager (). Hassystemfeature (Packagemanager.feature_bluetooth_le)) {            ViewUtils.getInstance (). Showtoast ("The device does not support BLE, will leave to change the page");}    }

Turn on the phone bluetooth switch

    Private Bluetoothadapter Mbluetoothadapter; // Bluetooth Adapter     // turn on Bluetooth permissions    if Null | | ! mbluetoothadapter.isenabled ()) {         new  Intent (bluetoothadapter.action_request_enable);         Startactivityforresult (Enablebtintent, What.Bluetooth.bluetoothEnable);   }

2.3 Scanning devices

To start the scan and stop the scan, just call the corresponding Startlescan () and Stoplescan (), but you need to use the Lescancallback as a parameter:

    //Timeout period    Private Static Final LongScan_period = 10000; ...    Private voidScanledevice (Final Booleanenable) {        if(enable) {//to reach the time-out period, stop scanningMhandler.postdelayed (NewRunnable () {@Override Public voidrun () {mscanning=false;                Mbluetoothadapter.stoplescan (Mlescancallback);            }}, Scan_period); //Start ScanningMbluetoothadapter.startlescan (Mlescancallback); } Else {            //Stop ScanningMbluetoothadapter.stoplescan (Mlescancallback); }        ...    }

Implementation of Lescancallback:

Private Bluetoothadapter.lescancallback Mlescancallback =        new Bluetoothadapter.lescancallback () {    @Override Public    void Onlescan (final bluetoothdevice device, int rssi,            byte[] scanrecord) {        Runonuithread (new Runnable () {           @Override public           void Run () {               //TODO:2017/9/18             }       });}   ;

In the code above:

    • Scan_period: Scanning time, the number of milliseconds, it should be noted that Bluetooth device scanning is very power consumption, this time is not easy to set too long, after the successful scan will stop the device scanning.
    • Bluetoothdevice: Bluetooth device related classes, through which he can get a lot of information about Bluetooth devices

If you only want to scan a specific type of peripheral device, you can call Startlescan (uuid[], bluetoothadapter.lescancallback) and provide a UUID array object that specifies the GATT service your app supports.

2.3 Connecting the GATT service side

The premise of communication with BLE device is to connect the GATT service side, the GATT concept has been mentioned before, call Connectgatt () can be, but need three parameters: This method requires three parameters, a context object, AutoConnect (A Boolean value that indicates whether to automatically connect to a BLE device-when it is available), and a bluetoothgattcallback reference:

Mbluetoothgatt = Device.connectgatt (thisfalse, mgattcallback);

This method returns a Bluetoothgatt that can be used to manipulate GATT.

How does it work? Next blog to say.

This is my personal. Public number: Interested can pay attention to, inside share some of the machine video, programmer! It's important to learn how to be a flirt:

Android Low power Bluetooth (Bluetooth 4.0)--ble Development (top)

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.