I want to send data to Android Studio Bluetooth and temporarily disable the button

Asked 2 years ago, Updated 2 years ago, 96 views

    mBtnSendData.setOnClickListener(new Button.OnClickListener() {
        Timer buttonTimer = new Timer();

        @Override
        public void onClick(View view) {
            if (mThreadConnectedBluetooth != null) {
                String SendData1 = mTvSendData1.getText().toString();
                String SendData2 = mTvSendData2.getText().toString();
                String SendData = SendData1 + SendData2;
                mTvSendData.setText(SendData);

                mThreadConnectedBluetooth.write(mTvSendData.getText().toString());
            }

            mBtnSendData.setEnabled(false);

            buttonTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBtnSendData.setEnabled(true);
                        }
                    });
                }
            }, 15000);
        }
    });

When you press the button called mBtnSendData, you are creating a source that binds the information created by the application into a string.

Next, I'm creating a source to pause mBtnSendData for about 15 seconds and then reactivate it... It works fine without Bluetooth connection, but if you try to connect Bluetooth and send information, you will get an error.

I would like to ask you how to modify it.

android studio bluetooth button

2022-09-22 14:23

1 Answers

when Bluetooth is not connected
if (mThreadConnectedBluetooth != null) {
    .
    .
    .
    mThreadConnectedBluetooth.write(...);
}

I don't think there was an error because this code wasn't executed.

Please read the error message and resolve the error where you send the data to Bluetooth.


2022-09-22 14:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.