【发布时间】:2018-04-03 14:56:25
【问题描述】:
我在互联网上找到了一些代码并更改了它。我尝试通过蓝牙发送一个短字符串。我用的是HC-05蓝牙模块。
我可以将我的 android 设备与模块连接,但我无法向我的 Arduino 发送字符串。 我有: 1 EditText 输入我的字符串。
2 个按钮:
-1 发送
-2 连接
你能看看我的代码吗?谢谢:)
Android 代码...
private BluetoothDevice device;
private BluetoothSocket socket;
private OutputStream outputStream;
String command;//string variable that will store value to be transmitted to the bluetooth module
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button BtSend = findViewById(R.id.BtSend);
Button BtVerbinden = findViewById(R.id.BtVerbinden);
final EditText EtEingabe = findViewById(R.id.EtEingabe);
BtSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
command = EtEingabe.getText().toString();
try {
outputStream.write(command.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
});
//Button that connects the device to the bluetooth module when pressed
BtVerbinden.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (BTinit()) {
BTconnect();
}
}
});
}
public boolean BTinit() {
boolean found = false;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) //Checks if the device supports bluetooth
{
Toast.makeText(getApplicationContext(), "Device doesn't support bluetooth", Toast.LENGTH_SHORT).show();
}
if (!bluetoothAdapter.isEnabled())
{
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
if (bondedDevices.isEmpty())
{
Toast.makeText(getApplicationContext(), "Please pair the device first", Toast.LENGTH_SHORT).show();
} else {
for (BluetoothDevice iterator : bondedDevices) {
if (iterator.getAddress().equals(DEVICE_ADDRESS)) {
device = iterator;
found = true;
break;
}
}
}
return found;
}
public boolean BTconnect() {
boolean connected = true;
try {
socket = device.createRfcommSocketToServiceRecord(PORT_UUID); //Creates a socket to handle the outgoing connection
socket.connect();
Toast.makeText(getApplicationContext(),
"Connection to bluetooth device successful", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
connected = false;
}
if (connected) {
try {
outputStream = socket.getOutputStream(); //gets the output stream of the socket
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream == null) {
try {
outputStream = socket.getOutputStream();
} catch (IOException e) {
}
}
return connected;
}
@Override
protected void onStart() {
super.onStart();
}
}
Android XML...
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.swini.gimbalarduino.MainActivity">
<Button
android:id="@+id/BtSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="184dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/EtEingabe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="220dp"
android:ems="10"
android:hint="Hoi"
android:inputType="textPersonName"
android:text="Text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/BtVerbinden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Connect"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
你得到的错误到底是什么,来自哪一行,那一行的变量值是什么?
-
我没有收到任何错误。我的 Arduino 上没有字符串输入,所以我猜它没有发送。
-
我的 Arduino
CodeString myString = "0"; int iDelay = 0; void setup() { pinMode(13, OUTPUT); Serial.begin(300); } void loop() { while (Serial.available()) { myString = Serial.readString(); } iDelay = myString.toInt(); if (myString!="0") { digitalWrite(13, HIGH); delay(iDelay * 1000); digitalWrite(13, LOW); delay(iDelay*1000); } } -
您的应用是否与 HC-05 蓝牙模块连接?如果已连接,我将分享您想要的工作代码。
-
是的。所以模块在连接智能手机后会闪烁。但 LED 不闪烁。
标签: java android string bluetooth arduino