gpt4 book ai didi

java - arduino 发送数据失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:59 25 4
gpt4 key购买 nike

我已成功将 android 应用程序连接到 arduino,但似乎我无法获取数据,即使我使用了输入流......我不知道这几天一直在寻找和尝试其他解决方案有什么问题。 .

安卓工作室代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bluetooth = new BluetoothSPP(this);
text = (TextView) findViewById(R.id.textView2);
connect = (Button) findViewById(R.id.connect);
on = (Button) findViewById(R.id.on);
BluetoothSocket socket = null;
mAdapter = BluetoothAdapter.getDefaultAdapter();

if (!bluetooth.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext(), "Bluetooth is not available", Toast.LENGTH_SHORT).show();
finish();
}

bluetooth.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
connect.setText("Connected to " + name);
}

public void onDeviceDisconnected() {
connect.setText("Connection lost");
}

public void onDeviceConnectionFailed() {
connect.setText("Unable to connect");
}
});

connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetooth.getServiceState() == BluetoothState.STATE_CONNECTED) {
bluetooth.disconnect();
} else {
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}


}
});


on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}


});

/*off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetooth.send(OFF, true);
}
});*/




}






public void onStart() {
super.onStart();
if (!bluetooth.isBluetoothEnabled()) {
bluetooth.enable();
} else {
if (!bluetooth.isServiceAvailable()) {
bluetooth.setupService();
bluetooth.startService(BluetoothState.DEVICE_OTHER);
}
}
}

< /* class ConnectedThread extends Thread {        private InputStream mInStream;

public ConnectedThread(BluetoothSocket socket) throws IOException {
InputStream tmpIn = null;


// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();

} catch (IOException e) {
}

mInStream = tmpIn;

}
}


public void run() {
BufferedReader r = new BufferedReader(new InputStreamReader(mInStream));

while (true) {
try {
int a = r.read();

Log.d(TAG, Integer.toString(a));


} catch (IOException e) {
break;
}
}
}
}*/

void beginListenForData() {
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character

stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
int bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;

handler.post(new Runnable() {
public void run() {
text.setText(data);
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});

workerThread.start();
}
void closeBT() throws IOException {
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();

text.setText("Bluetooth Closed");
}



public void onDestroy() {
super.onDestroy();
bluetooth.stopService();
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if (resultCode == Activity.RESULT_OK)
bluetooth.connect(data);
} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
bluetooth.setupService();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}

Arduino 代码:

enter image description here

最佳答案

您不需要 InputStream 和 OutputStream,因为您没有使用 native android RFCOMM 蓝牙 api。我用谷歌搜索了 BluetoothSPP 类,发现你正在使用这个 github 库进行蓝牙 spp 通信:akexorcist/Android-BluetoothSPPLibrary

您的代码似乎适用于连接/断开您的 arduino 设备。所以,我认为你适本地整合了图书馆。缺少的是发送/接收蓝牙数据的代码。我更新了你的代码如下:

on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetooth.send("ON", true);
}
});

off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetooth.send("OFF", true);
}
});

bluetooth.setOnDataReceivedListener(new OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Toast.makeText(getApplicationContext(), String.format("Data Received: %s", message), Toast.LENGTH_SHORT).show();
}
});

关于java - arduino 发送数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55668562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com