1717import net .sharksystem .asap .android .service2AppMessaging .ASAPServiceRequestNotifyIntent ;
1818
1919import java .io .IOException ;
20+ import java .util .HashMap ;
21+ import java .util .Map ;
2022
2123import static android .bluetooth .BluetoothAdapter .ACTION_DISCOVERY_FINISHED ;
2224import static android .bluetooth .BluetoothAdapter .ACTION_DISCOVERY_STARTED ;
@@ -68,7 +70,11 @@ public void start() {
6870
6971 public void stop () {
7072 Log .d (this .getLogStart (), "stop bluetooth" );
71- this .shutdown ();
73+ try {
74+ this .shutdown ();
75+ } catch (ASAPException e ) {
76+ Log .e (this .getLogStart (), "could not shutdown bt: " + e .getLocalizedMessage ());
77+ }
7278 }
7379
7480 /////////////////////////////////////////////////////////////////////////////////
@@ -95,7 +101,7 @@ private void setup() {
95101 Log .d (this .getLogStart (), "check if BT is enabled" );
96102 if (!mBluetoothAdapter .isEnabled ()) {
97103 Log .i (this .getLogStart (),
98- "Bluetooth disabled - ask application for help - stop setting up bt " );
104+ "Bluetooth disabled - ask application to start BT - stop setting up so far " );
99105
100106 Intent requestIntent = new ASAPServiceRequestNotifyIntent (
101107 ASAPServiceRequestNotifyIntent .ASAP_RQ_ASK_USER_TO_ENABLE_BLUETOOTH );
@@ -185,8 +191,8 @@ public void startDiscoverable(int time) {
185191 * Start a BT scanning sweep of the area. According to android manual, each
186192 * sweep takes 12 seconds - thus that method could be called frequently.
187193 */
188- public boolean startDiscovery () {
189- if (this .mBluetoothAdapter .startDiscovery ()) {
194+ public boolean startDiscovery () throws ASAPException {
195+ if (this .getBTAdapter () .startDiscovery ()) {
190196 Log .d (this .getLogStart (), "successfully started Bluetooth discovery" );
191197 return true ;
192198 } else {
@@ -195,7 +201,7 @@ public boolean startDiscovery() {
195201 }
196202 }
197203
198- private void shutdown () {
204+ private void shutdown () throws ASAPException {
199205 // unregister broadcast receiver
200206 if (this .foundBTDeviceBC != null ) {
201207 Util .unregisterBCR (this .getLogStart (), this .getContext (), this .foundBTDeviceBC );
@@ -219,8 +225,8 @@ private void shutdown() {
219225 }
220226
221227 // stop BT adapter
222- this .mBluetoothAdapter .cancelDiscovery ();
223- this .mBluetoothAdapter .disable ();
228+ this .getBTAdapter () .cancelDiscovery ();
229+ this .getBTAdapter () .disable ();
224230
225231 this .btEnvironmentOn = false ;
226232
@@ -234,14 +240,15 @@ BluetoothAdapter getBTAdapter() throws ASAPException {
234240 if (this .mBluetoothAdapter == null )
235241 throw new ASAPException ("bluetooth not yet initialized" );
236242
243+ Log .d (this .getLogStart (), "my mac address: " + this .mBluetoothAdapter .getAddress ());
237244 return this .mBluetoothAdapter ;
238245 }
239246
240247 /////////////////////////////////////////////////////////////////////////////////////////
241248 // handle connections and connection attempts //
242249 /////////////////////////////////////////////////////////////////////////////////////////
243250
244- void deviceFound (BluetoothDevice btDevice , BluetoothClass btClass ) {
251+ void deviceFound (BluetoothDevice btDevice , BluetoothClass btClass ) throws ASAPException {
245252 String macAddress = btDevice .getAddress ();// MAC address
246253
247254 StringBuilder sb = new StringBuilder ();
@@ -250,7 +257,7 @@ void deviceFound(BluetoothDevice btDevice, BluetoothClass btClass) {
250257 sb .append (" | " );
251258 sb .append (btDevice .getName ());
252259 sb .append ("my address: " );
253- sb .append (this .mBluetoothAdapter .getAddress ());
260+ sb .append (this .getBTAdapter () .getAddress ());
254261 Log .d (this .getLogStart (), sb .toString ());
255262
256263 // strongly recommended to stop discovery
@@ -265,37 +272,46 @@ void deviceFound(BluetoothDevice btDevice, BluetoothClass btClass) {
265272 }
266273 }
267274
275+ private Map <String , BluetoothSocket > openSockets = new HashMap <>();
276+
268277 /**
269278 * Both client and server sockets
270279 * @param socket
271280 * @throws IOException
272281 */
273- void handleBTSocket (BluetoothSocket socket ) throws IOException {
274- Log .d (this .getLogStart (), "new BT connection established" );
275- /* don't check here with shouldConnectToMACPeer if to talk
276- with remote peer.
277-
278- It must be checked before establishing a connection as client!
279- Checked again here would fail - due to missing waiting period.
280-
281- Thus, this must be check in server socket as well. See comments there.
282+ synchronized void handleBTSocket (BluetoothSocket socket ) throws IOException {
283+ String address = socket .getRemoteDevice ().getAddress ();
284+ Log .d (this .getLogStart (), "going to handle new BT connection to " + address );
285+
286+ // already an open connection?
287+ BluetoothSocket bluetoothSocket = this .openSockets .get (address );
288+ if (bluetoothSocket != null ) {
289+ // still active
290+ if (bluetoothSocket .isConnected ()) {
291+ // already connected - connection is active
292+ Log .d (this .getLogStart (),
293+ "we already have an open and active connection to " + address );
294+ socket .close ();
295+ return ;
296+ } else {
297+ Log .d (this .getLogStart (), "we had a connection but it is gone " + address );
298+ }
299+ }
282300
283- more:
284- There is also a race condition between both peers - who will create connection
285- earlier - that mechanism drops the later connection attempt.
286- */
301+ // remember that new connection
302+ this .openSockets .put (address , socket );
287303
288- // set up new ASAP Session and we are done here.
304+ // set up new ASAP Session on that connection
289305 new ASAPConnectionLauncher (socket .getInputStream (), socket .getOutputStream (),
290306 this .getAsapService ().getASAPEngine ()).start ();
291307 }
292308
293- public void propagateStatus (Context ctx ) {
309+ public void propagateStatus (Context ctx ) throws ASAPException {
294310 Log .d (this .getLogStart (), "going to send status broadcast messages" );
295311 ASAPServiceRequestNotifyIntent notifyIntent = null ;
296312
297313 // Bluetooth running?
298- if (this .mBluetoothAdapter .isEnabled ()) {
314+ if (this .getBTAdapter () .isEnabled ()) {
299315 notifyIntent = new ASAPServiceRequestNotifyIntent (
300316 ASAPServiceRequestNotifyIntent .ASAP_NOTIFY_BT_ENVIRONMENT_STARTED );
301317 } else {
@@ -305,7 +321,7 @@ public void propagateStatus(Context ctx) {
305321
306322 ctx .sendBroadcast (notifyIntent );
307323
308- if (this .mBluetoothAdapter .isDiscovering ()) {
324+ if (this .getBTAdapter () .isDiscovering ()) {
309325 notifyIntent = new ASAPServiceRequestNotifyIntent (
310326 ASAPServiceRequestNotifyIntent .ASAP_NOTIFY_BT_DISCOVERY_STARTED );
311327 } else {
0 commit comments