[英]Android sending data to Arduino via Bluetooth
Using various tutorials, I have managed to connect my Android phone to a HC-05 bluetooth module connected to an Arduino. 通过使用各种教程,我设法将Android手机连接到与Arduino连接的HC-05蓝牙模块。 What I'm trying to do is have 5 Buttons set up that will transmit a unique integer per button only when the button is held down, otherwise they will send a "0" when the button is released. 我正在尝试做的是设置5个按钮,仅当按下按钮时,每个按钮才会发送一个唯一的整数;否则,当释放按钮时,它们将发送“ 0”。 ergo BUTTON1 sends a "1" when pressed and "0" when released, BUTTON2 sens a "2" when pressed and a "0" when released. ergo BUTTON1按下时发送“ 1”,松开时发送“ 0”,BUTTON2按下时发送“ 2”,释放时发送“ 0”。 Currently, I cannot figure out how to send ANY data over the connection. 目前,我无法弄清楚如何通过连接发送任何数据。 From reading and watching various tutorials I've gained a small understanding but seem to be missing something. 通过阅读和观看各种教程,我获得了一些了解,但似乎缺少了一些东西。
Towards the bottom of my code in the public void run(), i have set up an OnClickListener for one of my buttons to try to send...well something once its pressed just to see if I can send SOMETHING useful to the Arduino. 在公共void run()的代码底部,我为我的一个按钮设置了一个OnClickListener,以尝试发送...按一下按钮,以便查看是否可以向Arduino发送有用的东西。
Here is where I have my OnClickListener. 这是我的OnClickListener所在的位置。 I believe I should be sending "T" to the Arduino. 我相信我应该将“ T”发送给Arduino。
pUpBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
String testr="T:";
byte[] msgBuffer = testr.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
});
First 第一
Basically as per your requirement you can not use onClickListner instead use onTouchListner 基本上,根据您的要求,您不能使用onClickListner而是使用onTouchListner
Example 例
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//send integer value here.(pressed)
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//Send zero here.(released)
}
}
};
here is a sample code for sending and receiving data from bluetoothSPP 这是用于从bluetoothSPP发送和接收数据的示例代码
this method is to connect bluetooth device to remote device 此方法是将蓝牙设备连接到远程设备
private void connectToDevice(BluetoothDevice mBTDevice) {
try {
SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
mBtSocket = mBTDevice.createRfcommSocketToServiceRecord(SPP_UUID);
mBtSocket.connect();
} catch (IOException e) {
Log.d("connectBT", "while connecting device");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use this for sending bytes. 用它来发送字节。
public void send(String data) {
byte[] buffer = data.getBytes();
try {
mOutputStream = mBtSocket.getOutputStream();
mOutputStream.write(buffer);
Log.d("message", data + " sent");
} catch (IOException e) {
e.printStackTrace();
}
}
use this function for sending Integers 使用此功能发送整数
public void send() {
byte[] buffer = new bytes[size];
buffer[0]=(byte)'1';//prepare data like this
..
..
try {
mOutputStream = mBtSocket.getOutputStream();
mOutputStream.write(buffer);
Log.d("message", " sent");
} catch (IOException e) {
e.printStackTrace();
}
}
Hope this helps :) 希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.