简体   繁体   中英

How to release(gc) bluetoothgatt instance?

In my project, I called the mCmdBinder.gattConnect() and mCmdBinder.gattClose() method multiple times and it generated multiple BluetoothGatt instances. In the dumped file .hprof , I can see there are multiple instances of the BluetoothGatt object exist. Even when I run initiate gc command, these instances are not cleaned. Why these instances can not be released?

问题截图

MyGattService.java

public class MyGattService extends Service {
private BluetoothAdapter mAdapt;
private BluetoothDevice mDevice;
private BluetoothGatt mGatt;

@Override
public void onCreate() {
    super.onCreate();

    Log.e("mLog", "service oncreate !");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e("mLog", "service ondestroy()!");
}

@Override
public IBinder onBind(Intent intent) {
    return new CmdBinder();
}

//发送各种命令
public class CmdBinder extends Binder {
    public void gattClose() {
        if (mGatt != null) {
            mAdapt = null;
            mDevice = null;
            mGatt.close();
            mGatt = null;

            Log.e("mLog", "gatt close! mGatt=" + mGatt);
        }
    }

    public void gattConnect() {

        mAdapt = BluetoothAdapter.getDefaultAdapter();
        mDevice = mAdapt.getRemoteDevice("F4:04:4C:0C:81:1B");
        Log.e("mLog", "device bind status:" + mDevice.getBondState());
        mGatt = mDevice.connectGatt(MyGattService.this, true, new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                Log.e("mlog", "status:" + status + "; newState:" + newState);
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.e("mLog", " go discover services!");
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    Log.e("mLog", "mGatt=" + mGatt);
                }
            }
        });
        Log.e("mLog", "gatt connect!");
    }
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

/**
 * 蓝牙处理放在service中
 */
private MyGattService.CmdBinder mCmdBinder;
private MyGattServiceConnection conn;
public class MyGattServiceConnection implements ServiceConnection {
    public void onServiceConnected(ComponentName name, IBinder service) {
        mCmdBinder = (MyGattService.CmdBinder) service;
        Log.e("mLog", "service connected!");
    }
    public void onServiceDisconnected(ComponentName name) {
        Log.e("mLog", "service disconnected!");
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    Intent gattService = new Intent(MainActivity.this, MyGattService.class);
    conn = new MyGattServiceConnection();
    bindService(gattService, conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    unbindService(conn);
}
public void initView() {
    TextView retxt = (TextView) findViewById(R.id.txt_reconnect);
    retxt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCmdBinder.gattConnect();
        }
    });
    TextView close = (TextView) findViewById(R.id.txtclose);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCmdBinder.gattClose();
        }
    });
}
}

In my project, I called the mCmdBinder.gattConnect() and mCmdBinder.gattClose() method multiple times and it generated multiple BluetoothGatt instances. In the dumped file .hprof, I can see there are multiple instances of the BluetoothGatt object exist. Even when I run initiate gc command, these instances are not cleaned. Why these instances can not be released?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM