我有2手机与Android 5.0.2,他们都安装了最新的半径信标的应用程序:定位信标,同时,我打开了2 IBeacon发送器,可以看到RSSI不断变化在两个手机与应用程序.
但当我尝试编写一些示例代码来模拟上述情况时,我发现ble scan回调总是在调用2或3次后**stop get call,我最初怀疑“定位信标”可能使用不同的方式,所以我尝试了2种API,一种是旧版4.4,另一种是Android 5中引入的新方式,但两者的行为相同(但都运行在Android 5上)。
4.4版本:
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final String LOG_TAG = "BleCollector";
private TextView calledTimesTextView = null;
private int calledTimes = 0;
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
calledTimes++;
runOnUiThread(new Runnable() {
@Override
public void run() {
calledTimesTextView.setText(Integer.toString(calledTimes));
}
});
Log.e(LOG_TAG, "in onScanResult, " + " is coming...");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calledTimesTextView = (TextView) findViewById(R.id.CalledTimes);
mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE))
.getAdapter();
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
以及5.0.2:
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothLeScanner mLescanner;
private ScanCallback mLeScanCallback;
private static final String LOG_TAG = "BleFingerprintCollector";
private TextView calledTimesTextView = null;
private int calledTimes = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calledTimesTextView = (TextView) findViewById(R.id.CalledTimes);
this.mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE))
.getAdapter();
this.mLescanner = this.mBluetoothAdapter.getBluetoothLeScanner();
ScanSettings bleScanSettings = new ScanSettings.Builder().setScanMode(
ScanSettings.SCAN_MODE_LOW_LATENCY).build();
this.mLeScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
calledTimes++;
runOnUiThread(new Runnable() {
@Override
public void run() {
calledTimesTextView.setText(Integer
.toString(calledTimes));
}
});
Log.e(LOG_TAG, "in onScanResult, " + " is coming...");
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
}
@Override
public void onScanFailed(int errorCode) {
}
};
this.mLescanner.startScan(null, bleScanSettings, this.mLeScanCallback);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
它们非常简单,只是在UI中显示一个计数器,最终证明总是停在2或3。
我以前在三星Note 2上玩过这个ble广告接收与安卓4.4设备,它工作完美,回调得到调用每一秒.那么任何人都可以帮助?为什么半径'定位信标在这里工作得很好?
2条答案
按热度按时间nx7onnlm1#
您提到的Locate app使用开源Android Beacon Library来检测信标。它基于您在问题中展示的相同扫描API构建,但它通过定义扫描周期(前台默认为1.1秒)并在此间隔停止和重新启动扫描来解决此问题。停止和重新启动扫描会导致Android发送新回调。
这里还有一些其他注意事项:
充分披露:我是Locate app的作者,是Android Beacon Library开源项目的首席开发人员。
gt0wga4j2#
大卫-您确定扫描回调会针对每个不可连接广告调用吗?我有一部小米Redmi 3和另一部Nexus 5手机,运行Android 6.0。我有一个BLE传感器,每隔1分钟发送一次数据。这些作为中央BLE设备的手机应该接收并处理来自传感器的数据。我可以从空中下载看到(OTA)BLE捕获设备,其传感器每1分钟发送一次数据。然而,两部手机似乎以1分钟的间隔处理数据几分钟,但之后停止处理4 - 6分钟,然后开始处理ag
enter code here
ain。手机处理的时间间隔如下所示:1分钟、2分钟、3分钟、8分钟、9分钟、10分钟、11分钟因此,在以1分钟的间隔处理3个分组之后,任一电话将停止处理4 - 6分钟。下面是执行处理的代码。