Android BLE发现问题

时间:2018-12-10 18:03:10

标签: java android bluetooth bluetooth-lowenergy

我正在开发带有蓝牙的Android应用程序,但有时我发现LE设备时遇到问题:通常,发现回调将我找到的设备返回给我,但有时会停止工作,并且不退回我的设备。

我在不同的设备(三星,LG)和不同的Android版本(8.0、6.0、4.4)中测试了代码(以调试模式),但是问题是相同的,不是系统性的,经过一段时间后,它返回到工作正常。

我已经应用了互联网上的所有建议:

但问题仍然存在。

一小段清单:

.
.
.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
.
.
.

和蓝牙代码:

public BLEH_RES StartDiscovery()
{
    .
    .
    .
    CheckPermission();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        return(scanLeDevice21(true));
    else
        return(scanLeDevice18(true));
}

 /**
 * Scan BLE devices on Android API 20 to last version (Android 9.0)
 *
 * @param enable Enable scan
 */
@RequiresApi(21)
private BLEH_RES scanLeDevice21(boolean enable)
{
    bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();

    if (bluetoothLeScanner == null)
        return(BLEH_RES.BLE_NOT_SUPPORTED);

    if (enable)
    {
        ScanSettings.Builder scanSettings = new ScanSettings.Builder();
        scanSettings.setScanMode(SCAN_MODE_LOW_LATENCY);
        bluetoothLeScanner.startScan(null, scanSettings.build(), BLEScanCallback);
    }
    else
        bluetoothLeScanner.stopScan(BLEScanCallback);

    return(BLEH_RES.OK);
}

/**
 * Scan BLE devices on Android API 18 to 20
 *
 * @param enable Enable scan
 */
private BLEH_RES scanLeDevice18(boolean enable)
{
    if(bluetoothAdapter == null)
        return(BLEH_RES.BT_NOT_SUPPORTED);


    if (enable) 
    {
        bluetoothAdapter.startDiscovery();
        bluetoothAdapter.startLeScan(mLeScanCallback);
    }
    else
    {
        bluetoothAdapter.cancelDiscovery();
        bluetoothAdapter.stopLeScan(mLeScanCallback);
    }

    return(BLEH_RES.OK);
}

@RequiresApi(21)
private ScanCallback BLEScanCallback = new ScanCallback()
{
    @Override
    public void onScanResult(int callbackType, ScanResult result)
    {
        addDevice(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes());
    }

    @Override
    public void onScanFailed(int errorCode)
    {
        for(ErrorEvent ee:errorEventList)
            ee.onError(BLEH_EVENT_ERROR.SCAN_FAILED);
    }
};

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback()
{
    @Override
    public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi,byte[] scanRecord)
    {
        addDevice(bluetoothDevice, rssi, scanRecord);
    }
};

您能建议我任何解决该问题的方法吗?

2 个答案:

答案 0 :(得分:0)

确保始终在搜索设备时启用GPS。还建议您观看有关BLE的内容 https://youtu.be/jDykHjn-4Ng

答案 1 :(得分:0)

我通过遵循以下链接中的建议解决了我的问题:https://stackoverflow.com/a/42267678/7006955

我将所有建议的代码添加到我的代码中,并且在Android 4中也可以正常工作