BowlerKernel
BluetoothConnectionPanel.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.ui;
16 
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 
20 import javax.bluetooth.RemoteDevice;
21 import javax.swing.JButton;
22 import javax.swing.JComboBox;
23 import javax.swing.JLabel;
24 import javax.swing.JProgressBar;
25 import javax.swing.JTextArea;
26 
27 import net.miginfocom.swing.MigLayout;
28 
29 import com.neuronrobotics.sdk.common.Log;
30 import com.neuronrobotics.sdk.util.IMonitorable;
31 import com.neuronrobotics.sdk.util.IProgressMonitorListener;
32 import com.neuronrobotics.sdk.util.ProcessMonitor;
33 import com.neuronrobotics.sdk.wireless.bluetooth.BlueCoveManager;
34 import com.neuronrobotics.sdk.wireless.bluetooth.BluetoothSerialConnection;
35 
36 // TODO: Auto-generated Javadoc
41 
43  private static final long serialVersionUID = 1L;
44 
47 
49  private BlueCoveManager blue = null;
50 
52  private boolean displayWarning = false;
53 
55  private JComboBox connectionCbo;
56 
58  private JButton search;
59 
61  private JProgressBar progress = new JProgressBar();
62 
64  private JLabel message = new JLabel();
65 
72  super("Bluetooth", ConnectionImageIconFactory.getIcon("images/bluetooth-icon.png"),connectionDialog);
73 
74  if(displayWarning) {
75  return;
76  }
77 
78  search = new JButton("Search for Devices");
79  search.addActionListener(new ActionListener() {
80 
81  public void actionPerformed(ActionEvent arg0) {
82  refresh();
83  }
84  });
85 
86  setLayout(new MigLayout("", // Layout Constraints
87  "[right][left]", // Column constraints with default align
88  "[center][center]" // Row constraints with default align
89  ));
90 
91  connectionCbo = new JComboBox();
92 
93  add(new JLabel("Connection:"), "cell 0 0");
94  add(connectionCbo);
95  add(search, "wrap");
96  add(progress, "spanx, growx");
97  add(message, "spanx, growx");
98  }
99 
100 
101 
102 
103  /* (non-Javadoc)
104  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#getConnection()
105  */
107  try {
108  String port = connectionCbo.getSelectedItem().toString();
109  RemoteDevice dev = blue.getDevice(port);
110  connection = new BluetoothSerialConnection(blue,dev.getBluetoothAddress());
111  Log.info("Using device:"+port+"\n");
112  } catch(Exception e) {
113  Log.warning("Unable to connect with bluetooth connection");
114  }
115  return connection;
116  }
117 
118 
119  /* (non-Javadoc)
120  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#refresh()
121  */
122  public void refresh() {
123  Log.info("Searching for devices over bluetooth...");
124 
125  search.setEnabled(false);
126 
127  connectionCbo.removeAllItems();
128  connectionCbo.setEnabled(false);
129 
130  progress.setIndeterminate(true);
131 
132  message.setText("Searching...");
133 
135 
136  ProcessMonitor pm = new ProcessMonitor(bsp);
138 
139 
140  public void onUpdate(double value) {
141  // TODO Auto-generated method stub
142 
143  }
144 
145 
146  public void onComplete() {
147  progress.setIndeterminate(false);
148  search.setEnabled(true);
149  connectionCbo.setEnabled(true);
150  }
151  });
152  pm.start();
153  bsp.start();
154  getConnectionDialog().pack();
155  }
156 
160  private class BluetoothSearchProcess extends Thread implements IMonitorable {
161 
163  private boolean isRunning = false;
164 
165  /* (non-Javadoc)
166  * @see java.lang.Thread#run()
167  */
168  public void run() {
169  setName("Bowler Platform Bluetooth connection thread");
170  isRunning = true;
171  try {
172  if (blue == null)
173  blue = new BlueCoveManager();
174  message.setText("Searching for bluetooth devices, please wait...");
175  String [] devices = blue.getAvailableSerialDevices(true);
176  connectionCbo.removeAllItems();
177  for(String s: devices) {
178  Log.info("Adding: "+s);
179  message.setText("Adding " + s);
180  connectionCbo.addItem(s);
181  }
182  if(devices.length == 0) {
183  message.setText("No devices found");
184  }
185  } catch(Exception e) {
186  e.printStackTrace();
187  displayWarning = true;
188  String m = "BlueCove not installed properly, native library not found or missing dependancy\n\n";
189  JTextArea tx = new JTextArea();
190  tx.setBorder(null);
191  tx.setLineWrap(true);
192  tx.setWrapStyleWord(true);
193  tx.setText(m);
194  tx.setColumns(20);
195  removeAll();
196  add(new JLabel(ConnectionImageIconFactory.getIcon("images/dialog-error.png")), "cell 0 0,ax center, ay center");
197  add(tx, "cell 1 0");
198  } finally {
199  if (connection!=null) {
201  }
202 
203  connection = null;
204  isRunning = false;
205  }
206  }
207 
208 
209  /* (non-Javadoc)
210  * @see com.neuronrobotics.sdk.util.IMonitorable#getPercentage()
211  */
212  public double getPercentage() {
213  return 0;
214  }
215 
216 
217  /* (non-Javadoc)
218  * @see com.neuronrobotics.sdk.util.IMonitorable#isComplete()
219  */
220  public boolean isComplete() {
221  return !isRunning;
222  }
223 
224  }
225 }
static void info(String message)
Definition: Log.java:110
static void warning(String message)
Definition: Log.java:101
void addProcessMonitorListener(IProgressMonitorListener listener)