通过 HC-05 蓝牙将变量值或字符串从 Android App 发送到 Arduino
Send variable value or string from Android App to Arduino via HC-05 Bluetooth
我正在使用 Android Studio 创建一个 Android 应用程序。当我按下一个按钮时,我想通过蓝牙 HC-05 向 Arduino 发送一个变量值,也许在串行监视器中打印它。这是我正在使用的代码,但它没有按预期工作。
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice hc05 = btAdapter.getRemoteDevice("98:D3:91:FD:3E:F0");
BluetoothSocket btSocket = null;
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
btSocket.connect();
} catch (IOException e) {
e.printStackTrace();
}
BluetoothSocket finalBtSocket = btSocket;
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = finalBtSocket.getOutputStream();
finalBtSocket.getOutputStream().write("S".toString().getBytes());
//outputStream.write(Integer.parseInt(puk));
} catch (IOException e) {
e.printStackTrace();
}
}
});
我在另一个 Activity 中插入了“puk”变量,并将其传递给这个 Activity。我想要的是能够在串行监视器中打印该变量的值。
如果您有任何可以提供帮助的信息,我们将不胜感激。
尝试使用此代码:
BluetoothSocket btSocket = null;
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
btSocket.connect();
AlertDialog.Builder builder = new AlertDialog.Builder(Status_Poarta.this);
builder.setCancelable(true);
builder.setMessage("Connection is successful");
builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} catch (IOException e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(Status_Poarta.this);
builder.setCancelable(true);
builder.setMessage("No connection established");
builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
BluetoothSocket finalBtSocket = btSocket;
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = finalBtSocket.getOutputStream();
outputStream.write(puk.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
});
我正在使用 Android Studio 创建一个 Android 应用程序。当我按下一个按钮时,我想通过蓝牙 HC-05 向 Arduino 发送一个变量值,也许在串行监视器中打印它。这是我正在使用的代码,但它没有按预期工作。
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice hc05 = btAdapter.getRemoteDevice("98:D3:91:FD:3E:F0");
BluetoothSocket btSocket = null;
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
btSocket.connect();
} catch (IOException e) {
e.printStackTrace();
}
BluetoothSocket finalBtSocket = btSocket;
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = finalBtSocket.getOutputStream();
finalBtSocket.getOutputStream().write("S".toString().getBytes());
//outputStream.write(Integer.parseInt(puk));
} catch (IOException e) {
e.printStackTrace();
}
}
});
我在另一个 Activity 中插入了“puk”变量,并将其传递给这个 Activity。我想要的是能够在串行监视器中打印该变量的值。
如果您有任何可以提供帮助的信息,我们将不胜感激。
尝试使用此代码:
BluetoothSocket btSocket = null;
try {
btSocket = hc05.createRfcommSocketToServiceRecord(mUUID);
btSocket.connect();
AlertDialog.Builder builder = new AlertDialog.Builder(Status_Poarta.this);
builder.setCancelable(true);
builder.setMessage("Connection is successful");
builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} catch (IOException e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(Status_Poarta.this);
builder.setCancelable(true);
builder.setMessage("No connection established");
builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
BluetoothSocket finalBtSocket = btSocket;
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
OutputStream outputStream = finalBtSocket.getOutputStream();
outputStream.write(puk.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
});