Android BLE Bluetooth Low Power tutorial, central and peripheral implementation

Source: Internet
Author: User

Android4.3 standardizes BLE APIs. However, up to 4.4 of the current version still has some incomplete functions.

In the BLE protocol, there are two roles: the perimeter (Periphery) and the Central (Central); the peripheral is the data provider, and the Central is the data user/processor; In the iOS SDK, an iOS device can be used as a peripheral device or as a central device. However, in the Android SDK, until now, the latest Android4.4.2, Android phones can only use and process data as the central device; where does the data come from? From BLE devices, many wearable devices now use BLE to provide data.

A central node can be connected to multiple peripheral nodes at the same time, but a peripheral node can be connected to only one central node at a certain time.

After learning about the concept, let's take a look at the four key classes of the Android ble sdk ):

A)BluetoothGattServerProvide data as the surrounding area;BluetoothGattServerCallbackReturn the surrounding status.

B)BluetoothGattUse and process data as a central node;BluetoothGattCallbackReturns the central status and surrounding data.

Because we are talking about the Android ble sdk, all of the following javasthgattservers represent the surrounding area, and javasthgatt represents the central area.

1. Create a perimeter(Although the surrounding API does not work on the Android phone, check it out)

A) First, let's take a look at the class used in the surrounding area. The blue elliptic


B) Description:

Each peripheral gatethgattserver contains multiple services, and each Service contains multiple Characteristic features.

1. A new feature: character = new BluetoothGattCharacteristic (
UUID. fromString (characteristicUUID ),
Descrithgattcharacteristic. property_policy,
Descrithgattcharacteristic. PERMISSION_READ );

2. A new service: service = new BluetoothGattService (UUID. fromString (serviceUUID ),
BluetoothGattService. SERVICE_TYPE_PRIMARY );

3. Add features to the service: service. addCharacteristic (character );

4. Obtain descrithmanager: manager = (descrithmanager) getSystemService (Context. descrith_service );

5. Obtain/open the perimeter: descrithgattserver server = manager. openGattServer (this,
New effecthgattservercallback (){...});

6. Add the service to the surrounding area: server. addService (service );

7. Start broadcast service: Google has no Broadcast Service API yet. Wait !!!!! So currently, we cannot use an Android mobile phone as a peripheral to provide data.


2. Create a central(This time it won't disappoint you. You can successfully create and connect to the surrounding area)

A) First, let's look at the class used in the center, the blue ellipse.

: Http://download.csdn.net/detail/jimoduwu/7072515



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.