Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. package soluciones;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.io.OutputStreamWriter;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Scanner;
  13.  
  14. import javax.bluetooth.DataElement;
  15. import javax.bluetooth.DeviceClass;
  16. import javax.bluetooth.DiscoveryAgent;
  17. import javax.bluetooth.DiscoveryListener;
  18. import javax.bluetooth.LocalDevice;
  19. import javax.bluetooth.RemoteDevice;
  20. import javax.bluetooth.ServiceRecord;
  21. import javax.bluetooth.UUID;
  22. import javax.microedition.io.Connector;
  23. import javax.microedition.io.StreamConnection;
  24. import javax.microedition.io.StreamConnectionNotifier;
  25.  
  26. public class Cliente {
  27. public String chatURL;
  28.  
  29. public void startClient() throws IOException, InterruptedException {
  30.  
  31. List<RemoteDevice> devicesDiscovered = new ArrayList<RemoteDevice>();
  32. final Object inquiryCompletedEvent = new Object();
  33.  
  34. devicesDiscovered.clear();
  35.  
  36. DiscoveryListener listener = new DiscoveryListener() {
  37.  
  38. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  39. System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
  40. devicesDiscovered.add(btDevice);
  41. try {
  42. System.out.println(" Name: " + btDevice.getFriendlyName(false));
  43. System.out.println(" BT Address: " + btDevice.getFriendlyName(false));
  44. } catch (IOException cantGetDeviceName) {
  45. }
  46. }
  47.  
  48. public void inquiryCompleted(int discType) {
  49. System.out.println("Device Inquiry completed!");
  50. synchronized(inquiryCompletedEvent){
  51. inquiryCompletedEvent.notifyAll();
  52. }
  53. }
  54.  
  55. public void serviceSearchCompleted(int transID, int respCode) {
  56. System.out.println("Service search completed!");
  57. synchronized(inquiryCompletedEvent){
  58. inquiryCompletedEvent.notifyAll();
  59. }
  60. }
  61.  
  62. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  63. for (int i = 0; i < servRecord.length; i++) {
  64. String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
  65. if (url == null) {
  66. continue;
  67. }
  68. DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
  69.  
  70. if (serviceName != null) {
  71. System.out.println("Service name: " + serviceName.getValue() + ". Service url: " + url);
  72. if(serviceName.getValue().toString().equals("newchat")) {
  73. chatURL=url;
  74. }
  75. } else {
  76. System.out.println("Service name: " + url);
  77. }
  78. }
  79. }
  80. };
  81.  
  82. synchronized(inquiryCompletedEvent) {
  83. boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
  84. if (started) {
  85. System.out.println("Client Searching for devices...");
  86. inquiryCompletedEvent.wait();
  87. System.out.println(devicesDiscovered.size() + " device(s) found");
  88. }
  89. }
  90. if (!devicesDiscovered.isEmpty()){
  91. UUID uuids[] = new UUID[1];
  92. uuids[0] = new UUID(0x1101); // SerialPort
  93. int attridset[] = new int[1];
  94. attridset[0] = 0x0100;
  95.  
  96. for (RemoteDevice d : devicesDiscovered) {
  97. System.out.println(
  98. "Services available on the device " + d.getBluetoothAddress() + " - " + d.getFriendlyName(false));
  99. LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attridset, uuids, d, listener);
  100. synchronized (inquiryCompletedEvent) {
  101. inquiryCompletedEvent.wait();
  102. }
  103. }
  104.  
  105. if(chatURL!=null) {
  106. StreamConnection con = (StreamConnection) Connector.open(chatURL);
  107. Scanner sc = new Scanner(System.in);
  108. String response= " ";
  109. String mensaje = " ";
  110.  
  111.  
  112. RemoteDevice dev = RemoteDevice.getRemoteDevice(con);
  113. System.out.println("BT Address of the remote device: " + dev.getBluetoothAddress());
  114. System.out.println("Name of the remote device: " + dev.getFriendlyName(false));
  115.  
  116. InputStream is = con.openInputStream();
  117. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  118.  
  119. OutputStream os = con.openOutputStream();
  120. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
  121.  
  122.  
  123. do {
  124. System.out.println("Client ->");
  125. mensaje=sc.nextLine();
  126. bw.write(mensaje);
  127. bw.newLine();
  128. bw.flush();
  129. System.out.println("Waiting for service response...");
  130. response=br.readLine();
  131. System.out.println(dev.getBluetoothAddress() + " - " + dev.getFriendlyName(false) + ": " + response);
  132.  
  133. }while(!mensaje.equals("FIN"));
  134.  
  135. System.out.println("Device " + dev.getBluetoothAddress() + " - " + dev.getFriendlyName(false) + " disconnected successfully");
  136. br.close();
  137. bw.close();
  138. sc.close();
  139. mensaje = "";
  140. con.close();
  141. }
  142. }
  143. }
  144.  
  145. public static void main(String args[]) throws IOException, InterruptedException {
  146. Cliente cliente = new Cliente();
  147. cliente.startClient();
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement