어떻게 스윙 jButton에서 자바 클래스를 사용할 수 있습니까?이 코드는 버튼과 연결하려고합니다. 이 코드는 블루투스 장치를 발견하고 연결된 장치에 텍스트 파일을 보냅니다. 나는 bluecove 라이브러리를 사용했습니다.스윙 JButton에서 Java 클래스를 어떻게 사용할 수 있습니까?
import java.io.OutputStream;
import java.util.ArrayList;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
public class MyDiscoveryListener implements DiscoveryListener{
private static Object lock=new Object();
public ArrayList<RemoteDevice> devices;
public MyDiscoveryListener() {
devices = new ArrayList<RemoteDevice>();
}
public static void main(String[] args) {
MyDiscoveryListener listener = new MyDiscoveryListener();
try{
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, listener);
try {
synchronized(lock){
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
return;
}
System.out.println("Device Inquiry Completed. ");
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID(0x1105); //OBEX Object Push service
int[] attrIDs = new int[] {
0x0100 // Service name
};
for (RemoteDevice device : listener.devices) {
agent.searchServices(
attrIDs,uuidSet,device,listener);
try {
synchronized(lock){
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
return;
}
System.out.println("Service search finished.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) {
String name;
try {
name = btDevice.getFriendlyName(false);
} catch (Exception e) {
name = btDevice.getBluetoothAddress();
}
devices.add(btDevice);
System.out.println("device found: " + name);
}
@Override
public void inquiryCompleted(int arg0) {
synchronized(lock){
lock.notify();
}
}
@Override
public void serviceSearchCompleted(int arg0, int arg1) {
synchronized (lock) {
lock.notify();
}
}
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for (int i = 0; i < servRecord.length; i++) {
String url = servRecord[i].getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, true);
if (url == null) {
continue;
}
DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
if (serviceName != null) {
System.out.println("service " + serviceName.getValue() + " found " + url);
sendMessageToDevice(url);
if(serviceName.getValue().equals("OBEX Object Push")){
sendMessageToDevice(url);
}
} else {
System.out.println("service found " + url);
}
}
}
private static void sendMessageToDevice(String serverURL){
try{
System.out.println("Connecting to " + serverURL);
ClientSession clientSession = (ClientSession) Connector.open(serverURL);
System.out.println(clientSession.toString());
HeaderSet hsConnectReply = clientSession.connect(null);
System.out.println("Reply: " + hsConnectReply.toString());
if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
System.out.println("Failed to connect");
return;
}
HeaderSet hsOperation = clientSession.createHeaderSet();
hsOperation.setHeader(HeaderSet.NAME, "do u want to encrypt/decrypt");
hsOperation.setHeader(HeaderSet.TYPE, "text");
System.out.println("Operation: " + hsOperation.toString());
//Create PUT Operation
Operation putOperation = clientSession.put(hsOperation);
System.out.println("Putt Operation: " + putOperation.toString());
// Send some text to server
byte data[] = "Hello World !!!".getBytes("iso-8859-1");
hsOperation.setHeader(hsOperation.LENGTH, new Long(data.length));
OutputStream os = putOperation.openOutputStream();
System.out.println("Writing the data: " + data[0]);
os.write(data);
os.close();
putOperation.close();
clientSession.disconnect(null);
clientSession.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
가 귀하의 질문에 요구됩니다 이 주제에 대해 자습서 나 유사한 질문을 검색하지 않았 음을 나타내는 방법으로,이 질문 (또는 모든 사이트)에 대해 질문하는 가장 좋은 방법은 아닙니다. 적절한 자습서를 먼저 확인하십시오. 예를 들어,이 간단한 [jbutton 자습서 검색] (https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=jbutton%20tutorial)을 사용하면 기본 튜토리얼 첫 번째 히트에. –
그래서 문제가 무엇입니까? [최소한의 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve)을 읽어보십시오. –