0

在我的项目中,我多次调用mCmdBinder.gattConnect()and方法并生成了多个实例。在转储文件中,我可以看到该对象存在多个实例。即使我运行命令,这些实例也不会被清除。为什么这些实例不能被释放?mCmdBinder.gattClose()BluetoothGatt.hprofBluetoothGattinitiate gc

问题截图

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();
        }
    });
}
}
4

1 回答 1

0

在我的项目中,我多次调用了 mCmdBinder.gattConnect() 和 mCmdBinder.gattClose() 方法,它生成了多个 BluetoothGatt 实例。在转储文件 .hprof 中,我可以看到存在多个 BluetoothGatt 对象实例。即使我运行启动 gc 命令,这些实例也不会被清除。为什么这些实例不能被释放?

于 2017-12-17T05:27:42.323 回答