The use of ibeacon in Android development

Source: Internet
Author: User

The ibeacon works based on Bluetooth low-energy (BLE) bluetooth® transmission technology, and the Ibeacon base station constantly sends Bluetooth signals around, and when smart devices enter the set area, they are able to receive signals. Android is also able to support ibeacon as long as the ibeacon technical standards are available. Google supports BLE technology in Android4.3

Positioning has always been a very critical feature. The deployment of the Ibeacon base station allows for indoor navigation, while sending information via Bluetooth, ibeacon is a great experience in retail stores or in some public services areas such as stadiums and museums. In particular, Bluetooth good transmission distance, low power, and signal encryption makes ibeacon in the mobile payment field is also very promising. In short, the potential of ibeacon seems to be infinite, but also receives more and more attention.

To understand how ibeacon works first we need to understand ble. BLE (also known as Bluetooth Smart) was first traced back to Nokia's Wibree in 2006 and later fused into the Bluetooth standard as part of the Bluetooth4.0. Currently we can see 3 kinds of Bluetooth devices:

    • Bluetooth: Only traditional mode-enabled bluetooth® devices
    • Bluetooth Smart ready: Supports both traditional and low-power modes of devices
    • Bluetooth Smart: Supports only low-power Bluetooth devices


The biggest advantage of BLE compared to traditional Bluetooth is that power consumption is reduced by 90% while the transmission distance is increased (more than 100 meters), security and stability are improved (AES encryption and CRC verification are supported). Ibeacon also has some of its own features:

    • There is no need to pair, the general Bluetooth device in the impression of pairing work. The ibeacon does not need pairing, as it transmits the signal using a Bluetooth broadcast channel.
    • The program can wake up in the background, ibeacon information push needs app support. But we receive the ibeacon signal without opening the app, as long as the installation is guaranteed, while the phone Bluetooth turned on.

How does the ibeacon work? In fact, the Ibeacon base station through the Bluetooth radio channel continuously send location information, the faster the transmission of power consumption. This means that ibeacon does not push messages, but only for positioning, and the ability to push messages must be done by the app. Apple defines the data format for ibeacon 32-bit broadcasts.


    • UUID: Vendor identification number
    • Major: Equivalent to the group number, the same group Beacon has the same Major
    • Minor: Equivalent to identifying individual beacons in a group
    • TX Power: Used to measure the distance from beacon to the device

The Uuid+major+minor constitutes a beacon identification number, which is somewhat similar to the IP address in the network. TX power is used for ranging, and ibeacon currently only defines roughly 3 rough levels:

    • Very close (Immediate): Within about 10 centimeters
    • Near: within 1 meters
    • Far: 1 m away

Here is the main introduction to its usage:

The first is to get the Bluetoothadapter object:

Final Bluetoothmanager Bluetoothmanager =                (Bluetoothmanager) Getsystemservice (context.bluetooth_service);        Mbluetoothadapter = Bluetoothmanager.getadapter ();        Mbluetoothadapter.startlescan (Mlescancallback);

and then it's the callback, where the search for the ibeacon device is a sort of distance from the phone .

