我正在编写关于查找和检测 IBeacons(这些是 BLE 设备)和测距它们(取决于 RSSI 值)的 Android 应用程序我使用来自https://developer.android.com/guide/topics/connectivity/bluetooth-le 的示例代码。 html
但是此代码在我的 android 设备(三星 Galaxy S3 和 LG G3)上的工作方式不同。
在我的 S3 上,“onLeScan”回调在循环中上升了很多次(大约每秒 5 次),并且每次都根据范围给我不同的 RSSI 值。
但是在我的 LG G3 上,当我开始扫描时,“onLeScan”回调只会上升一次。所以如果我想获得新的 RSSI 值,我需要重新开始扫描。而且我认为这不是很好。
我不知道 LG G3 驱动程序是否有问题,或者我必须检查一些 android 设置。任何人都可以告诉我一些关于它的事情吗?
这是我的代码:
public class Main2Activity extends Activity implements BluetoothAdapter.LeScanCallback {
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
/**/
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.addFlags(enableBtIntent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(enableBtIntent);
}
scanLeDevice(true);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(Main2Activity.this);
}
}, 30000);
mScanning = true;
mBluetoothAdapter.startLeScan(Main2Activity.this);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(Main2Activity.this);
}
}
ArrayList<String> datas = new ArrayList<String>();
@Override
public void onLeScan(BluetoothDevice arg0, int arg1, byte[] arg2) {
// TODO Auto-generated method stub
datas.add( arg2.toString() );
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}