首页 > 解决方案 > 在我的 android studio 应用程序上成功连接后无法接收 HC - 05 的数据

问题描述

我使用了 BluetoothSPP 库:

https://github.com/akexorcist/Android-BluetoothSPPLibrary

arduino 成功连接到 android studio 应用程序,但我无法接收数据,我将导入我的两个代码,如果我做错了什么,请有人告诉我,根据我的阅读和理解,bluetooth.setOnDataReceivedListener 是接收功能,您应该将 bluetooth.print(data) 放在 arduino 部分,以便

安卓:

公共类 MainActivity 扩展 AppCompatActivity {

final String ON = "1";
final String OFF = "0";
BluetoothSocket socket;

private BluetoothAdapter mAdapter;
BluetoothSPP bluetooth;
private BluetoothDevice remoteDevice;
TextView text;
private BluetoothServerSocket mmServerSocket;
String TAG = "TEST: ";
Button connect;
Button on;
Button off;
private OnDataReceivedListener mDataReceivedListener = null;



public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
        if (resultCode == Activity.RESULT_OK)
            bluetooth.connect(data);
    } else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
        if (resultCode == Activity.RESULT_OK) {
            bluetooth.setupService();
        } else {
            Toast.makeText(getApplicationContext()
                    , "Bluetooth was not enabled."
                    , Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}


public void setOnDataReceivedListener (OnDataReceivedListener listener) {
    if (mDataReceivedListener == null)
        mDataReceivedListener = listener;
}

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket, String socketType) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }


}

public void onStart() {
    super.onStart();
    if (!bluetooth.isBluetoothEnabled()) {
        bluetooth.enable();
    } else {
        if (!bluetooth.isServiceAvailable()) {
            bluetooth.setupService();
            bluetooth.startService(BluetoothState.DEVICE_OTHER);
        }
    }
}

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

    bluetooth = new BluetoothSPP(this);
    text = (TextView) findViewById(R.id.textView2);
    connect = (Button) findViewById(R.id.connect);
    on = (Button) findViewById(R.id.on);
    off = (Button) findViewById(R.id.off);
    BluetoothSocket socket = null;

    mAdapter = BluetoothAdapter.getDefaultAdapter();

    if (!bluetooth.isBluetoothAvailable()) {
        Toast.makeText(getApplicationContext(), "Bluetooth is not available", Toast.LENGTH_SHORT).show();
        finish();
    }
        bluetooth.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
            public void onDataReceived(byte[] data, String message) {
                Log.d(TAG, "Start Reading");
                /*Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();*/
                Log.i("Check", "Message : " + message);
            }
        });

        bluetooth.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
        public void onDeviceConnected(String name, String address) {
            connect.setText("Connected to " + name);
        }

        public void onDeviceDisconnected() {
            connect.setText("Connection lost");
        }

        public void onDeviceConnectionFailed() {
            connect.setText("Unable to connect");
        }
    });

    connect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (bluetooth.getServiceState() == BluetoothState.STATE_CONNECTED) {
                bluetooth.disconnect();
            } else {
                Intent intent = new Intent(getApplicationContext(), DeviceList.class);
                startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
            }


        }
    });


    on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Its on");
            bluetooth.send(ON, true);



        }


    });

    off.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Its off");
            bluetooth.send(OFF, true);
        }
    });





}

Arduino部分:

在此处输入图像描述

标签: javaandroidarduino

解决方案


Arduino 代码看起来不错。首先尝试在您的 Android 上使用蓝牙串行监视器来检查数据是否真的被发送和接收。然后确保在您的 Android 代码中首先连接到模块,然后尝试接收数据。


推荐阅读