Private Bluetoothadapter.lescancallback Mlescancallback =            new Bluetoothadapter.lescancallback () {        @Override Public        void Onlescan (final bluetoothdevice device, int rssi, byte[] scanrecord) {        final IBeacon IBeacon = IBeacon Class.fromscandata (Device,rssi,scanrecord);        AddDevice (ibeacon);        Collections.sort (Mledevices, New comparator<ibeacon> () {    @Override public    int compare (IBeacon H1, IBeacon H2) {    return h2.rssi-h1.rssi;}});}    ;
Finally, add the search data to the collection .

Private arraylist<ibeacon> mledevices = new arraylist<ibeacon> ();    private void AddDevice (IBeacon device) {//Update beacon information if (device==null) {log.d ("devicescanactivity", "device==null"); return;} for (int i=0;i<mledevices.size (); i++) {String btaddress = Mledevices.get (i). Bluetoothaddress;if (Btaddress.equals ( device.bluetoothaddress) {Mledevices.add (i+1, device); Mledevices.remove (i); break;}} Mledevices.add (device);}

We parse the data in the Ibbeaconclass class, for reference: Https://github.com/AltBeacon/android-beacon-library

public class Ibeaconclass {static public class ibeacon{public String name;    public int major;    public int minor;    Public String Proximityuuid;    Public String bluetoothaddress;    public int txpower;    public int rssi; } public static IBeacon Fromscandata (bluetoothdevice device, int rssi,byte[] scandata) {int startbyte = 2;boolean p Atternfound = False;while (startbyte <= 5) {if (((int) scandata[startbyte+2] & 0xff) = = 0x02 && ((int) Scandat  A[STARTBYTE+3] & 0xff) = = 0x15) {//yes! This was an ibeaconpatternfound = True;break;} else if (((int) scandata[startbyte] & 0xff) = = 0x2d && ((int) scandata[startbyte+1] & 0xff) = = 0x24 &&amp                ;((int) scandata[startbyte+2] & 0xff) = = 0XBF && ((int) scandata[startbyte+3] & 0xff) = = 0x16) { IBeacon IBeacon = new IBeacon (); ibeacon.major = 0;ibeacon.minor = 0;ibeacon.proximityuuid = "00000000-0000-0000-0000-00          0000000000 "; ibeacon.txpower = -55;return IBeacon;}  else if (((int) scandata[startbyte] & 0xff) = = 0xad && ((int) scandata[startbyte+1] &                     0xff) = = 0x77 && ((int) scandata[startbyte+2] & 0xff) = = 0x00 && ((int) scandata[startbyte+3] & 0xff) = = 0xc6) {IBeacon IBeacon = new IBeacon ()                    ;                    ibeacon.major = 0;                    Ibeacon.minor = 0;                    Ibeacon.proximityuuid = "00000000-0000-0000-0000-000000000000";                    Ibeacon.txpower =-55;            return IBeacon; }startbyte++;} if (Patternfound = = False) {//This was not a ibeaconreturn null;} IBeacon IBeacon = new IBeacon () Ibeacon.major = (scandata[startbyte+20] & 0xff) * 0x100 + (scandata[startbyte+21] &amp ;  0xFF); Ibeacon.minor = (scandata[startbyte+22] & 0xff) * 0x100 + (scandata[startbyte+23] & 0xff); ibeacon.txpower = (int) scandata[startbyte+24]; This one is SIGNEDIBEACON.RSSI = rssi;//airlocate://1a 1a ff 4c XX # Apple ' s fixed iBeacon advertising prefix//E2 c5 6d b5 DF fb-D 2 b0 d0 f5 A7 ten E0 # IBeacon profile uuid//XX # Major///XX # minor//C5 # the 2 ' s complement of the CALIBR ated Tx power//estimote://1a each 2d bf//394b31ba3f486415ab376e5c0f09457374696d6f7465426561636f6e00000000 000000000000000000000000000000000000000000byte[] proximityuuidbytes = new BYTE[16]; System.arraycopy (Scandata, startbyte+4, proximityuuidbytes, 0, 16); String hexstring = bytestohexstring (proximityuuidbytes); StringBuilder sb = new StringBuilder () Sb.append (hexstring.substring (0,8)); Sb.append ("-"); Sb.append ( Hexstring.substring (8,12)); Sb.append ("-"); Sb.append (Hexstring.substring (12,16)); Sb.append ("-"); Sb.append ( Hexstring.substring (16,20)); Sb.append ("-"); Sb.append (Hexstring.substring (20,32)); Ibeacon.proximityuuid =        Sb.tostring ();            if (device! = null) {ibeacon.bluetoothaddress = Device.getaddress (); IBeAcon.name = Device.getname ();    }return IBeacon;}          public static String bytestohexstring (byte[] src) {StringBuilder StringBuilder = new StringBuilder ("");          if (src = = NULL | | src.length <= 0) {return null;              } for (int i = 0; i < src.length; i++) {int v = src[i] & 0xFF;              String HV = integer.tohexstring (v);              if (Hv.length () < 2) {stringbuilder.append (0);          } stringbuilder.append (HV);      } return stringbuilder.tostring (); }  }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The use of ibeacon in Android development

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.