[英]Receiving data from raspberry pi in android via bluetooth
I am creating an android app using which I am going to connect to Raspberry pi over Bluetooth. 我正在创建一个Android应用程序,通过它我将通过蓝牙连接到Raspberry pi。 The issue that I am able to send data to Raspberry pi and it is visible on the terminal (I am using OutputStream in android to send data), but whatever Raspberry pi is sending I am not able to get that in my InputStream. 我能够将数据发送到Raspberry pi并在终端上可见的问题(我正在android中使用OutputStream发送数据),但是无论Raspberry pi正在发送什么内容,我都无法在InputStream中获得它。
I have read about using listenrfcomm to get the data sent by another device, but while using createrfcomm also, I have input as well output streams. 我已经读过关于使用listenerfcomm来获取另一台设备发送的数据的信息,但是在同时使用createrfcomm的同时,我也具有输入流和输出流。 I am confused as what to use and how to use. 我对使用什么以及如何使用感到困惑。
NOTE: Using createrfcomm I am able to send data to Raspberry pi successfully. 注意:使用createrfcomm,我可以将数据成功发送到Raspberry pi。 Only data reception from Rasperry pi is the part that's remaining. 仅剩下来自Rasperry pi的数据了。
Please advise accordingly. 请据此告知。
It would be easier to answer specifically with your code, but I found the API guide example helpful although slightly disjointed at first: 使用代码专门回答会更容易,但是我发现API指南示例很有帮助,尽管起初有点脱节:
Have a thread to connect: 有一个线程可以连接:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
} }
and a thread to listen and do the work: 还有一个线程来监听和工作:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
} }
I assume if you can sent that you have BluetoothDevice, and BluetoothAdapter already, and can create and run the connect thread 我假设是否可以发送已具有BluetoothDevice和BluetoothAdapter的信息,并且可以创建并运行连接线程
mConnectThread = new ConnectThread(bluetoothAdapter.getRemoteDevice(deviceAddress));
mConnectThread.start();
In the example bytes is the data read, which is sent to the UI thread with mHandler.obtainMessage. 在示例中,字节是读取的数据,该数据通过mHandler.obtainMessage发送到UI线程。 This line can be edited to suit whatever you want to do with the received data. 可以编辑此行以适合您想要对接收到的数据执行的任何操作。
Example comes from http://developer.android.com/guide/topics/connectivity/bluetooth.html 示例来自http://developer.android.com/guide/topics/connectivity/bluetooth.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.