它会更容易与您的代码明确回答,但我发现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();
在这个例子中字节的数据读取,被发送到UI线程与mHandler.obtainMessage。这行可以编辑,以适应你想要处理的数据。
例子来自http://developer.android.com/guide/topics/connectivity/bluetooth.html
在你所提到的,我们使用createRfcommSocketToServiceRecord()的“ConnectThread”,但如果我有收到来自其他设备发送的数据,是否有必要,我必须使用listenRfcomm?我很困惑如何放置线程的执行,以便听取以及发送任何消息。 –
我刚刚更新了答案,希望能够增加一些清晰度。让我知道,如果它不清除事情。 – Nathan
我的代码中有ConnectThread和ConnectedThread,我已经提到我能够通过蓝牙从我的设备成功发送数据到Raspberry Pi。我只需要问,如果树莓派正在发送一些数据,我如何通过蓝牙读取设备中的数据......? ConnectedThread mmInStream.read()是否会做这个部分,或者我需要设置一些'listenrfcomm'? 我希望我明白我现在向你要求的东西。 –