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.53 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. }
  140. else{
  141. ps.setString(1, "Absence");
  142. }
  143. conn.close();
  144.  
  145. }
  146. catch(SQLException s1){
  147. s1.getStackTrace();
  148. }
  149. mapDevicePosition.put(intDevicePosition, entry.getValue());
  150. intDevicePosition++;
  151. }
  152. }
  153.  
  154. /* On click of any item in List Box */
  155. private void JListBluetoothDevicesMouseClicked(java.awt.event.MouseEvent evt) {
  156. /* Get bluetooth device details from temporary list */
  157. List<String> tmpDeviceDetails = mapDevicePosition.get(JListBluetoothDevices.getSelectedIndex());
  158. /* Set bluetooth device name */
  159. lblRuntimeDeviceName.setText(tmpDeviceDetails.get(0));
  160. /* Set bluetooth device Address */
  161. lblRuntimeDeviceAddress.setText(tmpDeviceDetails.get(1));
  162. String x=tmpDeviceDetails.get(1);
  163.  
  164. if(tmpDeviceDetails.size() > 2 && tmpDeviceDetails.get(2) != null){
  165. /* Set bluetooth device service name and URL */
  166. JTextAreaServiceDetails.setText(tmpDeviceDetails.get(2));
  167. }else{
  168. JTextAreaServiceDetails.setText("Service not found");
  169. }
  170. }
  171.  
  172. /**
  173. * @param args the command line arguments
  174. */
  175. public static void main(String args[]) {
  176. java.awt.EventQueue.invokeLater(new Runnable() {
  177.  
  178. public void run() {
  179. /* To set new look and feel */
  180. JFrame.setDefaultLookAndFeelDecorated(true);
  181. try {
  182. /**
  183. * Change look and feel of JFrame to Nimbus
  184. * For other look and feel check
  185. * http://www.javaquery.com/2013/06/how-to-applyset-up-swing-look-and-feel.html
  186. */
  187. UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  188. } catch (Exception ex) {
  189. ex.printStackTrace();
  190. }
  191. /* Create an object of BluetoothDevices */
  192. BluetoothDevices bluetoothDevicesFrame = new BluetoothDevices();
  193. /* make BluetoothDevices visible */
  194. bluetoothDevicesFrame.setVisible(true);
  195. }
  196. });
  197. }
  198. // Variables declaration - do not modify
  199. private javax.swing.JList JListBluetoothDevices;
  200. private javax.swing.JTextArea JTextAreaServiceDetails;
  201. private javax.swing.JScrollPane jScrollPane1;
  202. private javax.swing.JScrollPane jScrollPane2;
  203. private javax.swing.JLabel lblDeviceAddress;
  204. private javax.swing.JLabel lblDeviceName;
  205. private javax.swing.JLabel lblRuntimeDeviceAddress;
  206. private javax.swing.JLabel lblRuntimeDeviceName;
  207. private javax.swing.JLabel lblServiceDetails;
  208. // End of variables declaration
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement