android蓝牙4.0(BLE)开发之ibeacon初步
一個(gè)april beacon里攜帶的信息如下
?| 1 | <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC3F33468C2788A3044B0378C60C09417072696C426561636F6E051250002003020A0000000000000000000000</code> |
具體是什么意思呢
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <code class=" hljs vhdl">02? Number of bytes that follow in first AD structure 01? Flags AD type 06 ????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 前面是常規(guī)智能硬件廣播包部分 ff (FF代表后面是Manufacture Data) 4c 00?? (組織標(biāo)識(shí),0x4c00蘋(píng)果公司標(biāo)識(shí),https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers) 02 (0x02 ibeacon標(biāo)識(shí)位) 15 (0x15,22個(gè)字節(jié)標(biāo)識(shí)長(zhǎng)度,uuid,major,minor總和的長(zhǎng)度) 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? (切記這里是補(bǔ)碼,轉(zhuǎn)化為原碼就是-58,iBeacon的信號(hào)發(fā)出強(qiáng)度值,用來(lái)作為和RSSI一起測(cè)距的基準(zhǔn) ,txPower) ????????計(jì)算 ????????????C6 ????????????1100 0110 補(bǔ)碼 ????????????1100 0101 反碼 ????????????1011 1010 原碼 ????????????-(32+16+8+2) ????????????-58 0c09??? (未知) 417072696c426561636f6e(AprilBeacon字符串對(duì)應(yīng)的十六進(jìn)制) 051250002003020a0000000000000000000000(未知) </code> |
Proximity UUID :這是將你所有的beacon與其他人的beacon設(shè)備區(qū)別開(kāi)的id!例如,目前在商店里某個(gè)區(qū)域分布著多個(gè)beacon形成一條“鏈帶”,用于為顧客提供特定的服務(wù),那么歸屬于同一條“鏈帶”的beacon將分配到相同的proximity UUID。為這條“鏈帶”設(shè)計(jì)的專(zhuān)用應(yīng)用程序?qū)?huì)在后臺(tái)使用這個(gè)UUID掃描到這條“鏈帶”中的beacon設(shè)備。
major 編號(hào):用于將相關(guān)的beacon標(biāo)識(shí)為一組。例如,一個(gè)商店中的所有beacon將會(huì)分配到相同的major編號(hào)。通過(guò)這種方式,應(yīng)用程序就能夠知道顧客位于哪一家商店。
minor 標(biāo)號(hào):用于標(biāo)識(shí)特定的beacon設(shè)備。例如一個(gè)商店中的每一個(gè)beacon設(shè)備都擁有唯一的minor編號(hào),這樣你才能夠知道顧客位于商店中的哪個(gè)位置。
Measuring distance(測(cè)量距離)?
最后一個(gè)值, TX power ,用于確定你和beacon之間距離有多近。根據(jù)這個(gè)值不但可以獲得粗略的信息(比如靠近/遠(yuǎn)離/不在范圍內(nèi)等),也可以獲取精確到米的距離(當(dāng)然你也可以轉(zhuǎn)換為以步為單位的距離)。那么如何實(shí)現(xiàn)?
TX power (上面例子中為0xC6=198,根據(jù)2的補(bǔ)碼測(cè)得256-198=-58dBm)是距離設(shè)備1米測(cè)得的信號(hào)強(qiáng)度值(RSSI- Received Signal Strength Indication,接收到的信號(hào)強(qiáng)弱指標(biāo))。假如接收到的信號(hào)強(qiáng)度減弱了,那么我們可能在遠(yuǎn)離。只要知道1米距離的RSSI,以及當(dāng)前的RSSI(我們可以從接收到的信號(hào)中一塊獲取到這些信息),那么計(jì)算出當(dāng)前的距離是可能的。IOS已經(jīng)實(shí)現(xiàn)了個(gè)這個(gè)功能,對(duì)于其它平臺(tái)需要自己手動(dòng)編碼計(jì)算 。
一個(gè)簡(jiǎn)單的測(cè)距函數(shù)
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 | <code class="language-java hljs ">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; ????????} ????}</code> |
在使用藍(lán)牙時(shí)需要加權(quán)限
?| 1 2 3 4 | <code class="language-xml hljs "> <uses-permission android:name="android.permission.BLUETOOTH"> ????<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"> ????<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"> </uses-permission></uses-permission></uses-permission></code> |
關(guān)鍵代碼如下
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | <code class="language-java hljs ">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; ????????????// 尋找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 (patternFound) { ????????????????// 轉(zhuǎn)換為16進(jìn)制 ????????????????byte[] uuidBytes = new byte[16]; ????????????????System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16); ????????????????String hexString = bytesToHex(uuidBytes); ????????????????// ibeacon的UUID值 ????????????????String uuid = hexString.substring(0, 8) + "-" ????????????????????????+ hexString.substring(8, 12) + "-" ????????????????????????+ hexString.substring(12, 16) + "-" ????????????????????????+ hexString.substring(16, 20) + "-" ????????????????????????+ hexString.substring(20, 32); ????????????????// ibeacon的Major值 ????????????????int major = (scanRecord[startByte + 20] & 0xff) * 0x100 ????????????????????????+ (scanRecord[startByte + 21] & 0xff); ????????????????// ibeacon的Minor值 ????????????????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:" + 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; ????????} ????} } </code> |
至此,本文也就結(jié)束,所謂初步,就是獲取ibeacon模塊的基本信息。
源碼下載?
http://download.csdn.net/detail/sbsujjbcy/8503507
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/5560433.html
總結(jié)
以上是生活随笔為你收集整理的android蓝牙4.0(BLE)开发之ibeacon初步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 干虾仁价格多少钱一斤 干虾仁的营养价值及
- 下一篇: 站立会议10