iOS Question WifiNetworkSpecifier + NetworkRequest for B4i

Filippo

Expert
Licensed User
Longtime User
Hi,

For B4a, there is this code snippet from Erel that works very well.

Question: Is there something similar for B4i?

Thank you very much.
Filippo
 

Filippo

Expert
Licensed User
Longtime User
Hi Erel,

Thank you for your code snippet, but unfortunately I can't establish a connection.
I think it's better to create the connection manually and then communicate via the IP address.

In Android, I also have the problem that I can't establish a connection with Android 10, but everything works fine with Android 15.

I wanted to make the whole thing easier for users, but if it's possible, then it has to work that way.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Hi Erel,

I have improved/optimized your code snippet a little with the help of AI.

B4X:
Public Sub ConnectToWifi(SSID as String, Password as String) As ResumableSub
    Dim success As Boolean
    Me.As(NativeObject).RunMethod("connectToWifi:::", Array(SSID, Password, False))
    Wait For Connection_State (Result As Int)
    Sleep(1000)
    Select Result
        Case 0    'Success
            success = True
        Case -1    'Unknown error
        Case 1    'Invalid SSID
        Case 2    'Invalid WPA passphrase
        Case 7    'User denied
        Case 8    'Internal error
        Case 11    'Unknown
        Case 13    'Already associated (bereits verbunden)
            success = True
    End Select
    Return success
End Sub

Public Sub RemoveWifiConfig As ResumableSub
    Dim mgr As NativeObject
    mgr = mgr.Initialize("NEHotspotConfigurationManager").RunMethod("sharedManager", Null)
    mgr.RunMethod("removeConfigurationForSSID:", Array(target_SSID))
    Return True
End Sub

#if OBJC
#import <NetworkExtension/NetworkExtension.h>

- (void)connectToWifi:(NSString*)ssid :(NSString*)password :(BOOL)joinOnce {
        
    NEHotspotConfiguration *config = [[NEHotspotConfiguration alloc]
        initWithSSID:ssid
        passphrase:password
        isWEP:NO];
    
    config.joinOnce = joinOnce;

    [[NEHotspotConfigurationManager sharedManager]
     applyConfiguration:config
     completionHandler:^(NSError * _Nullable error) {
        
        NSInteger code = 0;
        
        if (error) {
            NSString *domain = error.domain;
            NSInteger code = error.code;
            
            [B4I shared].lastError = error;
        }
        
        // Event auch bei "already associated" (13) als success signalisieren
        //BOOL success = (code == 0 || code == 13);
        [self.bi raiseUIEvent:nil event:@"connection_state:" params:@[@(code)]];
    }];
}

#End If
 
Upvote 0
Top