Android L BLE Peripheral Kind

Source: Internet
Author: User

Know that Android L has some improvements to Bluetooth, including adding A2DP sink, HFP client, BLE peripheral features, and more.

I spent a day more time on the Android L BLE Peripheral SDK has been studied, the online information is very few, there is not clear enough to introduce, so I wrote a test application, hoping to understand the BLE peripheral have some help.

This paste mainly to explain the code-based, I will be the project code is also passed to CSDN, to help you test.

First of all, it is not the Android L system can support ble peripheral, which is related to the hardware (I have been told that support ble peripheral is pure software, or it is nonsense, or I measured a problem). I use the pad on my hand (support ble central,android5.0) found directly unsupported, Android5.0 SDK has started to support check phone support ble peripheral, the following code will refer to.

Okay, I'm going to go straight to the code. For the code to be simple and neat, I use an activity to complete the most basic functions, if there are other needs, just a little change on it.

When I write this code, the first puzzle is what is the BLE peripheral operation process? How is the code flow written? I believe that everyone and I should be the same confused. So I do not post all the code (after I upload, the code is downloaded directly). I follow the process to tell you my thoughts on writing.

First, I went to the SDK interface, I found a package:android.bluetooth.le in the Android SDK, there are more peripheral and Scanner,scanner I will update later.

The second step begins to write the code, the Code first Check to see if ble, ble peripheral are supported. The code is as follows:

private void init () {if (!getpackagemanager (). Hassystemfeature (Packagemanager.feature_bluetooth_le)) { Toast.maketext (This, r.string.ble_not_supported, Toast.length_long). Show (); Finish ();} Final Bluetoothmanager Mbluetoothmanager = (bluetoothmanager) getsystemservice (bluetooth_service); mBluetoothAdapter = Mbluetoothmanager.getadapter (); if (mbluetoothadapter = =  null) {Toast.maketext (this, r.string.bluetooth_not_ Supported, Toast.length_long). Show (); Finish ();} Mbluetoothleadvertiser = Mbluetoothadapter.getbluetoothleadvertiser (); if (Mbluetoothleadvertiser = = null) { Toast.maketext (This, "The device is not a support peripheral", toast.length_short). Show (); LOG.E (TAG, "The device is not a support peripheral"); Finish ();}}
The first few pieces of code I will not say, have been the basic of BLE is universal, but you will find in the code more than a sentence
Mbluetoothleadvertiser = Mbluetoothadapter.getbluetoothleadvertiser ();
This code will directly determine whether your device supports BLE peripheral. If this return value is not empty, you can continue to have the opportunity to develop, if returned empty, that means your device does not have ble peripheral (of course, my code does not determine whether Bluetooth is turned on, this to save time, you can add it yourself).

Thirdly, your device already supports BLE peripheral, so the next step is to consider how I send the broadcast. But before you send the broadcast, prepare your own data, such as what service you are, what data is there, and so on.

Let's start by looking at what the broadcast function looks like:

Mbluetoothleadvertiser.startadvertising (Createadvsettings (True, 0), Createadvertisedata (), mAdvertiseCallback);
From the broadcast function should be able to see the required parameters, one is the broadcast settings parameters, one is broadcast data, and one is callback. Of course startadvertising has two formats, and the other is a response that can get broadcast data.

Here's a look at advertisesettings:

