尝试检测BLE设备时未调用onLeScan函数 [英] onLeScan function not called when trying to detect BLE devices
问题描述
我正在尝试检测服务附近的蓝牙低能耗设备. 服务启动后,将调用 startLeScan(),然后在10秒钟后调用 stopLeScan(). 即使 startLeScan()返回true,但我没有收到任何错误,但 LeScanCallback 上的 onLeScan 不叫. 服务:
I am trying to detect nearby Bluetooth Low-Energy devices on a service. When the service is started, startLeScan() is called, and then stopLeScan() is called after 10 seconds. Even though startLeScan() returns true, and I didn't get any error, onLeScan on the LeScanCallback was not called. the Service:
.
.
.
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
if (DEVICE_NAME.equals(device.getName()) && deviceAddress.equals(device.getAddress())) {
mDevice = device;
BleScan(false);
connectBluetoothDevice();
}
}
};
/**
* Starts and stops Bluetooth LE scanning for BLE devices.
*
* @param enable true to start scanning, false to stop scanning.
*/
public void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mScanning) {
BleScan(false);
}
}
}, SCAN_PERIOD);
BleScan(true);
} else {
BleScan(false);
}
}
/**
* @param scan true to scan, and false to stop scanning for Bluetooth LE devices.
*/
private void BleScan(boolean scan) {
if (scan) {
mScanning = true;
boolean temp = mBluetoothAdapter.startLeScan(mLeScanCallback);
Log.i(TAG, "Started LE scan: " + temp);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
我尝试使用 BluetoothLeScanner startScan()和 stopScan(),而是使用 ScanCallback 它没有帮助:
I tried to use BluetoothLeScanner startScan() and stopScan(), and using ScanCallback instead, but it didn't help:
ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "New LE Device: " + result.getDevice().getName() + " @ " + result.getRssi());
}
@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "Scan Faild!!");
}
};
/**
* Starts and stops Bluetooth LE scanning for BLE devices.
*
* @param enable true to start scanning, false to stop scanning.
*/
public void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mScanning) {
BleScan(false);
}
}
}, SCAN_PERIOD);
BleScan(true);
} else {
BleScan(false);
}
}
/**
* @param scan true to scan, and false to stop scanning for Bluetooth LE devices.
*/
private void BleScan(boolean scan) {
if (scan) {
mScanning = true;
mBluetoothLeScanner.startScan(mScanCallback);
Log.i(TAG, "Started LE scan");
} else {
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
}
}
有人说需要打开GPS,所以我打开了GPS. 我尝试重新启动android设备. 另一个BLE检测应用可以看到BLE设备.
some say GPS needs to be turned on, so I turned on GPS. I tried rebooting the android device. Another BLE detection app can see the BLE device.
为什么不调用扫描回调?
Why isn't the scan callback called?
修改
我正在运行Android 6.0.1的nexus 5设备上运行此应用程序.
I'm running this app on a nexus 5 device running Android 6.0.1 .
我尝试添加位置权限,但这没有帮助:
I tried adding location permission, but it didn't help:
( uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" )
推荐答案
我有类似的问题. 相同的代码,一种平板电脑无法正常工作,而其他平板电脑则可以正常工作. 我没有解决方案,但是当我重置平板电脑并重新安装apk时,它可以再次正常工作.
I have similar problem. Same code, one tablet not work but other does. I have no solution but when I reset my tablet and reinstall apk, it's can work fine again.
我发现了这个 onLeScan from BluetoothAdapter.LeScanCallback没有被调用Android棉花糖 当我打开GPS时,就可以了...
I found this onLeScan from BluetoothAdapter.LeScanCallback not called in Android Marshmallow when I open gps, it's work...
这篇关于尝试检测BLE设备时未调用onLeScan函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!