简体   繁体   English

如何释放(gc)蓝牙实例?

[英]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. 在我的项目中,我mCmdBinder.gattConnect()调用了mCmdBinder.gattConnect()mCmdBinder.gattClose()方法,它生成了多个BluetoothGatt实例。 In the dumped file .hprof , I can see there are multiple instances of the BluetoothGatt object exist. 在转储的文件.hprof ,我可以看到存在BluetoothGatt对象的多个实例。 Even when I run initiate gc command, these instances are not cleaned. 即使我运行initiate gc命令,也不会清除这些实例。 Why these instances can not be released? 为什么这些实例无法释放?

问题截图

MyGattService.java 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 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. 在我的项目中,我多次调用了mCmdBinder.gattConnect()和mCmdBinder.gattClose()方法,它生成了多个BluetoothGatt实例。 In the dumped file .hprof, I can see there are multiple instances of the BluetoothGatt object exist. 在转储的文件.hprof中,我可以看到存在BluetoothGatt对象的多个实例。 Even when I run initiate gc command, these instances are not cleaned. 即使我运行启动gc命令,也不会清除这些实例。 Why these instances can not be released? 为什么这些实例无法释放?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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