应用程序进入无限循环尝试连接到HC-06,但不存在循环

时间:2016-08-03 22:28:27

标签: android bluetooth

当我按下连接按钮连接到HC-06蓝牙模块(Arduino屏蔽)时,我的应用程序进入无限循环。但是,按下按钮时代码中没有任何循环。

package dleedesign.dubcommunicationstestapp;

import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.Set;

/**
 * Created by Chris on 7/24/2016.
 */
public class FirstFragment extends Fragment {

    View myView;

    public final String TAG = "Main";
    private Bluetooth bt;
    public Button sendCommand;
    public Button send;
    public TextView msgReceived;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.first_layout, container, false);

        sendCommand = (Button) myView.findViewById(R.id.sendCommand);
        sendCommand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                connectService();
            }
        });
        send = (Button) myView.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendBtCommand("1");
            }
        });
        msgReceived = (TextView) myView.findViewById(R.id.msgReceived);
        msgReceived.setText("Ready to connect");

        bt = new Bluetooth(new MainActivity(), mHandler);

        return myView;
    }

    public void sendBtCommand(String msg)
    {
        bt.sendMessage(msg);
    }

    public void connectService()
    {
        try {
            msgReceived.setText("Connecting...");
            BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
            if(btAdapter.isEnabled())
            {
                bt.start();
                bt.connectDevice("HC-06");
                Log.d(TAG, "Btservice started- listening");
                msgReceived.setText("Connected!");

            }
            else
            {
                Log.w(TAG, "Btservice started - bluetooth is not enabled, requesting enable...");
                Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBluetooth, 0);
                msgReceived.setText("Bluetooth not enabled, requested for enable");
            }
        } catch (Exception e) {
            Log.e(TAG, "Unable to start bluetooth", e);
            msgReceived.setText("Unable to connect: " + e);
        }
    }

    private final Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case Bluetooth.MESSAGE_STATE_CHANGE:
                    Log.d(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                    break;
                case Bluetooth.MESSAGE_WRITE:
                    Log.d(TAG, "MESSAGE_WRITE");
                    break;
                case Bluetooth.MESSAGE_READ:
                    Log.d(TAG, "MESSAGE_READ");
                    break;
                case Bluetooth.MESSAGE_DEVICE_NAME:
                    Log.d(TAG, "MESSAGE_DEVICE_NAME " + msg);
                    break;
                case Bluetooth.MESSAGE_TOAST:
                    Log.d(TAG, "MESSAGE_TOAST " + msg);
                    break;
            }
        }
    };

}

出于某种原因,当我尝试使用循环来继续尝试连接而未建立连接时,这个问题突然出现了。我删除了那段代码,但是它仍然可以存在吗?

1 个答案:

答案 0 :(得分:0)

您是否覆盖了片段的onActivityResult方法? 如果没有,请尝试覆盖它并在其中调用connectService(),方法是将其添加到您的片段中: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { connectService() }