Android BLE信标在不同手机型号上的扫描 [英] Android BLE Beacon scanning on different phone models

查看:244
本文介绍了Android BLE信标在不同手机型号上的扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写有关查找和检测IBeacons(这些是BLE设备)并对其进行范围调整(取决于RSSI值)的Android应用程序 我使用来自 https://developer.android.com/guide/的示例代码topic/connectivity/bluetooth-le.html

I am writing Android Application on finding and detecting IBeacons(These are BLE devices) and ranging them(depending on RSSI value) I use Sample code from https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

但是此代码在我的Android设备(三星Galaxy S3和LG G3)上的工作原理有所不同.

But this Code works different on my android devices (Samsung Galaxy S3 and LG G3).

在我的S3上,"onLeScan"回调在循环中多次出现(大约每秒5次),并根据范围每次都给我不同的RSSI值.

On my S3 the "onLeScan" callback rises many times in loop (about 5 per second) and give me different RSSI values every time depending on the range.

但是在我的LG G3上,当我开始扫描时,"onLeScan"回调仅上升一次.因此,如果我要获取新的RSSI值,则需要重新启动扫描.而且我认为这不是很好.

But on my LG G3 the "onLeScan" callback rises only once, when i start Scanning. so if i want to obtain new RSSI values, i need to restart scanning. And i think it is not very good.

我不知道LG G3驱动程序是否有问题,或者我必须检查一些android设置.有人可以告诉我一些事情吗?

I don't know whether it is something wrong with LG G3 driver, or i must check some android settings. Can any one tell me something about it?

这是我的代码:

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;
}

推荐答案

不幸的是,您需要停止并重新开始扫描以获取其他回调.这正是在 Android信标库(停止扫描每一个1.1秒,然后立即重新启动.如果操作系统没有为每个广告进行回调,则可以使每个周期获得一个回调.

Unfortunately, you need to stop and restart scanning to get additional callbacks. This is exactly how it is implemented in the Android Beacon Library, which stops scanning every 1.1 seconds and then immediately restarts. This makes it possible to get at one callback per cycle in cases where the operating system is not making a callback for every advertisement.

目前尚不清楚这在设备和操作系统版本之间是如何变化的.在具有Android 4.3的Nexus 4上,可连接BLE广告与不可连接BLE广告的扫描行为有所不同.可连接的广告每个扫描周期仅引起一个广告回调,而不可连接的广告每个扫描周期接收多个回调.在其他设备和操作系统版本上,此行为可能会有所不同,这就是为什么必须进行循环才能获得广泛的兼容性.

It is unclear exactly how this varies between devices and operating system versions. On the Nexus 4 with Android 4.3, scanning behavior was different for connectable BLE advertisements vs. non-connectable BLE advertisements. Connectable advertisements cause only one advertisement callback per scan cycle, whereas non-connectable advertisements receive multiple callbacks per scan cycle. This behavior may vary on other devices and OS versions, which is why cycling is necessary for wide compatibility.

在具有Android 5.0的Nexus 5设备上,新的扫描API始终为来自同一设备的每个BLE广告返回多个回调,无论该广告是否可连接.但是,搭载Android 5.0的Nexus 4设备仍然只能获得一个可回调广告的广告回调,直到扫描停止并重新启动.这似乎是在驱动程序级别实现的,因此每个ROM映像可能有所不同.

On Nexus 5 devices with Android 5.0, the new scanning APIs always return multiple callbacks for each BLE advertisement from the same device, regardless of whether the advertisement is connectable or not. Nexus 4 devices with Android 5.0, however still only get one advertisement callback for connectable advertisements until the scan is stopped and restarted. This appears to be implemented at the driver level, so it is may be different for each ROM image.

这篇关于Android BLE信标在不同手机型号上的扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