在我的android studio应用上成功连接后,无法接收HC-05的数据

时间:2019-04-17 10:09:11

标签: java android arduino

我已经使用了BluetoothSPP库:

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

arduino已成功连接到android studio应用程序,但我无法接收数据,我将导入我的两个代码,并请有人告诉我,如果我从阅读和理解的角度看做错了蓝牙。 setOnDataReceivedListener是要接收的函数,您应该将bluetooth.print(data)放在arduino部分上,以便

Android:

公共类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部分:

enter image description here

1 个答案:

答案 0 :(得分:0)

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