【发布时间】:2018-07-12 04:15:16
【问题描述】:
我正在用java编写程序,当我点击按钮时,它会将我从文本字段获取的字符串传递给java类并通过蓝牙发送。问题是当我运行我的 jframe(RegistrasiForm)时,我的 java 类(SendtoAndroid.java)似乎没有运行。你能告诉我我的程序有什么问题吗?谢谢。
这是我的 jFrame(RegistrasiForm) 代码:
private void cmd_sendActionPerformed(java.awt.event.ActionEvent evt) {
try{
JSONObject obj = new JSONObject();
obj.put("user_id",user_id_text.getText());
obj.put("password",password_text.getText());
String kirim = obj.toString();
SendtoAndroid st=new SendtoAndroid();
st.setName(kirim);
}catch (Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
这是我的 java 类 (SendtoAndroid.java) 代码:
公共类 SendtoAndroid {
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void startServer() throws IOException, JSONException{
//Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Server Bluetooth 2";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection=streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//takes string from setName methode
String kirim = name;
//send it via bluetooth
byte[] msgBuffer = kirim.getBytes();
OutputStream outStream=connection.openOutputStream();
outStream.write(msgBuffer);
outStream.flush();
outStream.close();
streamConnNotifier.close();
}}
【问题讨论】: