Android BLE Bluetooth Low power tutorial, central Bluetoothgatt and the implementation of the peripheral bluetoothgattserver

Source: Internet
Author: User

Android4.3 has standardized the BLE API, but until now 4.4, some features are not perfect.

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 use/processor; in the iOS SDK, you can use an iOS device as a perimeter or as a central; Inside the SDK, until now the newest android4.4.2,android mobile phone can only use and process data as the center; 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.

If you know the concept, take a look at the four key classes of the Android BLE SDK (Class):

A) Bluetoothgattserver as a peripheral to provide data;Bluetoothgattservercallback returns to the surrounding state.

b) Bluetoothgatt uses and processes data as a central part;bluetoothgattcallback returns to the central State and data provided by the perimeter.

Because we are talking about the android ble SDK, all of the following bluetoothgattserver represent the perimeter, Bluetoothgatt represents the central.

I. Create a perimeter (although the current perimeter API does not work on Android phones, but still look)

A) First look at the surrounding class, the Blue Oval

b) Description:

Each of the perimeter Bluetoothgattserver contains multiple service services, each of which contains multiple feature characteristic.

1.new a feature: character = new Bluetoothgattcharacteristic (
Uuid.fromstring (Characteristicuuid),
Bluetoothgattcharacteristic.property_notify,
Bluetoothgattcharacteristic.permission_read);

2.new Services: Service = new Bluetoothgattservice (uuid.fromstring (Serviceuuid),
Bluetoothgattservice.service_type_primary);

3. Adding features to the service: service.addcharacteristic (character);

4. Get Bluetoothmanager:manager = (Bluetoothmanager) getsystemservice (Context.bluetooth_service);

5. Get/Open perimeter: Bluetoothgattserver Server = Manager.opengattserver (this,
New Bluetoothgattservercallback () {...});

6. Add the service to the perimeter: server.addservice (service);

7. Start broadcasting Service:google not yet broadcast service API, wait!!!!! So for now we can't have an Android phone as a perimeter to provide data.

two. Create a central (this time will not let you down, can be successfully created and connected to the surrounding)

A) First look at the class used in the center, the blue Oval

b) Description:

To get the central Bluetoothgatt, you have to Shanshan 18 bends:

1. First Get Bluetoothmanager:bluetoothmanager = (Bluetoothmanager) getsystemservice (Context.bluetooth_service);

2. Get bluetoothadapt:btadapter = Bluetoothmanager.getadapter ();

3. Start scanning: Btadapter.startlescan (bluetoothadapter.lescancallback);

4. Get bluetoothdevice:public void Onlescan (bluetoothdevice device, int rssi, byte[] scanrecord) from Lescancallback {...}

5. Use Bluetoothdevice to get Bluetoothgatt:gatt = Device.connectgatt (this, true, Gattcallback);

Finally get the central Bluetoothgatt, it has a bunch of methods (check API bar), call these methods, you can through the Bluetoothgattcallback and the surrounding bluetoothgattserver interaction.

Demo Project: http://download.csdn.net/detail/jimoduwu/7072515

from:http://blog.csdn.net/jimoduwu/article/details/21604215

Android BLE Bluetooth Low power tutorial, central Bluetoothgatt and the implementation of the peripheral bluetoothgattserver

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.