private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
int rang = rssi;
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) {
//Convert to hex String
byte[] uuidBytes = new byte[16];
System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
String hexString = bytesToHex(uuidBytes);
//UUID detection
String uuid = hexString.substring(0, 8) + "-" +
hexString.substring(8, 12) + "-" +
hexString.substring(12, 16) + "-" +
hexString.substring(16, 20) + "-" +
hexString.substring(20, 32);
// major
final int major = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);
// minor
final int minor = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
BeaconModel model = new BeaconModel();
Log.i(TAG, "UUID: " + uuid + "\\nmajor: " + major + "\\nminor" + minor + "rang+:" + rang + "txPower" + (int) scanRecord[29]);
if (rang == 0) {
}
int txPower = (int) scanRecord[29];
Log.d(TAG, "calculating accuracy based on rssi of " + rssi);
double accuracy;
double ratio = rssi * 1.0 / txPower;
if (ratio < 1.0) {
accuracy = Math.pow(ratio, 10);
Log.d(TAG, " accuracy: +~1:" + accuracy);
} else {
accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
Log.d(TAG, " avg rssi: " + rssi + " accuracy: ~2:" + accuracy);
}
if (accuracy<5){
model.setRang("very_near");
}else if(accuracy<10){
model.setRang("near");
}else if(accuracy>10){
model.setRang("far");
}
Date date = new Date();
model.setUuid(uuid);
model.setMajor(major + "");
model.setMinor(minor + "");
model.setTime(date);
Wyal.getInstance().getUser().AddBeacon(model);
}}
};
uj5u.com熱心網友回復:
你好 請問你解決了嗎?我現在在弄這個 一直弄不好,能跟我說一下怎么弄的嗎 謝謝uj5u.com熱心網友回復:
你好,找到解決方案了嗎uj5u.com熱心網友回復:
你好,請問你這個方案解決了嗎?我也遇到這個問題了,一直沒有弄成功,你能給我說說怎么弄的嗎?謝謝你。轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/212787.html
標籤:Android
