[英]How to get connect with closest bluetooth low energy device from android ?
[英]How to check null Bluetooth Low Energy device in android
我的项目涉及低功耗蓝牙。 我扫描附近的所有BLE设备,并通过许多不同的图标显示在屏幕上。 没关系 但是我想检查是否不是BLE设备扫描,我将显示一条消息“找不到BLE设备”。 我不知道检查NULL BLE设备。 也许你知道!!
这是我的一些代码:
private BluetoothAdapter mBluetoothAdapter;
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapter.startLeScan(mLeScanCallback);
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice arg0, int arg1, byte[] arg2) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
//TODO add device to screen
});
}
};
https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#find
根据Android文档,开发人员有责任在一定时间间隔后停止Le扫描。
在激活扫描的时间间隔内,您可以跟踪触发onLeScan回调的次数。 如果甚至一次都没有触发,则可以在Le Scan停止后显示想要的任何错误消息。
简单,请按照以下步骤操作。
1)运行BLE扫描一段时间。 您可以使用postDelayed处理程序在一定的时间(假设10秒)后调用停止LE扫描。
2)您必须在列表或某处添加找到的设备才能在UI上显示它。 您说过要为不同的设备显示不同的图标。
3)要检查进程是否找到任何设备,可以检查图标集或设备列表大小。 如果它为零,那么您就不会找到任何东西。
或者您可以使用可以在onLeScan方法中启用(true)的布尔值标志,这样您就知道至少调用了一次。
编码愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.