Java 后台Android蓝牙服务?
我是android编程新手。我想建立一个应用程序,可以通过蓝牙与arduino通信 我已经开始了,我有一个活动可以建立连接并维护它。它工作得很好。对于connect和connected线程,我编写了一个自己的类,在我的活动中通过实例验证调用该类 但现在问题来了: 成功连接到Arduino后,当前的活动连接应停止并打开一个新活动,我可以开始发送消息。 但是如何从我在上一个活动中创建的线程发送/接收数据 我读了一些关于服务等方面的内容。? 解决这个问题的最好办法是什么 当我完成了应用程序,它应该有很多活动,我可以通过蓝牙通信不同的数据。因此,我希望第一个活动设置连接,然后我可以从其他活动使用它 公共级蓝牙{Java 后台Android蓝牙服务?,java,android,bluetooth,arduino,Java,Android,Bluetooth,Arduino,我是android编程新手。我想建立一个应用程序,可以通过蓝牙与arduino通信 我已经开始了,我有一个活动可以建立连接并维护它。它工作得很好。对于connect和connected线程,我编写了一个自己的类,在我的活动中通过实例验证调用该类 但现在问题来了: 成功连接到Arduino后,当前的活动连接应停止并打开一个新活动,我可以开始发送消息。 但是如何从我在上一个活动中创建的线程发送/接收数据 我读了一些关于服务等方面的内容。? 解决这个问题的最好办法是什么 当我完成了应用程序,它应该有很
private ConnectThread connection;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
public synchronized void connect(BluetoothDevice device) {
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device);
mConnectThread.start();
}
public 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
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;
}
// Reset the ConnectThread because we're done
synchronized (blueTooth.this) {
mConnectThread = null;
}
// 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) {
}
}
}
public void manageConnectedSocket (BluetoothSocket socket) {
// Cancel the thread that completed the connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
}
/** .................................*/
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) { }
}
}
}
期待您的帮助!您能展示一下您已经尝试过的内容吗?到目前为止,我还没有尝试过任何东西,因为我不知道如何开始。我想不出关于如何从新活动与现有线程对话的任何想法?在我关闭第一个活动后,该线程仍在运行,不是吗?