/** Create advertisesettings */public static advertisesettings Createadvsettings (boolean connectable, int timeoutmillis {Advertisesettings.builder Msettingsbuilder = new Advertisesettings.builder (); Msettingsbuilder.setadvertisemode ( advertisesettings.advertise_mode_balanced); Msettingsbuilder.setconnectable (connectable); Msettingsbuilder.settimeout (Timeoutmillis); Msettingsbuilder.settxpowerlevel (Advertisesettings.advertise_tx_power_high); Advertisesettings madvertisesettings = Msettingsbuilder.build (); if (madvertisesettings = = null) {if (D) {Toast.makeText (Mcontext, "madvertisesettings = = null", Toast.length_long). Show (); LOG.E (TAG, "madvertisesettings = = null");}} return madvertisesettings; }
There are a total of four parameters, Advertisemode, connectable, Timeout, Txpowerlevel. Of course we can set what we need, and the other parameters will use the default values.

And then the format is very important, we must be advertisesettings.builder, otherwise you can only set a parameter.

And then Advertisedata:

public static Advertisedata Createadvertisedata () {  advertisedata.builder    mdatabuilder = new Advertisedata.builder (); Mdatabuilder.addserviceuuid (Parceluuid.fromstring (Heart_rate_service)); Advertisedata madvertisedata = Mdatabuilder.build (); if (madvertisedata==null) {if (D) {Toast.maketext (MContext, " Madvertisesettings = = null ", Toast.length_long). Show (); LOG.E (TAG, "madvertisesettings = = null");}} return madvertisedata; }
I need to set up a lot of parameters here, I am here to simply broadcast the heartbeat UUID, but no data. If you have your own data and so on, you can set it up again, and the custom function is in the Advertisedata class.

The final step is to prepare the callback function:

Private Advertisecallback Madvertisecallback = new Advertisecallback () {@Override public void onstartsuccess ( Advertisesettings settingsineffect) {super.onstartsuccess (Settingsineffect), if (settingsineffect! = null) {LOG.D (TAG  , "onstartsuccess txpowerlv=" + settingsineffect.gettxpowerlevel () + "mode=" + settingsineffect.getmode () + "timeout=" + Settingsineffect.gettimeout ()); } else {log.e (TAG, "onstartsuccess, Settingineffect is null");}    LOG.E (TAG, "onstartsuccess settingsineffect" + settingsineffect); } @Overridepublic void onstartfailure (int errorCode) {super.onstartfailure (ErrorCode), if (D) log.e (TAG, " Onstartfailure ErrorCode "+ errorCode), if (ErrorCode = = Advertise_failed_data_too_large) {if (D) {Toast.maketext ( Mcontext, R.string.advertise_failed_data_too_large, Toast.length_long). Show (); LOG.E (TAG, "Failed to start advertising as the advertise data to being broadcasted is larger than");}} else if (ErrorCode = = advertise_failed_too_many_advertisers) {if (D) {Toast.maketext (McontExt, r.string.advertise_failed_too_many_advertises, Toast.length_long). Show (); LOG.E (TAG, "Failed to start advertising because No. advertising instance is available.");} else if (ErrorCode = = advertise_failed_already_started) {if (D) {Toast.maketext (Mcontext, R.string.advertise_failed_ already_started, Toast.length_long). Show (); LOG.E (TAG, "Failed to start advertising as the advertising is already started");} else if (ErrorCode = = Advertise_failed_internal_error) {if (D) {Toast.maketext (Mcontext, R.string.advertise_failed_ Internal_error, Toast.length_long). Show (); LOG.E (TAG, "Operation failed due to an internal error");}} else if (ErrorCode = = advertise_failed_feature_unsupported) {if (D) {Toast.maketext (Mcontext, R.string.advertise_ failed_feature_unsupported, Toast.length_long). Show ();    LOG.E (TAG, "This feature isn't supported on this platform");}} }};
When you broadcast the success, you will be onstartsuccess callback, the parameters of the callback is also the parameters of the Advertisesettings setting. If you still have what you want to do, you can do it again.

For the convenience of everyone, I have errorcode may encounter problems, have made a judgment, only these five kinds of error conditions.

The final step is to shut down, open the broadcast to shut down, or it will cause unknown problems:

private void Stopadvertise () {if (Mbluetoothleadvertiser! = null) {mbluetoothleadvertiser.stopadvertising ( Madvertisecallback); Mbluetoothleadvertiser = null; } }
OK, the code is so simple, as long as the familiar with the process can be done. Hope to be of help to everyone.

The code path is: http://download.csdn.net/detail/lansefeiyang08/8799027









Android L BLE Peripheral Kind

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.