Android app退至后台, btAdapter.startLeScan(leScanCallback);停止了,怎么让他一直扫描。。。

囧灬8023 2016-11-04 09:03:35
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);

}}
};
...全文
4948 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
王能 2020-11-13
  • 打赏
  • 举报
回复
引用 4 楼 鸢虞樱落 的回复:
你好,请问你这个方案解决了吗?我也遇到这个问题了,一直没有弄成功,你能给我说说怎么弄的吗?谢谢你。
勿挖坟,自己发帖,描述清楚
鸢虞樱落 2020-11-12
  • 打赏
  • 举报
回复
你好,请问你这个方案解决了吗?我也遇到这个问题了,一直没有弄成功,你能给我说说怎么弄的吗?谢谢你。
果果_ 2017-11-15
  • 打赏
  • 举报
回复
你好,找到解决方案了吗
u010419173 2017-06-15
  • 打赏
  • 举报
回复
你好 请问你解决了吗?我现在在弄这个 一直弄不好,能跟我说一下怎么弄的吗 谢谢

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