Google Bluetooth 4.0 Workflow

Source: Internet
Author: User

Google officially launched the Android 4.3 operating system on July 25, 13, supporting Bluetooth 4.0 with Bluetooth low energy/ble technology. According to Google's website, the SDK provides a demo development, a total of a few steps:

1. Initialize ble

2. Search for BLE devices

3. Connecting devices that support common property protocols

4. Read the Ble property

5. Receiving Device data

6. Close

Each step is done as follows:

Step1 : Initialize ble

1) get a local adapter: Bluetoothadapter

2) Start Bluetooth: if (!mbluetoothadapter.isenabled ()) {

Intent enablebtintent = new Intent (bluetoothadapter.action_request_enable);

Startactivityforresult (Enablebtintent, REQUEST_ENABLE_BT); }

STEP2 : Search for ble devices

1) called Startlescan () , the bluetoothadapter.lescancallback as a parameter, because the scanning is very power consumption, so you need to set a scan time, And once the target device is found, stop scanning.

2) If you want to scan a known specific target, you can use the Startlescan (uuid[], bluetoothadapter.lescancallback)

give a straight set of UUID to scan.

STEP3 : Connecting a device that supports GATT (GATT Server)

1) Instantiate a Bluetoothgatt objects, such as (Mbluetoothgatt),

Bluetoothgatt Mbluetoothgatt = Device.connectgatt (this, false, mgattcallback);

It has three parameters (object, Boolean variable, Mgattcallback ) , Mgattcallback is a Bluetoothgattcallback a reference to the object used to guide the mobile phone side APP the operation ( gattclient )

Private final Bluetoothgattcallback mgattcallback = new Bluetoothgattcallback () {

public void Onconnectionstatechange (Bluetoothgatt gatt, int status, int newstate) {}

public void onservicesdiscovered (Bluetoothgatt gatt, int status) {}

Public void Oncharacteristicread (Bluetoothgatt GATT, bluetoothgattcharacteristic, int status) {}// implementing read data from the device side

public void oncharacteristicchanged (Bluetoothgatt GATT, bluetoothgattcharacteristic) {}

}; Mgattcallback has four methods, namely state connection change, low power device discovery, data read, data change notification

2) Bluetoothgattcallback Passing data to APP , such as connection status, and more app Operation

STEP4 : Read the Ble property

1 ) Read the name and address of the device

STEP5 : Receive GATT notification

1) setcharacteristicnotification () Notice APP , the device side of the production changes

public void oncharacteristicchanged (Bluetoothgatt GATT, bluetoothgattcharacteristic) {}

// Implementing Data Writing

STEP6 : Closes the program, unlocks the connection

GATT API Work Flow chart:

Read the official offer Demo : (mainly equipment connected to that piece)

1 ) BLE App provide devicecontrolactivity to connect, display data and GATT some of the property features supported by the device and the device, which are primarily Bluetoothleservice communication, and via Android's BLE APT and the BEL device to interact.

2 when a particular feedback is triggered, the appropriate broadcastupdate () method, and pass an action

3 ) Back to devicecontrolactivity The above four events are determined by the Broadcastrecevier for processing

Google Bluetooth 4.0 Workflow

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.