Android Bluetooth 4.0 Development Introduction

Source: Internet
Author: User

Recently has been studying a Bluetooth function because I am a rookie to learn more busy has been engaged for a long time to understand, on the Internet on the Bluetooth 4.0 is a few dome copied to copy, all English comments, on the poor English friends

What a mishap, some unnecessary description Luo Li Bar, the key method of the interface a pen over ... Forget about the groove. I will introduce my recent learning experience, simple what to turn Bluetooth search Bluetooth What I will not say that you Baidu Android Bluetooth 4.0 a big lump. Watch my blog again, I will introduce some of the network of the more important interface callback and so on.

What's specific in Bluetooth: Bluetooth name (name) Bluetooth address (adress) Bluetooth signal strength (RISS) Bluetooth Service (service) Bluetooth services The following is a small service that is specific to do these should be our common use of the other I also do not know.

Bluetooth Communication process: Open Find (You can specify the UUID to find the device) connection is not said, after the connection first get the Bluetooth service (through the UUID) and then look for a Bluetooth service inside the small services called Bluetoo Th

Characteristic  (also obtained through the UUID)  


Here's a look at
Bluetoothgattcallback   on the internet is a pen with no explanation at all for  a long time to show good-hearted tired and then oneself with the device a method of adjustment: The following paste code and comments inside is sent data:
Bluetoothgattcallback bluetoothgattcallback = new Bluetoothgattcallback () {

@Override
public void Onconnectionstatechange (Bluetoothgatt gatt, int status, int newstate) {
Connection state Trigger
if (newstate = = bluetoothprofile.state_connected) {
LOG.D ("Bluetooth", "-------connectsucceed------");
Bluetoothgatt.discoverservices ();//Discovery Service
} else if (newstate = = bluetoothprofile.state_disconnected) {
LOG.D ("Bluetooth", "-------Disconnect------");
if (Bluetoothadapter = = NULL | |!bluetoothadapter.isenabled ()) {
Bluetoothadapter = Bluetoothmanager.getadapter ();
Bluetoothadapter.enable ();
Bluetoothadapter.startlescan (UUID, lescancallback);
}
}
}

/**
* Discover Device Service callbacks
*/
@Override
public void onservicesdiscovered (Bluetoothgatt gatt, int status) {
if (status = = Bluetoothgatt.gatt_success) {
Final byte[] buffer = new BYTE[5];
String open = "open=1";//Data sent
try {
byte[] data = open.getbytes ("UTF-8");
Writedatatodevice (data);//Send (a single method)
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
}
}

@Override
public void Oncharacteristicwrite (Bluetoothgatt GATT, bluetoothgattcharacteristic characteristic, int status) {
if (status = = Bluetoothgatt.gatt_success) {
LOG.D ("E", "--------Send data successfully-----status:");
}
}

@Override
public void Oncharacteristicread (Bluetoothgatt GATT, bluetoothgattcharacteristic characteristic, int status) {
LOG.D ("Bluetooth reply data is", Characteristic.getvalue (). toString ());
}

@Override
public void oncharacteristicchanged (Bluetoothgatt GATT, bluetoothgattcharacteristic characteristic) {

}
.....
};

/**
* Send data to the device
*/
public void Writedatatodevice (byte[] data) {
if (Bluetoothgatt = = null) {
LOG.D ("xx", "----------------bluetoothgatt=null-----------------------");
Return
}

Bluetoothgattservice = Bluetoothgatt
. GetService (Blurtooth_service);//UUID of the service to be used in the Bluetooth device
if (Bluetoothgattservice = = null) {
LOG.D ("PCG", "Service not found!");
Bluetoothgatt.disconnect ();
Return
}

Bluetoothgattcharacteristic Mcharac = Bluetoothgattservice
. getcharacteristic (bluetooth_characteristic);//need to use the UUID of a specific feature under the service
if (Mcharac = = null) {
No case found.
Bluetoothgatt.disconnect ();
Return
}
Bluetoothgatt.setcharacteristicnotification (Mcharac, true);
LOG.D ("PCG", "Data:" + arrays.tostring (data));
Mcharac.setvalue (data); Set the data that needs to be sent
try {
Thread.Sleep (200);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Writecharacteristic (Mcharac);
}

I'll introduce you here. Read this download a dome look at it will be a little clearer.  Engage in development or look good do not copy people, understand the use of the flexible point, to do this function found all over the world dome is a person written like all the English began to suspect that everyone is just copy the official dome even the interaction is a big lump of the broadcast sent to really look not to see. Here we are, The writing is not good do not spray!

Android Bluetooth 4.0 Development Introduction

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.