通过HC-05蓝牙将Android应用程序中的变量值或字符串发送到Arduino



我正在使用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();
}

}
});

我有";吐;变量,并将其传递给此"活动"。我想要的是能够在串行监视器中打印该变量的值。

如果您有任何可以帮助的信息,我们将不胜感激。

尝试使用以下代码:

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();
}
}
});

相关内容

  • 没有找到相关文章

最新更新