99import android .content .IntentFilter ;
1010import android .util .Log ;
1111
12+ import net .sharksystem .asap .ASAPEncounterManager ;
1213import net .sharksystem .asap .ASAPException ;
14+ import net .sharksystem .asap .EncounterConnectionType ;
1315import net .sharksystem .asap .android .service .MacLayerEngine ;
1416import net .sharksystem .asap .android .service .ASAPService ;
1517import net .sharksystem .asap .android .Util ;
1618import net .sharksystem .asap .android .service2AppMessaging .ASAPServiceRequestNotifyIntent ;
19+ import net .sharksystem .streams .StreamPair ;
20+ import net .sharksystem .streams .StreamPairImpl ;
1721
18- import java .io .DataInputStream ;
19- import java .io .DataOutputStream ;
2022import java .io .IOException ;
21- import java .net .Socket ;
2223import java .util .ArrayList ;
2324import java .util .HashMap ;
2425import java .util .List ;
@@ -44,10 +45,10 @@ public class BluetoothEngine extends MacLayerEngine {
4445 private boolean btEnvironmentOn = false ;
4546 private BluetoothServerSocketThread btServerSocketThread ;
4647
47- public static BluetoothEngine getASAPBluetoothEngine (ASAPService ASAPService ,
48+ public static BluetoothEngine getASAPBluetoothEngine (ASAPService asapService ,
4849 Context context ) {
4950 if (BluetoothEngine .engine == null ) {
50- BluetoothEngine .engine = new BluetoothEngine (ASAPService , context );
51+ BluetoothEngine .engine = new BluetoothEngine (asapService , context );
5152 }
5253
5354 return BluetoothEngine .engine ;
@@ -445,43 +446,17 @@ public void checkConnectionStatus() {
445446 /**
446447 * Both client and server sockets
447448 * @param socket
448- * @param isClient for debugging: who is calling client socket / server socket?
449+ * @param initiator for debugging: who is calling client socket / server socket?
449450 * @throws IOException
450451 */
451- void handleBTSocket (BluetoothSocket socket , boolean isClient ) throws IOException {
452+ void handleBTSocket (BluetoothSocket socket , boolean initiator ) throws IOException {
452453 String remoteMacAddress = socket .getRemoteDevice ().getAddress ();
453454
454- String logMessage = isClient ? "Client" : "Server" ;
455- logMessage += "socket called: handle new BT connection" + socket ;
455+ StreamPair streamPair = StreamPairImpl . getStreamPairWithEndpointAddress (
456+ socket . getInputStream (), socket . getOutputStream (), remoteMacAddress ) ;
456457
457- Log .d (this .getLogStart (), logMessage + remoteMacAddress );
458-
459- // already an open connection?
460- if (this .checkAlreadyConnectedWithDevice (remoteMacAddress )) {
461- socket .close ();
462- return ;
463- }
464-
465- // avoid the nasty race condition
466- boolean waited = this .waitBeforeASAPSessionLaunch (
467- socket .getInputStream (),
468- socket .getOutputStream (),
469- isClient , 500 );
470-
471- // ask again?
472- if (waited ) {
473- if (this .checkAlreadyConnectedWithDevice (remoteMacAddress )) {
474- socket .close ();
475- return ;
476- }
477- }
478-
479- // remember that new connection
480- Log .d (this .getLogStart (), "remember socket: " + socket );
481- this .openSockets .put (remoteMacAddress , socket );
482-
483- Log .d (this .getLogStart (), "launch asap session" );
484- this .launchASAPConnection (remoteMacAddress , socket .getInputStream (), socket .getOutputStream ());
458+ this .getAsapService ().getASAPEncounterManager ().handleEncounter (
459+ streamPair , EncounterConnectionType .AD_HOC_LAYER_2_NETWORK , initiator );
485460 }
486461
487462 public void propagateStatus (Context ctx ) throws ASAPException {
0 commit comments