Android Ble蓝牙开发内存泄漏

记录2个Android Ble蓝牙开发容易导致内存泄漏的地方:

  1. 蓝牙扫描
    当BluetoothAdapter.LeScanCallback作为内部类时要定义成static,如果回调内部需要用到activity的引用,要使用弱引用包裹起来。参考:It leaks when I startLeScan in onCreate and stopLeScan in onDestroy

  2. 蓝牙连接
    ble蓝牙连接时,需要传入一个context作为参数,BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback),这个context使用getApplicationContext()获取,可以有效避免内存泄漏。

你可能感兴趣的:(android)