我有一个蓝牙服务器,它从客户端(手机)接收数据。我正在使用的代码如下
@Override
public void run() {
try {
this.localDevice = LocalDevice.getLocalDevice();
this.localDevice.setDiscoverable(DiscoveryAgent.GIAC);
this.server = (StreamConnectionNotifier) Connector.open(URL);
while(true) {
if(this.connection == null) {
this.connection = this.server.acceptAndOpen();
System.out.println("INFO: Bluetooth client connected");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.openInputStream()));
this.writer = new BufferedWriter(new OutputStreamWriter(connection.openOutputStream()));
String line;
while((line = reader.readLine()) != null) {
if(line.equals("--#do:disconnect")) {
break;
}
System.out.println("INFO: Received from Bluetooth: " + line);
}
System.out.println("INFO: Client disconnected");
}
}
} catch(BluetoothStateException ex) {
ex.printStackTrace();
} catch(IOException ex) {
ex.printStackTrace();
}
}
如您所见,我有一个不定式循环来接收消息,直到被告知停止为止。目前,循环接收到所有消息。这是有问题的。使用代码的类是MVC中的模型类。在课堂上,我还有一个名为
getContacts()
的方法。它用于通过蓝牙从电话接收联系人。当服务器发送--#do:getcontacts
时,电话被告知要发送联系人。我需要做的是在
getContacts()
方法的ArrayList中获取联系人并将其作为方法的返回值返回,以便控制器可以处理联系人。public ArrayList<Contact> getContacts() {
ArrayList<Contact> contacts = new ArrayList<>();
// How do I get the contacts in the ArrayList?
return contacts;
}
最佳答案
我会给你一些建议。我的示例不是有效的代码,只是适合您的工作基础。
首先,我强烈建议您在服务器中使用线程。每次客户端连接到服务器时,您都会创建一个新线程,其参数包含启动它所需的所有数据:
boolean running = true; //this class variable will allow you to shut down the server correctly
public void stopServer(){ //this method will shut down the server
this.running = false;
}
public void run() {
...
while(running) {
// if(this.connection == null) { // I removed this line since it's unnecessary, or even harmful!
StreamConnection connection = this.server.acceptAndOpen(); //This line will block until a connection is made...
System.out.println("INFO: Bluetooth client connected");
Thread thread = new ServerThread(connection);
thread.start() //don't forget exception handling...
}
}
并且在ServerThread类中,您实现了以下这些处理客户端的行(未编译的代码,没有异常处理!):
Class ServerThread extends Thread {
StreamConnection connection;
public ServerThread(StreamConnection connection){
this.connection = connection;
}
public void run() {
...
connection.close(); //closing the connection...don't forget exception handling!
System.out.println("INFO: Client disconnected");
}
}
此代码的优点是什么?现在,您的服务器可以同时处理一千个客户端。您已经进行了并行化,这就是服务器通常的工作方式!没有线程的服务器就像没有鞋子的袜子...
其次,如果您有Java客户端和Java服务器,则可以使用一种更简单的方法将对象发送到服务器:ObjectOutputStream / ObjectInputStream。您只需将包含联系人的数组(通常将使用ArraList)发送到服务器,然后读取该数组。这是服务器的代码(同样未编译且没有任何异常处理):
Class ServerThread extends Thread {
StreamConnection connection;
public ServerThread(StreamConnection connection){
this.connection = connection;
}
public void run() {
BufferedInputStream bis = new BufferedInputStream(this.connection.openInputStream());
ObjectInputStream ois = new ObjectInputStream(bis);
ArrayList contacts = (ArrayList) ois.readObject(); //this is a cast: don't forget exception handling!
//You could also try the method ois.readUTF(); especially if you wanna use other non-Java clients
System.out.println("INFO: Received from Bluetooth: " + contacts);
this.connection.close(); //closing the connection...don't forget exception handling!
//ois.close(); //do this instead of "this.connection.close()" if you want the connection to be open...i.e. to receive more data
System.out.println("INFO: Client disconnected");
//here you do whatever you wanna do with the contacts array, maybe add to your other contacts?
}
}
在Java中,每个类都是一个对象,包括ArrayList。而且由于对象的末尾将被视为断开连接,因此您无需执行其他任何操作。
第三:您不仅将上述服务器用于蓝牙连接,而且还将WLAN连接用于aso。然后,您可以轻松地启动不同的线程,例如伪代码
if(connection.isBluetooth()){//create a thread from BluetoothThread} else if(connection.isWLAN()){//create a thread from WLANsThread}
。我不知道您的应用程序是关于什么的,但是也许有一天您想将其扩展到台式机,所以使用WLAN是正确的选择。另外,由于无论如何是蓝牙还是WLAN,无论如何,您都需要在客户端中建立一个验证(“哪些联系人将被发送到哪个服务器?”),因为蓝牙的范围很小,无法为您提供任何安全性。 ;)第四,最后是关于您的问题:要获得某些东西,您需要具有数据源和/或类变量。这是一个简短的示例,其中包含一个存储联系人的文件(但也可以是数据库...本地或其他地方!):
public class MyApp(){
ArrayList contacts;
...
public void run(){ //this happens when we start our app
this.contacts = new ArrayList();
FileReader fr = new FileReader ("C:\WhereverYourFileIs\Contacts.file");
BufferedReader br = new BufferedReader(fr);
//here you use a loop to read the contacts via "br" from the file and fill them into your array...I can't provide you more code, since the exact data structure is up to you.
}
//now we want to send our contacts array to the already connected server:
public sendArrayToServer() {
BufferedOutputStream bos = new BufferedOutputStream (this.connection.openOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this.contacts);
//If you use readUTF() in the server, you need to call here something like oos.writeUTF(this.contacts.toString()); or even need to use another parser method which exactly creates the string you want.
this.connection.close(); //closing the connection...don't forget exception handling!
//oos.close(); //do this instead of "this.connection.close()" if you want the connection to stay open...
}
}
现在,在服务器中,您已经如上所述读出了联系人数组。您对这些联系人的处理方式仍由您决定。
希望这可以帮助您了解问题并找到解决方案。编程是关于反复试验和改进代码的。
编辑:
在讨论之后,我终于找到了您需要的东西:您需要一个名为BluetoothManager的单线程服务器,该服务器与另一个名为GUIController的线程进行交互。现在,无论如何我都在脑海中实现了该实现,因此可以将其连同一些说明一起发布给您。请注意,在这种情况下,您不需要初始化服务器中的另一个线程,因为BluetoothManager已经是一个线程,并且无论如何您只需要同时连接一个(问题仍然存在,如果那是一个“服务器” ,我宁愿称其为“接收者”):
Public class BluetoothManager extends Thread{
boolean running = true; //this class variable will allow you to shut down the server correctly
GUIController controller;
public BluetoothManager(GUIController controller){
this.controller = controller; //this registers the GUIController in the BluetoothManager
}
public void stop(){ //this method will shut down the "server"
this.running = false;
}
public void run() {
this.localDevice = LocalDevice.getLocalDevice();
this.localDevice.setDiscoverable(DiscoveryAgent.GIAC);
this.server = (StreamConnectionNotifier) Connector.open(URL);
while(running){
StreamConnection connection = this.server.acceptAndOpen(); //This line will block until a connection is made...or running==false!
System.out.println("INFO: Bluetooth client connected");
BufferedInputStream bis = new BufferedInputStream(this.connection.openInputStream());
ObjectInputStream ois = new ObjectInputStream(bis);
ArrayList contacts = (ArrayList) ois.readObject(); //this is a cast: don't forget exception handling!
System.out.println("INFO: Received from Bluetooth: " + contacts);
this.connection.close(); //closing the connection...don't forget exception handling!
System.out.println("INFO: Client disconnected");
this.controller.refreshContacts(contacts);
}
}
}
public class GUIController extends Thread implements Runnable {
ArrayList contacts; //also a HashMap may be appropriate
BluetoothManager manager;
public void run(){
this.contacts = new ArrayList();
FileReader fr = new FileReader ("C:\WhereverYourFileIs\Contacts.file");
BufferedReader br = new BufferedReader(fr);
//here you use a loop to read the contacts via "br" from the file and fill them into your array...I can't provide you more code, since the exact data structure is up to you.
}
public void startBluetoothManager(){ //starting the BluetoothManager
this.manager = new BluetoothManager(this);
this.manager.start();
}
public void abortBluetoothManager(){ //call this when clicking on the "Abort" button
this.manager.stop();
//now the next 2 lines you normally don't need...still may use it if you've problems shutting down the thread:
// try{ this.manager.interrupt(); } //we want to be 100% sure to shut down our thread!
// catch(Exception e){}
this.manager = null; //now the garbage collector can clean everything...byebye
}
public void refreshContacts(ArrayList contacts) {
// synchronize(this.contactArray){ //no synchronisation needed if you have a GUI pop-up with an "Abort"-button!
Iterator i = this.contacts.iterator();
while(i.hasNext()){
this.contacts.add(i.next());
}
//At the end you need remove the "Receiving message" pop-up together with the "Abort Receiving"-button, these are all class variables!
// important note: If you have unique entries, you may need to replace them! In this case I suggest storing all contact objects better in a HashMap contacts, and use the unique ID as a key to find the element. And then you may prompt the user, if there are identical entries, to overwrite each entry or not. These things remain all up to you.
}
}
//As always: This is no compiled code!!
GUIController首先使用
startBluetoothManager()
运行BluetoothManager,除显示通知“正在接收联系人”和“中止接收”按钮外,什么也不做。当BluetoothManager完成时,他只需调用refreshContacts(...)
将新联系人添加到GUIController内部现有的contacts-array中。如果按下“中止接收”按钮,则立即调用abortBluetoothManager()
方法,该方法在BluetoothManager中设置running=false
以结束服务器并完成线程。该解决方案解决的主要问题是:两个线程不可能直接相互通信!调用
thread.start()
后,每个线程都是独立的。这就是为什么BluetoothManager线程无法告诉GUIController线程“我已经完成!”的原因。这些线程唯一能做的就是共享相同的资源,并通过该资源进行通信。在我们的例子中,它是GUIController中的contacts
-ArrayList,我首先认为它需要同步,并且可以由两个线程进行更新(但不能同时进行)。而且-有趣的是-还有第二个共享资源,实际上是BluetoothManager类中的running
标志可以将其关闭(但永远不需要running
的任何同步,此变量只能由GUIController更改)。现在,关于同步:我更加思考了这个问题,并且理解了,您也可以解决问题,而无需任何“synchronized(...)”调用。因此,如果您不想同步ArrayList,则必须执行以下操作:服务器运行时,仅显示“正在接收联系人”弹出窗口和“中止接收”按钮。发生这种情况时,您永远不会访问GUIController中的contact-ArrayList。这就是某种“本征同步”,不需要真正的Java同步。仍然可以实现同步,只是要100%确保以后再扩展应用程序时什么都不会发生。
关于java - 如何重写方法中获取消息的循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14676318/