Android ble Bluetooth 4.0 summary One

Source: Internet
Author: User

This article describes the android BLE Bluetooth 4.0, that is, API level >= 18, and Bluetooth 4.0 mobile phone can be used, if the phone system version API level < 18, is not used Bluetooth 4.0 OH.

First send the official demo, interested in the past to see: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html. Android system more than 4.3, mobile phone support Bluetooth 4.0, with search, pairing, connection, Discovery service and eigenvalues, disconnection and other functions: http://download.csdn.net/detail/lqw770737185/8116019.

First, understand the API and concepts

1.1 Bluetoothgatt

Inherit Bluetoothprofile, through Bluetoothgatt can connect the device (connect), Discover the service (discoverservices), and return the corresponding property to Bluetoothgattcallback

1.2 bluetoothgattcharacteristic

Equivalent to a data type that includes a description of value and 0~n value (bluetoothgattdescriptor)

1.3 Bluetoothgattdescriptor

Descriptor, description of characteristic, including scope, unit of measurement, etc.

1.4 Bluetoothgattservice

A collection of services, characteristic.

1.5 Bluetoothprofile

A generic specification for sending and receiving data according to this specification.

1.6 Bluetoothmanager

Get Bluetoothadapter with Bluetoothmanager

Bluetoothmanager Bluetoothmanager = (bluetoothmanager) getsystemservice (Context.bluetooth_service);

1.7 Bluetoothadapter

An Android system has only one bluetoothadapter, which gets through Bluetoothmanager

Bluetoothadapter mbluetoothadapter = Bluetoothmanager.getadapter ();

1.8 Bluetoothgattcallback

The results that are returned after certain operations on the device have been connected on the device. Here must be reminded that after the device has been connected to return, did not return to seriously see if there is no connection on the device.

Private Bluetoothgattcallback Gattcallback = new Bluetoothgattcallback () {//There are 9 methods to implement, see what the situation is going to do, and use those to implement those public void Onconnectionstatechange (Bluetoothgatt GATT, int status, int newstate) {};p ublic void Oncharacteristicwrite ( Bluetoothgatt GATT, bluetoothgattcharacteristic characteristic, int status) {};}; Bluetoothdevice device = Mbluetoothadapter.getremotedevice (address); Bluetoothgatt GATT = Device.connectgatt (this, false, mgattcallback);

1.8.1:notification对应Oncharacteristicchanged

Gatt.setcharacteristicnotification (characteristic, true);

1.8.2:readCharacteristic对应Oncharacteristicread;

Gatt.readcharacteristic (characteristic);

1.8.3: writeCharacteristic对应 Oncharacteristicwrite;

Gatt.wirtecharacteristic (mcurrentcharacteristic);

1.8.4: Connect bluetooth or disconnect bluetooth Onconnectionstatechange;

1.8.5: readDescriptor对应 Ondescriptorread;

1.8.6:writedescriptor Correspondence Ondescriptorwrite;

1.8.7:readremoterssi Correspondence Onreadremoterssi;

Gatt.readremoterssi ()

1.8.8:executereliablewrite Correspondence onreliablewritecompleted;

1.8.9:discoverservices Correspondence onservicesdiscovered.

Gatt.discoverservices ()

1.9 Bluetoothdevice

Discover connected devices after scanning to get connected devices

Second, turn on Bluetooth permissions

<uses-permission android:name= "Android.permission.BLUETOOTH"/><uses-permission android:name= " Android.permission.BLUETOOTH_ADMIN "/><uses-feature android:name=" Android.hardware.bluetooth_le "Android: Required= "true"/>

If Android.hardware.bluetooth_le is set to false, it can be installed on an unsupported device to determine if Bluetooth 4.0 is supported for code.

if (!getpackagemanager (). Hassystemfeature (Packagemanager.feature_bluetooth_le)) {    Toast.maketext (this, " Device does not support Bluetooth 4.0 ", Toast.length_short). Show ();    Finish ();}

Third, the start and close operation of Bluetooth

1. Use the system to turn on the Bluetooth dialog box by default

if (Mbluetoothadapter = = NULL | |!mbluetoothadapter.isenabled ()) {    Intent enablebtintent = new Intent ( bluetoothadapter.action_request_enable);    Startactivityforresult (Enablebtintent, REQUEST_ENABLE_BT);}

2, the background to open Bluetooth, do not make any hint, this can also be used to customize the Bluetooth dialog box

Mbluetoothadapter.enable ();

3. Turn off Bluetooth in the background

Mbluetoothadapter.disable ();

Four, scanning equipment, connection equipment, access equipment information, disconnect equipment, self-View the official demo, or see the demo is clearer AH

Android ble Bluetooth 4.0 summary One

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.