Advertisement
Guest User

BluetoothDevices.java

a guest
Mar 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 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. try{
  126. Class.forName("com.mysql.jdbc.Driver");
  127. }
  128. catch(ClassNotFoundException e){
  129. System.out.println("Driver not found");
  130. System.exit(0);
  131. }
  132. try{
  133. Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Attendance","root","123");
  134. PreparedStatement ps=conn.prepareStatement("select * from CSE_SPRING_16 where MAC_ADDRESS=?");
  135. ps.setString(1, s);
  136. ResultSet rs=ps.executeQuery();
  137. if(rs.next()){
  138. ps.setString(1, "Present");
  139. System.out.println("Present");
  140. }
  141. else{
  142. ps.setString(1, "Absence");
  143. System.out.println("Absence");
  144. }
  145. conn.close();
  146.  
  147. }
  148. catch(SQLException s1){
  149. s1.getStackTrace();
  150. }
  151. mapDevicePosition.put(intDevicePosition, entry.getValue());
  152. intDevicePosition++;
  153. }
  154. }
  155.  
  156. /* On click of any item in List Box */
  157. private void JListBluetoothDevicesMouseClicked(java.awt.event.MouseEvent evt) {
  158. /* Get bluetooth device details from temporary list */
  159. List<String> tmpDeviceDetails = mapDevicePosition.get(JListBluetoothDevices.getSelectedIndex());
  160. /* Set bluetooth device name */
  161. lblRuntimeDeviceName.setText(tmpDeviceDetails.get(0));
  162. /* Set bluetooth device Address */
  163. lblRuntimeDeviceAddress.setText(tmpDeviceDetails.get(1));
  164. String x=tmpDeviceDetails.get(1);
  165.  
  166. if(tmpDeviceDetails.size() > 2 && tmpDeviceDetails.get(2) != null){
  167. /* Set bluetooth device service name and URL */
  168. JTextAreaServiceDetails.setText(tmpDeviceDetails.get(2));
  169. }else{
  170. JTextAreaServiceDetails.setText("Service not found");
  171. }
  172. }
  173.  
  174. /**
  175. * @param args the command line arguments
  176. */
  177. public static void main(String args[]) {
  178. java.awt.EventQueue.invokeLater(new Runnable() {
  179.  
  180. public void run() {
  181. /* To set new look and feel */
  182. JFrame.setDefaultLookAndFeelDecorated(true);
  183. try {
  184. /**
  185. * Change look and feel of JFrame to Nimbus
  186. * For other look and feel check
  187. * http://www.javaquery.com/2013/06/how-to-applyset-up-swing-look-and-feel.html
  188. */
  189. UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  190. } catch (Exception ex) {
  191. ex.printStackTrace();
  192. }
  193. /* Create an object of BluetoothDevices */
  194. BluetoothDevices bluetoothDevicesFrame = new BluetoothDevices();
  195. /* make BluetoothDevices visible */
  196. bluetoothDevicesFrame.setVisible(true);
  197. }
  198. });
  199. }
  200. // Variables declaration - do not modify
  201. private javax.swing.JList JListBluetoothDevices;
  202. private javax.swing.JTextArea JTextAreaServiceDetails;
  203. private javax.swing.JScrollPane jScrollPane1;
  204. private javax.swing.JScrollPane jScrollPane2;
  205. private javax.swing.JLabel lblDeviceAddress;
  206. private javax.swing.JLabel lblDeviceName;
  207. private javax.swing.JLabel lblRuntimeDeviceAddress;
  208. private javax.swing.JLabel lblRuntimeDeviceName;
  209. private javax.swing.JLabel lblServiceDetails;
  210. // End of variables declaration
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement