Android Bluetooth 4.0 (BLE) Development ibeacon Preliminary

Source: Internet
Author: User
Tags pow

The ibeacon module used in this article is April Beacon, as to what is ibeacon. This article does not explain, the specific please self-examination.

The information that a April beacon carries is as follows

0201061AFF4C0002159069BDB88C11416BAC3F33468C2788A3044B0378C60C09417072696C426561636F6E051250002003020A0000000000000000000000

What do you mean, exactly?

Bytes the follow in first ad STRUCTURE01 flags AD type06 Flags Value 0x1A = 000011010 bit 0 (OFF       ) Le Limited Discoverable mode bit 1 (on) Le General Discoverable Mode bit 2 (OFF) BR/EDR not supported Bit 3 (on) simultaneous Le and br/edr to same Device capable (controller) bit 4 (on) simultaneous Le and Br/edr to S Ame Device capable (Host) 1a number of bytes this follow in second (and last) AD structure Front is the general Smart Hardware Broadcast package section FF (FF stands behind is manufact Ure Data) 4c 00 (organization logo, 0x4c00 apple logo, https://www.bluetooth.org/en-us/specification/assigned-numbers/  Company-identifiers) (0x02 ibeacon identification bit) (0x15,22 byte identification length, Uuid,major,minor sum length). BD b8-8c 11-41 6b-ac 3f-33 8c 27 A3 (Proximity UUID) 4b (1099,major) (888,minor) C6 (Remember that this is the complement, converted to the original code is the -58,ibeacon signal intensity value, used as the benchmark for measuring distance with RSSI, Txpower) Calculation C6 1100 0110 complement 1100 0101 anti-code 1011 1010 Original code-(32+1 6+8+2) -580c09 (unknown) 417072696c426561636f6e(Aprilbeacon string corresponding to hexadecimal) 051250002003020a0000000000000000000000 (unknown) 

Proximity UUID: This is the difference between all your beacons and others ' beacon devices id! For example, in a store where multiple beacons form a "chain" to provide specific services to customers, beacons belonging to the same "chain" will be assigned to the same proximity UUID. A dedicated application designed for this "chain belt" will use this UUID in the background to scan the beacon device in this "chain belt".

Major number: Used to identify the relevant beacon as a group. For example, all beacons in a store will be assigned to the same major number. In this way, the application can know which store the customer is in.

Minor marking: Used to identify a particular beacon device. For example, each beacon device in a store has a unique minor number so that you can know where the customer is located in the store.

measuring distance (measuring distance)
The last value, TX power, is used to determine how close you are to beacon. Depending on this value, you can obtain not only rough information (such as near/away/not within the range), but also the exact distance to the meter (you can, of course, be converted to a distance in steps). So how?

TX Power (0xc6=198 in the example above, measured according to the complement of 2 256-198=-58dbm) is the signal strength measured from the device 1 meters (rssi-received Signal strength Indication, Received signal strength indicator). If the received signal strength weakens, then we may be away from it. It is possible to calculate the current distance, as long as you know the rssi at a distance of 1 meters, and the current rssi (we can get this information from a piece of the received signal). iOS has implemented this feature, and for other platforms it is necessary to manually encode the calculations yourself.

A simple ranging function

protected static double calculateAccuracy(int txPower, double rssi) {        if (rssi == 0) {            return -1.0; // if we cannot determine accuracy, return -1.        }        double ratio = rssi * 1.0 / txPower;        if (ratio < 1.0) {            return Math.pow(ratio, 10);        } else {            double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;            return accuracy;        }    }

Need to add permission when using Bluetooth

         

The key code is as follows

Package Cn.edu.zafu.ble;import Android.app.activity;import Android.bluetooth.bluetoothadapter;import Android.bluetooth.bluetoothdevice;import Android.bluetooth.bluetoothmanager;import Android.content.Context; Import Android.content.intent;import Android.os.bundle;import Android.util.log;public class MainActivity extends    Activity {private Bluetoothadapter mbluetoothadapter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Bluetoothmanager Bluetoothmanager = (bluetoothmanager) getsystemservice (Context.bluetooth_service);        Mbluetoothadapter = Bluetoothmanager.getadapter ();                    if (Mbluetoothadapter = = NULL | |!mbluetoothadapter.isenabled ()) {Intent Enablebluetooth = new Intent (            bluetoothadapter.action_request_enable);        Startactivityforresult (Enablebluetooth, 1); } mbluetoothadapter.startlescan (Mlescancallback);        } private Bluetoothadapter.lescancallback Mlescancallback = new Bluetoothadapter.lescancallback () {@Override            public void Onlescan (final bluetoothdevice device, final int rssi, final byte[] Scanrecord) {            int startbyte = 2;            Boolean patternfound = false; Looking for ibeacon while (Startbyte <= 5) {if (((int) Scanrecord[startbyte + 2] & 0xff) = = 0x0                                                                        2 &&//identifies//an IBeacon ((int) Scanrecord[startbyte + 3] &A mp                                                                            0xff) = = 0x15) {//identifies//correct                                                                            Data Length PAtternfound = true;                Break            } startbyte++; }//If found if (patternfound) {//Convert to 16 binary byte[] uuidbytes = new Byt                E[16];                System.arraycopy (Scanrecord, Startbyte + 4, uuidbytes, 0, 16);                String hexstring = Bytestohex (uuidbytes); ibeacon uuid value String uuid = hexstring.substring (0, 8) + "-" + hexstring.substring (8, +) + "-" + hexstring.substring (+ +) + "-" + hexstring.substring (16,                + "-" + hexstring.substring (20, 32); Ibeacon major value int major = (Scanrecord[startbyte + & 0xff) * 0x100 + (SCA                Nrecord[startbyte +] & 0xff); ibeacon minor value int minor = (scanrecord[startbyte + & 0xff) * 0x100 + (SCANrecord[startbyte +] & 0xff);                String ibeaconname = Device.getname ();                String mac = Device.getaddress ();                int txpower = (scanrecord[startbyte + 24]);                LOG.D ("BLE", Bytestohex (Scanrecord)); LOG.D ("BLE", "Name:" + ibeaconname + "\nmac:" + Mac + "\nuuid:" + uuid + "\nmajor:" + Major + "\nm                Inor: "+ minor +" \ntxpower: "+ Txpower +" \nrssi: "+ rssi);            LOG.D ("BLE", "Distance:" +calculateaccuracy (Txpower,rssi));    }        }    };    Static final char[] Hexarray = "0123456789ABCDEF". ToCharArray ();        private static String Bytestohex (byte[] bytes) {char[] hexchars = new char[bytes.length * 2];            for (int j = 0; J < Bytes.length; J + +) {int v = bytes[j] & 0xFF;            HEXCHARS[J * 2] = Hexarray[v >>> 4];        HEXCHARS[J * 2 + 1] = hexarray[v & 0x0F];    } return new String (Hexchars); }    protected static double calculateaccuracy (int txpower, double rssi) {if (Rssi = = 0) {return-1.0;        If we cannot determine accuracy, return-1.        } Double ratio = Rssi * 1.0/txpower;        if (ratio < 1.0) {return Math.pow (ratio, 10);            } else {Double accuracy = (0.89976) * Math.pow (ratio, 7.7095) + 0.111;        return accuracy; }    }}

At this point, this article also ended, so-called preliminary, is to obtain the basic information of the ibeacon module.

SOURCE download
http://download.csdn.net/detail/sbsujjbcy/8503507

Android Bluetooth 4.0 (BLE) Development ibeacon Preliminary

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.