How do i set up a wifi connection via QR Code/ZXing/Unity?

Viewed 26

I've got the following problem. Currently Im working on a QR Code Scanner for Unity(C#). Im using the ZXing lib to read the QR Code. My code works to this point, but all I get is a string with the Wifi informations like password and SSID. I want to connect with the wifi tho not just getting the QR Code informations.

So this is my code:

using System
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using ZXing;
using ZXing.QrCode.Internal;
using System.Net;


public class QRScan : MonoBehaviour
{

    

    //Initialize

    [SerializeField]
    private RawImage QR_CodeField;

    [SerializeField]
    private AspectRatioFitter aspectRartioFitter;
    
    [SerializeField]
     private RectTransform Scanner;

    public TextMeshProUGUI Infotext;

   
     private bool isCamAvaible;
    private WebCamTexture backCam;
    public GameObject ScanButton;
    public GameObject StartButton;

    //public GameObject CancelButton;
    public GameObject Error;

    public GameObject QR_Picture;

    private string SSID;
    private string password;


public void StartScan() {

    Camera();
    QR_Picture.SetActive(false);
    StartButton.SetActive(false);
    ScanButton.SetActive(true);
    StartCoroutine(ContinousScan());
   
} 
private void Update(){
    
    UpdateCamera();
}

private IEnumerator ContinousScan(){
    Scan();
    yield return new WaitForSeconds(0.5f);
    StartCoroutine(ContinousScan());
    
}

    
public void Scan() {

     //QR Code Reader
try {
    IBarcodeReader barcodeReader = new BarcodeReader();
    var data = barcodeReader.Decode(backCam.GetPixels32(), backCam.width, backCam.height);
   
   if (data != null)
    {
    //Data in String - ceck
        Infotext.text= data.Text;
        string decoded = data.ToString().Trim();
        string[] splitted = decoded.Split(';',':'); 
        SSID = splitted[4];
        password = splitted[6];
        

        
        Debug.Log(decoded);
        Debug.Log(string.Join("\n", splitted));
    
        
    }
    else{
        Infotext.text = "Failed to read QR Code";

        Error.SetActive(true);
    }
        }
catch(Exception e){
        Debug.Log("'" + e.Message + "'");
        
    }
    
}

//public void ScanOnClick(){
//    Scan();
//}

private void Camera(){

    //Camera Setup

    WebCamDevice[] devices = WebCamTexture.devices;

    if(devices.Length == 0)
    {
        Debug.Log("No cam");
        isCamAvaible = false;
        return;

    }    
    
    for (int i=0; i< devices.Length; i++) 
    {   
        if(devices[i].isFrontFacing==false){
            backCam = new WebCamTexture(devices[i].name,(int)Scanner.rect.width, (int)Scanner.rect.height);
        }

    
}
    if(backCam == null){

        Debug.Log("Unable to find back cam");
        return;
    }

    backCam.Play();
    QR_CodeField.texture = backCam;

    isCamAvaible =true;  

}

private void UpdateCamera(){
    
    if (!isCamAvaible)
         return;

    float ratio= (float)backCam.width/(float)backCam.height; 
     aspectRartioFitter.aspectRatio = ratio;


     float scaleY = backCam.videoVerticallyMirrored ? -1f: 1f;
     QR_CodeField.rectTransform.localScale= new Vector3(1f,scaleY,1f);

     int orient = -backCam.videoRotationAngle;
     QR_CodeField.rectTransform.localEulerAngles  = new Vector3(0,0, orient) ;
    
}





0 Answers
Related