Android Bluetooth:ibeacon
6145 ワード
従来のBluetoothとは異なり、ibeaconは低消費電力のBluetooth技術であり、Android 4.3以上のバージョン、Bluetooth 4.0が必要です.
従来のBluetoothのコードを使用すると、ibeaconデバイスは検索されません.携帯電話に付属しているBluetoothは、一般的に伝統的なBluetoothです.だからibencon関連のcodeを書く必要がある.
従来のBluetoothのコードを使用すると、ibeaconデバイスは検索されません.携帯電話に付属しているBluetoothは、一般的に伝統的なBluetoothです.だからibencon関連のcodeを書く必要がある.
mAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
//code
}
});
ibeacon, onLeScan 。startLeScan , UI 。
code:
mAdapter.stoptLeScan(BluetoothAdapter.LeScanCallback);
onLeScan ,BluetoothDevice ,rssi ,scanRecord , uuid。
rssi :
int absRssi = Math.abs(rssi);
float power = (absRssi-59)/(10*2.0f); //59 rssi, 2.0 , 。
uuid ?
public String parseUUID(byte[] scanRecord) {
int startByte = 2;
boolean patternFound = false;
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) {
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord,startByte+4,uuidBytes,0,16);
String hexString = bytesToHexString(uuidBytes);
String uuid = hexString.substring(0,8)+"-"+
hexString.substring(8,12)+"-"+
hexString.substring(12,16)+"-"+
hexString.substring(16,20)+"-"+
hexString.substring(20,32);
return uuid;
} else {
return "";
}
}