@@ -689,48 +689,62 @@ private boolean internalConnect(@NonNull final BluetoothDevice device,
689689 // when retrying to create a connection.
690690 if (connectRequest == null )
691691 return false ;
692+
692693 final boolean shouldAutoConnect = connectRequest .shouldAutoConnect ();
693- // We will receive Link Loss events only when the device is connected with autoConnect=true.
694- userDisconnected = !shouldAutoConnect ;
695- // The first connection will always be done with autoConnect = false to make the connection quick.
696- // If the shouldAutoConnect() method returned true, the manager will automatically try to
697- // reconnect to this device on link loss.
694+ final boolean autoConnect ;
698695 if (shouldAutoConnect ) {
699- initialConnection = true ;
696+ // If shouldAutoConnectCreateDirectConnectionFirst() returns true, the first connection
697+ // will always be done with autoConnect = false to make the connection quick.
698+ // If the shouldAutoConnect() method returned true, the manager will automatically try
699+ // to reconnect to this device on link loss.
700+ initialConnection = connectRequest .shouldAutoConnectCreateDirectConnectionFirst ();
701+ autoConnect = !initialConnection ;
702+ } else {
703+ autoConnect = false ;
700704 }
705+ // We will receive Link Loss events only when the device is connected with autoConnect=true.
706+ userDisconnected = !shouldAutoConnect ;
707+
701708 bluetoothDevice = device ;
702- log (Log .VERBOSE , () -> connectRequest .isFirstAttempt () ? "Connecting..." : "Retrying..." );
703- connectionState = BluetoothGatt .STATE_CONNECTING ;
704- postCallback (c -> c .onDeviceConnecting (device ));
705- postConnectionStateChange (o -> o .onDeviceConnecting (device ));
709+ if (!autoConnect ) {
710+ log (Log .VERBOSE , () -> connectRequest .isFirstAttempt () ? "Connecting..." : "Retrying..." );
711+ connectionState = BluetoothGatt .STATE_CONNECTING ;
712+ postCallback (c -> c .onDeviceConnecting (device ));
713+ postConnectionStateChange (o -> o .onDeviceConnecting (device ));
714+ }
706715 connectionTime = SystemClock .elapsedRealtime ();
707716 if (Build .VERSION .SDK_INT > Build .VERSION_CODES .O ) {
708717 // connectRequest will never be null here.
709718 final int preferredPhy = connectRequest .getPreferredPhy ();
710719 log (Log .DEBUG , () ->
711- "gatt = device.connectGatt(autoConnect = false , TRANSPORT_LE, "
720+ "gatt = device.connectGatt(autoConnect = " + autoConnect + " , TRANSPORT_LE, "
712721 + ParserUtils .phyMaskToString (preferredPhy ) + ")" );
713722
714- bluetoothGatt = device .connectGatt (context , false , gattCallback ,
723+ bluetoothGatt = device .connectGatt (context , autoConnect , gattCallback ,
715724 BluetoothDevice .TRANSPORT_LE , preferredPhy , handler );
716725 } else if (Build .VERSION .SDK_INT == Build .VERSION_CODES .O ) {
717726 // connectRequest will never be null here.
718727 final int preferredPhy = connectRequest .getPreferredPhy ();
719728 log (Log .DEBUG , () ->
720- "gatt = device.connectGatt(autoConnect = false , TRANSPORT_LE, "
729+ "gatt = device.connectGatt(autoConnect = " + autoConnect + " , TRANSPORT_LE, "
721730 + ParserUtils .phyMaskToString (preferredPhy ) + ")" );
722731 // A variant of connectGatt with Handled can't be used here.
723732 // Check https://github.com/NordicSemiconductor/Android-BLE-Library/issues/54
724733 // This bug specifically occurs in SDK 26 and is fixed in SDK 27
725- bluetoothGatt = device .connectGatt (context , false , gattCallback ,
734+ bluetoothGatt = device .connectGatt (context , autoConnect , gattCallback ,
726735 BluetoothDevice .TRANSPORT_LE , preferredPhy /*, handler*/ );
727736 } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
728- log (Log .DEBUG , () -> "gatt = device.connectGatt(autoConnect = false , TRANSPORT_LE)" );
729- bluetoothGatt = device .connectGatt (context , false , gattCallback ,
737+ log (Log .DEBUG , () -> "gatt = device.connectGatt(autoConnect = " + autoConnect + " , TRANSPORT_LE)" );
738+ bluetoothGatt = device .connectGatt (context , autoConnect , gattCallback ,
730739 BluetoothDevice .TRANSPORT_LE );
731740 } else {
732- log (Log .DEBUG , () -> "gatt = device.connectGatt(autoConnect = false)" );
733- bluetoothGatt = device .connectGatt (context , false , gattCallback );
741+ log (Log .DEBUG , () -> "gatt = device.connectGatt(autoConnect = " + autoConnect + ")" );
742+ bluetoothGatt = device .connectGatt (context , autoConnect , gattCallback );
743+ }
744+
745+ if (autoConnect && this .connectRequest != null ) {
746+ this .connectRequest .notifySuccess (device );
747+ this .connectRequest = null ;
734748 }
735749 return true ;
736750 }
0 commit comments