通过蓝牙从Android中的Raspberry Pi接收数据 [英] Receiving data from raspberry pi in android via bluetooth
问题描述
我正在创建一个Android应用,该应用将通过蓝牙连接到Raspberry pi. 我能够将数据发送到Raspberry pi并在终端上可见的问题(我正在使用android中的OutputStream来发送数据),但是无论Raspberry pi正在发送什么内容,我都无法在我的InputStream中得到它.>
我已经阅读了有关使用listenerfcomm来获取另一台设备发送的数据的信息,但是同时使用createrfcomm时,我也具有输入流和输出流.我对使用什么以及如何使用感到困惑.
注意:使用createrfcomm,我可以成功将数据发送到Raspberry pi.仅剩下来自Rasperry pi的数据了.
请提供相应的建议.
使用代码专门回答会更容易,但是我发现API指南示例很有帮助,尽管起初有点脱节:
有一个线程可以连接:
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) { }
}
}
和一个线程来监听并完成工作:
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) { }
}
}
我假设您是否可以发送已具有BluetoothDevice和BluetoothAdapter的信息,并且可以创建并运行连接线程
mConnectThread = new ConnectThread(bluetoothAdapter.getRemoteDevice(deviceAddress));
mConnectThread.start();
在示例中,字节是读取的数据,该数据通过mHandler.obtainMessage发送到UI线程.可以编辑此行以适合您想要对接收到的数据执行的任何操作.
示例来自 http://developer.android.com/guide/topics/connectivity/bluetooth.html
I am creating an android app using which I am going to connect to Raspberry pi over Bluetooth. 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.
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. I am confused as what to use and how to use.
NOTE: Using createrfcomm I am able to send data to Raspberry pi successfully. Only data reception from Rasperry pi is the part that's remaining.
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:
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
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. 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
这篇关于通过蓝牙从Android中的Raspberry Pi接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!