Android-Arduino 蓝牙通信:Android 应用中未正确接收数据
Android-Arduino Bluetooth communication: Data is not received properly in Android app
我正在为 Android-Arduino 蓝牙串行通信创建一个应用程序。我能够成功连接到arduino。我的应用程序可以毫不费力地将数据发送到 arduino,我已经验证过了。但是在从 arduino 接收数据时,我的应用程序只接收到正在发送的数据的一部分。例如,如果从 arduino 发送“404”,我的应用程序只显示收到“4”。
我检查了其他此类应用程序,所有其他应用程序本身都能够接收“404”。所以问题出在我的代码上。
这是我从arduino读取数据的代码:
public String read(byte[] bytes){
try {
mInput.read(bytes);
strInput = new String(bytes);
}catch(Exception e){
e.printStackTrace();
}
return strInput;
}
//mInput is the input stream of bluetooth connection
如您所见,数据被接收到 byte
缓冲区并使用 new String(bytes);
方法转换为字符串。当我烤串时,只有 4
被烤而不是 404
从 arduino 发送。
byte
缓冲区的大小为 256
。
编辑:根据要求 BluetoothManager.java
的完整代码是这样的:
public class BluetoothManager {
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice bluetoothDevice;
private BluetoothSocket bluetoothSocket;
private ConnectedThread connectedThread;
private byte[] buffer;
public BluetoothManager(){
buffer=new byte[256];
bluetoothSocket=null;
bluetoothAdapter=null;
bluetoothDevice=null;
connectedThread=null;
getBluetoothAdapter();
if(!isBluetoothAvailable()){
turnBluetoothOn();
}
scanToConnect();
}
public void turnBluetoothOff(){
try {
bluetoothSocket.close();
bluetoothSocket=null;
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.disable();
bluetoothAdapter=null;
bluetoothDevice=null;
}catch(Exception e){
e.printStackTrace();
}
}
private boolean isBluetoothAvailable(){
return bluetoothAdapter.isEnabled();
}
private void turnBluetoothOn(){
bluetoothAdapter.enable();
}
public String readData(Context context){
String outputString=null;
if(isBluetoothAvailable()) {
outputString = connectedThread.read(buffer);
}else{
Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show();
}
return outputString;
}
public void writeData(String string, Context context){
if(isBluetoothAvailable()) {
connectedThread.write(string.getBytes());
}else{
Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show();
}
}
private void getBluetoothAdapter(){
try{
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
}catch (Exception e){
e.printStackTrace();
}
}
private void scanToConnect(){
Set<BluetoothDevice> pairedDevices=bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
try {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("HC-05")) {
bluetoothDevice = device;
new connectBt(bluetoothDevice);
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
private class connectBt extends Thread {
public connectBt(BluetoothDevice device) {
BluetoothSocket tmp = null;
bluetoothDevice = device;
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
try {
tmp = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = tmp;
run();
}
public void run() {
bluetoothAdapter.cancelDiscovery();
try {
bluetoothSocket.connect();
connectedThread = new ConnectedThread(bluetoothSocket);
} catch (IOException connectException) {
closeSocket();
}
}
private void closeSocket() {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectedThread extends Thread{
private InputStream mInput=null;
private OutputStream mOutput=null;
private String strInput;
public ConnectedThread(BluetoothSocket socket){
bluetoothSocket=socket;
InputStream tmpIn=null;
OutputStream tmpOut=null;
try{
tmpIn=socket.getInputStream();
tmpOut=socket.getOutputStream();
}catch(IOException e){
e.printStackTrace();
closeSocket();
}
mInput=tmpIn;
mOutput=tmpOut;
}
public void write(byte[] bytes){
try{
mOutput.write(bytes);
}catch(IOException e){
e.printStackTrace();
}
}
public String read(byte[] bytes){
try {
mInput.read(bytes);
strInput = new String(bytes);
}catch(Exception e){
e.printStackTrace();
}
return strInput;
}
public void closeSocket(){
try{
bluetoothSocket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Edit-2:在进一步调试时我发现 mInput.available()
returns 0
而 mInput.read(bytes)
returns 1
。为什么在我使用 bluetooth.println("404");
的 arduino 代码中出现这种行为
http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/
尝试一下应该可以。也请提供它的完整代码。可能错误应该出现在您将函数连接到串行事件处理程序的部分。
我遇到了同样的问题。我通过在从 arduino 即 println() 发送时添加定界符 (\n) 并签入 android 代码来解决。在 android 中尝试以下代码,它对我有用:
试试这个代码:http://pastebin.com/sfb3kuu6
好吧,我通过延迟 read()
方法解决了这个问题。
我正在为 Android-Arduino 蓝牙串行通信创建一个应用程序。我能够成功连接到arduino。我的应用程序可以毫不费力地将数据发送到 arduino,我已经验证过了。但是在从 arduino 接收数据时,我的应用程序只接收到正在发送的数据的一部分。例如,如果从 arduino 发送“404”,我的应用程序只显示收到“4”。
我检查了其他此类应用程序,所有其他应用程序本身都能够接收“404”。所以问题出在我的代码上。
这是我从arduino读取数据的代码:
public String read(byte[] bytes){
try {
mInput.read(bytes);
strInput = new String(bytes);
}catch(Exception e){
e.printStackTrace();
}
return strInput;
}
//mInput is the input stream of bluetooth connection
如您所见,数据被接收到 byte
缓冲区并使用 new String(bytes);
方法转换为字符串。当我烤串时,只有 4
被烤而不是 404
从 arduino 发送。
byte
缓冲区的大小为 256
。
编辑:根据要求 BluetoothManager.java
的完整代码是这样的:
public class BluetoothManager {
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice bluetoothDevice;
private BluetoothSocket bluetoothSocket;
private ConnectedThread connectedThread;
private byte[] buffer;
public BluetoothManager(){
buffer=new byte[256];
bluetoothSocket=null;
bluetoothAdapter=null;
bluetoothDevice=null;
connectedThread=null;
getBluetoothAdapter();
if(!isBluetoothAvailable()){
turnBluetoothOn();
}
scanToConnect();
}
public void turnBluetoothOff(){
try {
bluetoothSocket.close();
bluetoothSocket=null;
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.disable();
bluetoothAdapter=null;
bluetoothDevice=null;
}catch(Exception e){
e.printStackTrace();
}
}
private boolean isBluetoothAvailable(){
return bluetoothAdapter.isEnabled();
}
private void turnBluetoothOn(){
bluetoothAdapter.enable();
}
public String readData(Context context){
String outputString=null;
if(isBluetoothAvailable()) {
outputString = connectedThread.read(buffer);
}else{
Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show();
}
return outputString;
}
public void writeData(String string, Context context){
if(isBluetoothAvailable()) {
connectedThread.write(string.getBytes());
}else{
Toast.makeText(context, "Error: Not Connected", Toast.LENGTH_LONG).show();
}
}
private void getBluetoothAdapter(){
try{
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
}catch (Exception e){
e.printStackTrace();
}
}
private void scanToConnect(){
Set<BluetoothDevice> pairedDevices=bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
try {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("HC-05")) {
bluetoothDevice = device;
new connectBt(bluetoothDevice);
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
private class connectBt extends Thread {
public connectBt(BluetoothDevice device) {
BluetoothSocket tmp = null;
bluetoothDevice = device;
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
try {
tmp = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = tmp;
run();
}
public void run() {
bluetoothAdapter.cancelDiscovery();
try {
bluetoothSocket.connect();
connectedThread = new ConnectedThread(bluetoothSocket);
} catch (IOException connectException) {
closeSocket();
}
}
private void closeSocket() {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectedThread extends Thread{
private InputStream mInput=null;
private OutputStream mOutput=null;
private String strInput;
public ConnectedThread(BluetoothSocket socket){
bluetoothSocket=socket;
InputStream tmpIn=null;
OutputStream tmpOut=null;
try{
tmpIn=socket.getInputStream();
tmpOut=socket.getOutputStream();
}catch(IOException e){
e.printStackTrace();
closeSocket();
}
mInput=tmpIn;
mOutput=tmpOut;
}
public void write(byte[] bytes){
try{
mOutput.write(bytes);
}catch(IOException e){
e.printStackTrace();
}
}
public String read(byte[] bytes){
try {
mInput.read(bytes);
strInput = new String(bytes);
}catch(Exception e){
e.printStackTrace();
}
return strInput;
}
public void closeSocket(){
try{
bluetoothSocket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Edit-2:在进一步调试时我发现 mInput.available()
returns 0
而 mInput.read(bytes)
returns 1
。为什么在我使用 bluetooth.println("404");
http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/
尝试一下应该可以。也请提供它的完整代码。可能错误应该出现在您将函数连接到串行事件处理程序的部分。
我遇到了同样的问题。我通过在从 arduino 即 println() 发送时添加定界符 (\n) 并签入 android 代码来解决。在 android 中尝试以下代码,它对我有用:
试试这个代码:http://pastebin.com/sfb3kuu6
好吧,我通过延迟 read()
方法解决了这个问题。