Advertisement
Guest User

BluetoothDevices.java

a guest
Mar 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. package com.javaquery.swing;
  2.  
  3. import com.javaquery.bluetooth.ServicesSearch;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.swing.DefaultListModel;
  8. import javax.swing.JFrame;
  9. import javax.swing.UIManager;
  10. import java.sql.*;
  11.  
  12. public class BluetoothDevices extends javax.swing.JFrame {
  13.  
  14. /* DefaultListModel to attach it with JList */
  15. private DefaultListModel defaultModel;
  16. /* Map to get device details list */
  17. private Map<String, List<String>> mapReturnResult = new HashMap<String, List<String>>();
  18. /* Map to identify device on user click of JList */
  19. private Map<Integer, List<String>> mapDevicePosition = new HashMap<Integer, List<String>> ();
  20.  
  21. public BluetoothDevices() {
  22. initComponents();
  23. defaultModel = new DefaultListModel();
  24. }
  25.  
  26. @SuppressWarnings("unchecked")
  27. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  28. private void initComponents() {
  29.  
  30. jScrollPane1 = new javax.swing.JScrollPane();
  31. JListBluetoothDevices = new javax.swing.JList();
  32. lblDeviceName = new javax.swing.JLabel();
  33. lblRuntimeDeviceName = new javax.swing.JLabel();
  34. lblDeviceAddress = new javax.swing.JLabel();
  35. lblRuntimeDeviceAddress = new javax.swing.JLabel();
  36. lblServiceDetails = new javax.swing.JLabel();
  37. jScrollPane2 = new javax.swing.JScrollPane();
  38. JTextAreaServiceDetails = new javax.swing.JTextArea();
  39.  
  40. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  41. setTitle("Bluecove Bluetooth Discovery");
  42. setResizable(false);
  43. addWindowListener(new java.awt.event.WindowAdapter() {
  44. public void windowOpened(java.awt.event.WindowEvent evt) {
  45. formWindowOpened(evt);
  46. }
  47. });
  48.  
  49. JListBluetoothDevices.addMouseListener(new java.awt.event.MouseAdapter() {
  50. public void mouseClicked(java.awt.event.MouseEvent evt) {
  51. JListBluetoothDevicesMouseClicked(evt);
  52. }
  53. });
  54. jScrollPane1.setViewportView(JListBluetoothDevices);
  55.  
  56. lblDeviceName.setText("Bluetooth Device Name");
  57.  
  58. lblDeviceAddress.setText("Bluetooth Device Address");
  59.  
  60. lblServiceDetails.setText("Service Details");
  61.  
  62. JTextAreaServiceDetails.setColumns(20);
  63. JTextAreaServiceDetails.setRows(5);
  64. jScrollPane2.setViewportView(JTextAreaServiceDetails);
  65.  
  66. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  67. getContentPane().setLayout(layout);
  68. layout.setHorizontalGroup(
  69. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70. .addGroup(layout.createSequentialGroup()
  71. .addContainerGap()
  72. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
  73. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  74. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75. .addGroup(layout.createSequentialGroup()
  76. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  77. .addComponent(lblDeviceName)
  78. .addComponent(lblDeviceAddress))
  79. .addGap(73, 73, 73)
  80. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  81. .addComponent(lblRuntimeDeviceAddress)
  82. .addComponent(lblRuntimeDeviceName, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
  83. .addComponent(lblServiceDetails)
  84. .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE))
  85. .addContainerGap())
  86. );
  87. layout.setVerticalGroup(
  88. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89. .addGroup(layout.createSequentialGroup()
  90. .addContainerGap()
  91. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92. .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
  93. .addGroup(layout.createSequentialGroup()
  94. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  95. .addComponent(lblDeviceName)
  96. .addComponent(lblRuntimeDeviceName))
  97. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  98. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  99. .addComponent(lblDeviceAddress)
  100. .addComponent(lblRuntimeDeviceAddress))
  101. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  102. .addComponent(lblServiceDetails)
  103. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  104. .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)))
  105. .addContainerGap())
  106. );
  107.  
  108. pack();
  109. }// </editor-fold>
  110.  
  111. /* Search for bluetooth device when window opened */
  112. private void formWindowOpened(java.awt.event.WindowEvent evt) {
  113. int intDevicePosition = 0;
  114. JListBluetoothDevices.setModel(defaultModel);
  115.  
  116. /* Create an object of ServicesSearch */
  117. ServicesSearch ss = new ServicesSearch();
  118. /* Get bluetooth device details */
  119. mapReturnResult = ss.getBluetoothDevices();
  120.  
  121. /* Add devices in JList */
  122. for (Map.Entry<String, List<String>> entry : mapReturnResult.entrySet()) {
  123. defaultModel.addElement(entry.getValue().get(0));
  124. String s=(String)entry.getValue().get(1);
  125. String x="D4A148C2FDC8";
  126. if(s.equals(x)){
  127. System.out.println("Present");
  128. }
  129. else{
  130. System.out.println("Absent");
  131. }
  132. mapDevicePosition.put(intDevicePosition, entry.getValue());
  133. intDevicePosition++;
  134. }
  135. }
  136.  
  137. /* On click of any item in List Box */
  138. private void JListBluetoothDevicesMouseClicked(java.awt.event.MouseEvent evt) {
  139. /* Get bluetooth device details from temporary list */
  140. List<String> tmpDeviceDetails = mapDevicePosition.get(JListBluetoothDevices.getSelectedIndex());
  141. /* Set bluetooth device name */
  142. lblRuntimeDeviceName.setText(tmpDeviceDetails.get(0));
  143. /* Set bluetooth device Address */
  144. lblRuntimeDeviceAddress.setText(tmpDeviceDetails.get(1));
  145. String x=tmpDeviceDetails.get(1);
  146.  
  147. if(tmpDeviceDetails.size() > 2 && tmpDeviceDetails.get(2) != null){
  148. /* Set bluetooth device service name and URL */
  149. JTextAreaServiceDetails.setText(tmpDeviceDetails.get(2));
  150. }else{
  151. JTextAreaServiceDetails.setText("Service not found");
  152. }
  153. }
  154.  
  155. /**
  156. * @param args the command line arguments
  157. */
  158. public static void main(String args[]) {
  159. java.awt.EventQueue.invokeLater(new Runnable() {
  160.  
  161. public void run() {
  162. /* To set new look and feel */
  163. JFrame.setDefaultLookAndFeelDecorated(true);
  164. try {
  165. /**
  166. * Change look and feel of JFrame to Nimbus
  167. * For other look and feel check
  168. * http://www.javaquery.com/2013/06/how-to-applyset-up-swing-look-and-feel.html
  169. */
  170. UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  171. } catch (Exception ex) {
  172. ex.printStackTrace();
  173. }
  174. /* Create an object of BluetoothDevices */
  175. BluetoothDevices bluetoothDevicesFrame = new BluetoothDevices();
  176. /* make BluetoothDevices visible */
  177. bluetoothDevicesFrame.setVisible(true);
  178. }
  179. });
  180. }
  181. // Variables declaration - do not modify
  182. private javax.swing.JList JListBluetoothDevices;
  183. private javax.swing.JTextArea JTextAreaServiceDetails;
  184. private javax.swing.JScrollPane jScrollPane1;
  185. private javax.swing.JScrollPane jScrollPane2;
  186. private javax.swing.JLabel lblDeviceAddress;
  187. private javax.swing.JLabel lblDeviceName;
  188. private javax.swing.JLabel lblRuntimeDeviceAddress;
  189. private javax.swing.JLabel lblRuntimeDeviceName;
  190. private javax.swing.JLabel lblServiceDetails;
  191. // End of variables declaration
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement