startScan()中的Android BLE OutOfMemoryError

时间:2016-06-15 23:19:46

标签: android out-of-memory connectivity android-ble

在使用BluetoothLeScanner#startScan()扫描BLE设备时,当我通过过滤器和OutOfMemoryError时,我正在接收ScanResult。如果我只通过ScanCallback,则不会出现错误。

扫描代码:

private void startScan() {
    BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
    scanner.startScan(getFilterList(), getScanResult(),mScanCallback);
}

private List<ScanFilter> getFilterList() {
    List<ScanFilter> scanFilters = new ArrayList<>();
    scanFilters.add(getScanFilter());
    return scanFilters;
}

private ScanFilter getScanFilter() {
    return new ScanFilter.Builder()
            .setServiceUuid(new ParcelUuid(UUID.fromString("--uuid--").build();
}

private ScanSettings getScanResult() {
    return new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();
}

错误:

    OutOfMemoryError "Failed to allocate a 34873690 byte allocation with 16777216 free bytes and 17MB until OOM"
E/AndroidRuntime: Error reporting crash
                  java.lang.OutOfMemoryError: Failed to allocate a 34873690 byte allocation with 16777216 free bytes and 17MB until OOM
                      at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
                      at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)
                      at java.lang.StringBuffer.append(StringBuffer.java:278)
                      at java.io.StringWriter.write(StringWriter.java:123)
                      at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358)
                      at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303)
                      at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625)
                      at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658)
                      at java.io.PrintWriter.append(PrintWriter.java:691)
                      at java.io.PrintWriter.append(PrintWriter.java:31)
                      at java.lang.Throwable.printStackTrace(Throwable.java:324)
                      at java.lang.Throwable.printStackTrace(Throwable.java:300)
                      at android.util.Log.getStackTraceString(Log.java:504)
                      at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59)
                      at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43)
                      at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:91)
                      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
                      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

1 个答案:

答案 0 :(得分:0)

OutOfMemoryError "Failed to allocate a 34873690 byte allocation with 16777216 free bytes and 17MB until OOM"

List<ScanFilter> scanFilters = new ArrayList<>();
scanFilters.add(getScanFilter());
return scanFilters;

我怀疑这是一个很长的清单。我建议每次添加新的扫描过滤器时都记录此列表。

List<ScanFilter> scanFilters = new ArrayList<>();
scanFilters.add(getScanFilter());
Log.i("Scan filter: ", getScanFilter.toString());
return scanFilters;