Android Bluetooth 4.0 (BLE) Development: ibeacon preliminary

Source: Internet
Author: User

Android Bluetooth 4.0 (BLE) Development: ibeacon preliminary

The ibeacon module used in this article is javasl beacon. What is ibeacon. This article will not be explained. Please check it yourself.

The following information is contained in a javasl beacon:

0201061AFF4C0002159069BDB88C11416BAC3F33468C2788A3044B0378C60C09417072696C426561636F6E051250002003020A0000000000000000000000

What does it mean?

02 Number of bytes that 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 Same Device Capable (Host) 1a Number of bytes that follow in second (and last) AD structure front is conventional smart hardware broadcast package part ff (FF represents Manufacture Data) 4c 00 (Organization Logo, 0x4c00 Apple logo, https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers) 02 (0x02 ibeacon flag) 15 (0x15, 22 bytes ID length, uuid, major, minor total length) 90 69 bd b8-8c 11-41 6b-ac 3f-33 46 8c 27 88 a3 (Proximity UUID) 04 4b (1099, major) 03 78 (888, minor) c6, txPower is used as the benchmark for ranging from 1100 to 0110. txPower calculates the C6 1100 0101 complement code 1011 1010 reverse Code Original code-(32 + 16 + 8 + 2)-580c09 (unknown) 20171000096c426561636f6e (hexadecimal format of the AprilBeacon string) 051250002003020a00000000000000000000000000 (unknown)

Proximity UUID: This is the id that distinguishes all your beacon devices from other beacon devices! For example, multiple beacon nodes are distributed in a certain area of the store to form a "chain belt", which is used to provide specific services to customers, beacon that belongs to the same "chain" will be allocated to the same proximity UUID. The 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 related beacon as a group. For example, all beacon in a store will be assigned the same major number. In this way, the application can know the store where the customer is located.

Minor: used to identify a specific 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.

Measuring distance)
The last value, TX power, is used to determine the distance between you and beacon. Based on this value, not only can rough information be obtained (for example, close to/away from/out of the range ), you can also get the distance accurate to the meter (of course, you can also convert it to the distance in the unit of STEP ). So how to implement it?

TX power (0xC6 = 198 in the preceding example, 256-198 =-58dBm according to the 2's complement) is the Signal Strength value measured 1 meters away from the device (ECC-encoded ed Signal Strength Indication, signal strength indicators received ). If the received signal strength decreases, we may be away from it. As long as you know the 1-meter-distance relationship between the source and the current (we can obtain this information from one of the received signals), it is possible to calculate the current distance. IOS has implemented this function. For other platforms, You need to manually encode and calculate the function.

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;        }    }

Add permissions when using Bluetooth

 
      
      
  

The key code is as follows:

Package cn.edu. zafu. ble; import android. app. activity; import android. bluetooth. export thadapter; import android. bluetooth. export thdevice; 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 effecthadapter mblustmthadapter; @ Override protected void OnCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); effecthmanager implements thmanager = (effecthmanager) getSystemService (Context. descrith_service); mblustmthadapter = descrithmanager. getAdapter (); if (mbluw.thadapter = null |! Mblustmthadapter. isEnabled () {Intent enableBluetooth = new Intent (effecthadapter. ACTION_REQUEST_ENABLE); startActivityForResult (enableBluetooth, 1);} mblustmthadapter. startLeScan (mLeScanCallback);} private receivthadapter. leScanCallback mLeScanCallback = new systthadapter. leScanCallback () {@ Override public void onLeScan (final effecthdevice device, final int Arg, final byte [] scanRecord) {int startByte = 2; boolean patternFound = false; // find ibeacon while (startByte <= 5) {if (int) scanRecord [startByte + 2] & 0xff) = 0x02 & // Identifies // an // iBeacon (int) scanRecord [startByte + 3] & 0xff) = 0x15) {// Identifies // correct // data // length patternFound = true; break;} startByte ++;} // if it is found, if (patternFound) {// convert to hexadecimal byte [] uuidBytes = new byte [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, 12) + "-" + hexString. substring (12, 16) + "-" + hexString. substring (16, 20) + "-" + hexString. substring (20, 32); // Major value of ibeacon int major = (scanRecord [startByte + 20] & 0xff) * 0x100 + (scanRecord [startByte + 21] & 0xff); // The Minor value of ibeacon int minor = (scanRecord [startByte + 22] & 0xff) * 0x100 + (scanRecord [startByte + 23] & 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 +" \ nMinor: "+ minor +" \ nTxPower: "+ txPower +" \ nrssi: "+ Arg); Log. d ("BLE", "distance:" + calculateAccuracy (txPower, ARG) ;}}; static final char [] hexArray = "0123456789 ABCDEF ". 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 Arg) {if (ARG = 0) {return-1.0; // if we cannot determine accuracy, return-1 .} double ratio = Arg * 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 ;}}}

So far, this article ends. The preliminary step is to obtain the basic information of the ibeacon module.

Source code download
Http://download.csdn.net/detail/sbsujjbcy/8503507

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.