Android檢測(cè)IBeacon熱點(diǎn)的方法
IBeacon是BLE的一種,搜索iBeacon基站關(guān)鍵在于設(shè)備掃描到的scanRecord數(shù)組,識(shí)別是否有下面加粗斜體的02 15這兩個(gè)數(shù)字。如果有,搜索到的藍(lán)牙設(shè)備就是IBeacon。
// AirLocate:
// 02 01 1a 1a ff 4c 00 02 15 # Apple's fixed iBeacon advertising prefix
// e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 # iBeacon profile uuid
// 00 00 # major
// 00 00 # minor
// c5 # The 2's complement of the calibrated Tx Power
下面分步驟來實(shí)現(xiàn)檢測(cè)IBeacon熱點(diǎn)。
一、獲得手機(jī)藍(lán)牙控制權(quán)限
在manifest 文件中寫上:
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
二、檢測(cè)手機(jī)是否支持藍(lán)牙,并獲取mBluetoothAdapter 對(duì)象
if (!getPackageManager().hasSystemFeature( PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT) .show(); finish(); } final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); return; }
三、實(shí)現(xiàn)LeScanCallback回調(diào)接口
設(shè)備每次檢測(cè)到一個(gè)藍(lán)牙設(shè)備,就會(huì)回調(diào)這個(gè)接口中的onLeScan()方法,并且傳入掃描到的device,rssi,scanRecord等參數(shù)。
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { //在這里處理掃描到的參數(shù) //判斷是不是IBeacon設(shè)備,做相應(yīng)的處理。 } };
四、處理掃描到的參數(shù)的方法
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; } /** * 將掃描到的信息傳入這個(gè)方法 * 該方法會(huì)判斷掃描到的設(shè)備是不是IBeacon * 如果是就返回一個(gè)IBeacon對(duì)象 * 如果不是就返回null * @param device * @param rssi * @param scanData * @return */ public static iBeacon fromScanData(BluetoothDevice device, int rssi, byte[] scanData) { int startByte = 2; boolean patternFound = false; while (startByte <= 5) { if (((int) scanData[startByte + 2] & 0xff) == 0x02 && ((int) scanData[startByte + 3] & 0xff) == 0x15) { // yes! This is an iBeacon patternFound = true; break; } else if (((int) scanData[startByte] & 0xff) == 0x2d && ((int) scanData[startByte + 1] & 0xff) == 0x24 && ((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-000000000000"; 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 is not an iBeacon return null; } iBeacon iBeacon = new iBeacon(); iBeacon.major = (scanData[startByte + 20] & 0xff) * 0x100 + (scanData[startByte + 21] & 0xff); iBeacon.minor = (scanData[startByte + 22] & 0xff) * 0x100 + (scanData[startByte + 23] & 0xff); iBeacon.txPower = (int) scanData[startByte + 24]; // this one is signed iBeacon.rssi = rssi; // AirLocate: // 02 01 1a 1a ff 4c 00 02 15 # Apple's fixed iBeacon advertising prefix // e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 # iBeacon profile // uuid // 00 00 # major // 00 00 # minor // c5 # The 2's complement of the calibrated Tx Power // Estimote: // 02 01 1a 11 07 2d 24 bf 16 // 394b31ba3f486415ab376e5c0f09457374696d6f7465426561636f6e00000000000000000000000000000000000000000000000000 byte[] 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; } private 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(); } }
五、開啟藍(lán)牙
mBluetoothAdapter.enable();
六、開始掃描
mBluetoothAdapter.startLeScan(mLeScanCallback);
代碼改自鏈接地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android獲取ibeacon列表的方法
- Android提高之BLE開發(fā)Android手機(jī)搜索iBeacon基站
- Android藍(lán)牙開發(fā)深入解析
- 詳解Android——藍(lán)牙技術(shù) 帶你實(shí)現(xiàn)終端間數(shù)據(jù)傳輸
- Android Bluetooth藍(lán)牙技術(shù)使用流程詳解
- Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼
- 分享Android 藍(lán)牙4.0(ble)開發(fā)的解決方案
- android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,支持多種機(jī)型
- Android手機(jī)通過藍(lán)牙連接佳博打印機(jī)的實(shí)例代碼
- Android基于ibeacon實(shí)現(xiàn)藍(lán)牙考勤功能
相關(guān)文章
Android AlertDialog自定義樣式實(shí)現(xiàn)代碼
這篇文章主要介紹了Android AlertDialog自定義樣式實(shí)現(xiàn)代碼的相關(guān)資料,這里提供了實(shí)例代碼,一個(gè)簡(jiǎn)單示例,需要的朋友可以參考下2016-12-12Android雷達(dá)掃描動(dòng)態(tài)界面制作
這篇文章主要為大家詳細(xì)介紹了Android雷達(dá)掃描動(dòng)態(tài)界面制作資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android?studio實(shí)現(xiàn)日期?、時(shí)間選擇器與進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)日期、時(shí)間選擇器與進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼
這篇文章主要介紹了TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼,可實(shí)現(xiàn)左右滑動(dòng)切換視圖界面和點(diǎn)擊切換,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2019-01-01Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn)
這篇文章主要介紹了Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Android EditText限制輸入字符類型的方法總結(jié)
這篇文章主要介紹了Android EditText限制輸入字符類型的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03Android編程實(shí)現(xiàn)自定義控件的方法示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義控件的方法,結(jié)合實(shí)例形式分析了Android自定義控件的布局、功能實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2017-06-06