마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 스튜디오 질문입니다. false선언 질문입니다.

0 추천
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    search = (Button) findViewById(R.id.search);
    connect = (Button) findViewById(R.id.connect);

    listView = (ListView) findViewById(R.id.listview);

    if (savedInstanceState != null) {
        ArrayList<BluetoothDevice> list = savedInstanceState.getParcelableArrayList(DEVICE_LIST);
        if (list != null) {
            initList(list);
            MyAdapter adapter = (MyAdapter) listView.getAdapter();
            int selectedIndex = savedInstanceState.getInt(DEVICE_LIST_SELECTED);
            if (selectedIndex != -1) {
                adapter.setSelectedIndex(selectedIndex);
                connect.setEnabled(true);
            }
         
        } else {
            initList(new ArrayList<BluetoothDevice>());
        }

    } else {
        initList(new ArrayList<BluetoothDevice>());
    }
    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            mBTAdapter = BluetoothAdapter.getDefaultAdapter();

            if (mBTAdapter == null) {
                Toast.makeText(getApplicationContext(), "Bluetooth not found", Toast.LENGTH_SHORT).show();
            } else if (!mBTAdapter.isEnabled()) {
                Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBT, BT_ENABLE_REQUEST);
            } else {
                new SearchDevices().execute();
            }
        }
    });

    connect.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            BluetoothDevice device = ((MyAdapter) (listView.getAdapter())).getSelectedItem();
            Intent intent = new Intent(getApplicationContext(), Controlling.class);
            intent.putExtra(DEVICE_EXTRA, device);
            intent.putExtra(DEVICE_UUID, mDeviceUUID.toString());
            intent.putExtra(BUFFER_SIZE, mBufferSize);
            startActivity(intent);
        }
    });

MainActivity코드입니다. 블루투스 장치목록에서 연결버튼을 누르면 충돌이 일어나서 
작동이 중지되는데 
이유가 false선언을 해주지않아서 충돌이 일어나는것같은데 
어디에다가 선언을 해야 될지 몰라서 질문을 드립니다. 



초보안드로이너 (120 포인트) 님이 2021년 6월 21일 질문
초보안드로이너님이 2021년 6월 21일 수정

1개의 답변

0 추천

블루투스가 이미 연결된 상태인지 아닌지 체크하는 로직은 버튼 클릭할 때 넣으시면 될 것 같은데요.

 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

   

    connect.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (isBluetoothDeviceConnected()) return
            //에러메세지를 보여주셔도 됩니다.

            BluetoothDevice device = ((MyAdapter) (listView.getAdapter())).getSelectedItem();
            Intent intent = new Intent(getApplicationContext(), Controlling.class);
            intent.putExtra(DEVICE_EXTRA, device);
            intent.putExtra(DEVICE_UUID, mDeviceUUID.toString());
            intent.putExtra(BUFFER_SIZE, mBufferSize);
            startActivity(intent);
        }
    });

    private boolean isBluetoothDeviceConnected() {
          // Bluetooth가 연결 상태이면 true, 아니면 false  리턴.
    }
}

 

spark (226,420 포인트) 님이 2021년 6월 21일 답변
...