android5.0 (Lollipop) BLE peripheral Kind

Source: Internet
Author: User

Reprint please indicate http://blog.csdn.net/lansefeiyang08/article/details/46468743

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

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

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

Let's start by explaining a bit. Not the Android L system will be able to support the BLE peripheral, which is related to the hardware (I have been told to 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, so I'm going to go straight to the code. For the code simple and tidy. I use an activity to complete the main function, assuming there are other requirements, just a slight change can be.

I was writing 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 don't have all the sticker code (after I upload it, the code is downloaded directly). I follow the process to tell you my thoughts on writing.

First of all, I went to the SDK interface, I send the Android SDK now more than a package:android.bluetooth.le, there are more peripheral and scanner. Scanner I will update later.

The second step starts writing the code. The code first checks whether the 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 ();}}
I'm not going to say the first few pieces of code. The basic of the BLE is universal, but you will send a word in the code now
Mbluetoothleadvertiser = Mbluetoothadapter.getbluetoothleadvertiser ();

This code will directly infer whether your device supports BLE peripheral. If this return value is not NULL. You can continue to have the opportunity to develop, assuming that the return empty, it means that your device does not have ble peripheral (of course. My Code does not infer whether Bluetooth is turned on, in order to save time, you can join it yourself.

Support does not support ble peripheral, you can also use the Bluetoothadapter class ismultipleadvertisementsupported () function to check, In fact Getbluetoothleadvetiser () will also run the above ismultipleadvertisementsupported function, so I directly one step, but the principle we must understand.

Third. Your device already supports BLE peripheral, so the next step is to think about how I sent the broadcast. But before you send the broadcast, prepare your own data, for example, what service you are. What data is in 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 number of parameters, one is the broadcast set parameters, one is broadcast data. The other is callback. Of course startadvertising has two formats and the second is the response to 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; }
In this there is a co-ownership of four parameters. Advertisemode, connectable, Timeout, Txpowerlevel. Of course we can set what we need, and 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 number of parameters.

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 a lot of numbers here, I'm here for simplicity. Only the UUID of the heartbeat is broadcast, but there is no data. Assuming you have your own data and so on, you can set it up again. Their own definition functions are also 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 successfully, you will be onstartsuccess by the callback. The parameters of the callback are also the parameters of the Advertisesettings setting. Suppose you still have what you want to do, and you can do it again.

For the convenience of everyone, I have made inferences about the problems that ErrorCode may encounter, and there are only five kinds of error situations.

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. Just be familiar with the process and be able to handle it. Hope to be of help to everyone.

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









android5.0 (Lollipop) BLE peripheral Kind

Related Article

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.