BowlerKernel
BluetoothSerialConnection.java
Go to the documentation of this file.
1 
2 
3 /*******************************************************************************
4  * Copyright 2010 Neuron Robotics, LLC
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  ******************************************************************************/
17 package com.neuronrobotics.sdk.wireless.bluetooth;
18 
19 import javax.bluetooth.RemoteDevice;
20 
21 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
22 import com.neuronrobotics.sdk.common.Log;
23 import com.neuronrobotics.sdk.common.MissingNativeLibraryException;
24 
25 // TODO: Auto-generated Javadoc
30 
32  private int sleepTime = 800;
33 
35  private int pollTimeoutTime = 5;
36 
37 
39  private String bluetoothAddress=null;
40 
42  private final int baud = 115200;
43 
45  private BlueCoveManager blue = null;
46 
48  private boolean connecting=false;
49 
51  private boolean reconOk = false;
52 
64  public BluetoothSerialConnection(BlueCoveManager blue,String deviceAddress) {
65  this.blue = blue;
66  this.bluetoothAddress=deviceAddress;
67  RemoteDevice device=blue.getDevice(bluetoothAddress);
68  if(device== null){
69  throw new RuntimeException();
70  }else{
71  try {
72  String d = device.getBluetoothAddress();
73  Log.info("Device selected: "+d);
74  } catch (Exception e) {
75  // TODO Auto-generated catch block
76  e.printStackTrace();
77  }
78  }
79  init();
80  }
81 
85  private void init(){
87  //
88  }
89 
95  public String getPort() {
96  return bluetoothAddress;
97  }
98 
99  /* (non-Javadoc)
100  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#connect()
101  */
102  @Override
103  public synchronized boolean connect() {
104  if(isConnected()) {
105  Log.error(bluetoothAddress + " is already connected.");
106  return true;
107  }
108  try
109  {
110  setConnected(true);
111  connecting=true;
115  connecting=false;
116  }catch(UnsatisfiedLinkError e){
117  setConnected(false);
118  throw new MissingNativeLibraryException(e.getMessage());
119  }catch (Exception e) {
120  setConnected(false);
121  System.err.println("Failed to connect on port:"+bluetoothAddress+" exception: ");
122  e.printStackTrace();
123  return false;
124  }
125 
126  return isConnected();
127  }
128 
129  /* (non-Javadoc)
130  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#disconnect()
131  */
132  @Override
133  public synchronized void disconnect() {
134 
135  try{
136  super.disconnect();
137  if(getBlueManager() != null){
138  try{getBlueManager().disconnect();}catch(Exception e){}
139  }
140  }catch( UnsatisfiedLinkError e){
141  throw new MissingNativeLibraryException(e.getMessage());
142  }
143  setConnected(false);
144  }
145 
146  /* (non-Javadoc)
147  * @see java.lang.Object#toString()
148  */
149  @Override
150  public String toString() {
151  return bluetoothAddress;
152  }
153 
154 
160  public int getBaud() {
161  return this.baud;
162  }
163 
170  return blue;
171  }
172 
173  /* (non-Javadoc)
174  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#reconnect()
175  */
181  //@Override
182  public boolean reconnect() {
183  //return true;
184  reconOk = false;
185  return recon();
186  }
187 
193  private synchronized boolean recon() {
194  if(!isConnected()||reconOk==true)
195  return reconOk;
196  Log.info("Attempting to re-connect");
197  blue.find();
198  setConnected(false);
199  connect();
200  //This prevents multible resyncs from building up and running
201  reconOk = true;
202  return isConnected();
203  }
204 
205  /* (non-Javadoc)
206  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#waitingForConnection()
207  */
208  @Override
209  public boolean waitingForConnection() {
210  return connecting;
211  }
212 
213 
214 
215 }
static void info(String message)
Definition: Log.java:110
static void error(String message)
Definition: Log.java:92