Android Bluetooth 4.0 (BLE) Development ibeacon Preliminary

Source: Internet
Author: User

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?

 GenevaNumber ofBytes that followinchFirst AD structure onFlags ADtype .Flags value0x1A=000011010       bit 0(OFF) LE Limited Discoverable Modebit 1( on) LE General Discoverable Modebit 2(OFF) Br/edr notSupportedbit 3( on) Simultaneous LE andBr/edr toSame Device capable (Controller)bit 4( on) Simultaneous LE andBr/edr toSame Device capable (Host)1A number ofBytes that followinchSecond ( andLast) AD structure Front is the general Smart hardware Broadcast packet part FF (FF is represented behind manufacture Data)4Cxx(Organization logo,0x4c00Apple logo, https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers) Geneva(0x02ibeacon identification bit) the(0x15, ABytes identification length, Uuid,major,minor sum of length) -  theBD b8-8C One- A 6B-ac3F- -  $ 8C -  theA3 (Proximity UUID)Geneva 4b1099, Major)Geneva  +(888, minor) C6 (remember this is the complement, the conversion to the original code is- -, the ibeacon signal emits a strength value that is used as the benchmark for distance measurement with Rssi, txpower) calculation C61100 0110Complement1100 0101Anti-code1011 1010Original Code-( ++ -+8+2) - -0C09 (unknown)417072696c426561636f6e (hexadecimal for Aprilbeacon string)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(intTxpower,DoubleRSSI) {if(Rssi = =0) {return-1.0;//If we cannot determine accuracy, return-1.}DoubleRatio = Rssi *1.0/txpower;if(ratio <1.0) {returnMath.pow (ratio,Ten); }Else{DoubleAccuracy = (0.89976) * Math.pow (ratio,7.7095) +0.111;returnaccuracy; }    }

Need to add permission when using Bluetooth

<uses-permission android:name="android.permission.BLUETOOTH" />    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

The key code is as follows

 Packagecn.edu.zafu.ble;Importandroid.app.Activity;ImportAndroid.bluetooth.BluetoothAdapter;ImportAndroid.bluetooth.BluetoothDevice;ImportAndroid.bluetooth.BluetoothManager;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public  class mainactivity extends Activity {    PrivateBluetoothadapter 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 =NewIntent (bluetoothadapter.action_request_enable); Startactivityforresult (Enablebluetooth,1);    } mbluetoothadapter.startlescan (Mlescancallback); }PrivateBluetoothadapter.lescancallback Mlescancallback =NewBluetoothadapter.lescancallback () {@Override         Public void Onlescan(FinalBluetoothdevice device,Final intRssiFinal byte[] Scanrecord) {intStartbyte =2;BooleanPatternfound =false;//Looking for ibeacon             while(Startbyte <=5) {if(((int) Scanrecord[startbyte +2] &0xFF) ==0x02&&//Identifies                                                                        // an                                                                        //IBeacon((int) Scanrecord[startbyte +3] &0xFF) ==0x15) {//Identifies                                                                            //Correct                                                                            //Data                                                                            //LengthPatternfound =true; Break;            } startbyte++; }//If found ,            if(Patternfound) {//Convert to 16 binary                byte[] Uuidbytes =New byte[ -]; System.arraycopy (Scanrecord, Startbyte +4, Uuidbytes,0, -); String hexstring = Bytestohex (uuidbytes);//ibeacon UUID valueString uuid = hexstring.substring (0,8) +"-"+ hexstring.substring (8, A) +"-"+ hexstring.substring ( A, -) +"-"+ hexstring.substring ( -, -) +"-"+ hexstring.substring ( -, +);//Major value of ibeacon                intMajor = (Scanrecord[startbyte + -] &0xFF) *0x100+ (Scanrecord[startbyte + +] &0xFF);//Minor value of ibeacon                intMinor = (Scanrecord[startbyte + A] &0xFF) *0x100+ (Scanrecord[startbyte + at] &0xFF);                String ibeaconname = Device.getname (); String mac = Device.getaddress ();intTxpower = (Scanrecord[startbyte + -]); LOG.D ("BLE", Bytestohex (Scanrecord)); LOG.D ("BLE","Name:"+ Ibeaconname +"\nmac:"+ Mac +"\nuuid:"+ UUID +"\nmajor:"+ Major +"\nminor:"+ minor +"\ntxpower:"+ Txpower +"\nrssi:"+ Rssi); LOG.D ("BLE","Distance:"+calculateaccuracy (Txpower,rssi)); }        }    };Static Final Char[] Hexarray ="0123456789ABCDEF". ToCharArray ();Private StaticStringBytestohex(byte[] bytes) {Char[] Hexchars =New Char[Bytes.length *2]; for(intj =0; J < Bytes.length; J + +) {intv = bytes[j] &0xFF; HEXCHARS[J *2] = Hexarray[v >>>4]; HEXCHARS[J *2+1] = hexarray[v &0x0F]; }return NewString (Hexchars); }protected Static Double calculateaccuracy(intTxpower,DoubleRSSI) {if(Rssi = =0) {return-1.0;//If we cannot determine accuracy, return-1.}DoubleRatio = Rssi *1.0/txpower;if(ratio <1.0) {returnMath.pow (ratio,Ten); }Else{DoubleAccuracy = (0.89976) * Math.pow (ratio,7.7095) +0.111;returnaccuracy; }    }}

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

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.