我目前正在构建一个控制继电器的应用程序。该继电器使用arduino pro和继电器控制我家里的灯,并通过电话HC-05与之连接。 到目前为止,我已经能够使用此当前代码来控制灯光(因为在一切正常工作中,请打开和关闭灯光)。但是,我试图在Java代码中实现“自动连接”,以便该应用程序……一旦在HC-05范围内……它将自动连接到我的手机,并且灯会亮起。每次我尝试添加任何类型的自动连接代码或Gitt时,似乎都无法正常工作。另外,当我阅读开发人员说明时,我很难理解要添加的内容。有人可以帮我添加正确的代码,并解释发生了什么,以便我可以学习并“尝试”了解您的工作。谢谢。
这里是我当前的Java:
package com.example.android.labbluetooth;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import app.akexorcist.bluetotohspp.library.BluetoothSPP;
import app.akexorcist.bluetotohspp.library.BluetoothState;
import app.akexorcist.bluetotohspp.library.DeviceList;
public class MainActivity extends AppCompatActivity {
final String ON = "1";
final String OFF = "0";
BluetoothSPP bluetooth;
Button connect;
Button on;
Button off;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetooth = new BluetoothSPP(this);
connect = (Button) findViewById(R.id.connect);
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
if (!bluetooth.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext(), "Bluetooth is not available", Toast.LENGTH_SHORT).show();
finish();
}
bluetooth.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
connect.setText("Connected to " + name);
if (connect == connect){
bluetooth.send(ON, true);}
}
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) {
bluetooth.send(ON, true);
}
});
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetooth.send(OFF, true);
}
});
}
public void onStart() {
super.onStart();
if (!bluetooth.isBluetoothEnabled()) {
bluetooth.enable();
} else {
if (!bluetooth.isServiceAvailable()) {
bluetooth.setupService();
bluetooth.startService(BluetoothState.DEVICE_OTHER);
}
}
}
public void onDestroy() {
super.onDestroy();
bluetooth.stopService();
}
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();
}
}
}
}
和我当前的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connect" />
<Button
android:id="@+id/on"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LED ON" />
<Button
android:id="@+id/off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LED OFF" />
</LinearLayout>