Android app退至后台, btAdapter.startLeScan(leScanCallback);停止了,怎么让他一直扫描。。。
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);
}}
};