3

How to find nearby Wifi devices using swift? How can i find information about devices connected to wifi network using iPhone? Is there some swift code that can provide information about wifi devices? Devices can be like tv or camera or something else.

I know about this answer How to get all available wifi network name listings in iOS

But this answer about finding networks that are connected. I need to find nearby all devices.

i found this link with 1115 lines of code . https://gist.github.com/macropok/3df8ba519163d6e66d1de3b9819d49bd

with code

   import Foundation
   import UIKit
   import Darwin
   import SystemConfiguration.CaptiveNetwork
   import CoreLocation

   class CirrentService : NSObject {

private func getDevicesInRange(completion: @escaping FindDeviceCompletionHandler) {
        let appID:String = CirrentService.sharedService.setAppIdentifier()!
        APIService.sharedService.getDevicesInRange(appID: appID, completion: {
             data, response, error in
            guard let _ = data, error == nil else {
                LogService.sharedService.debug(data: "Get Devices In Range Failed - NO_RESPONSE")
                completion(FIND_DEVICE_RESULT.FAILED_NO_RESPONSE, nil)
                return
            }
            
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            LogService.sharedService.debug(data: "Get Devices In Range Failed - INVALID_STATUS:\(httpStatus.statusCode)")
            completion(FIND_DEVICE_RESULT.FAILED_INVALID_STATUS, nil)
            return
        }
        
        let str = String(data: data!, encoding: .utf8)
        print(str!)
        
        let jsonData:JSON = JSON(data: data!)
        let jsonArray = jsonData["devices"]
        
        var deviceArray:[Device] = [Device]()
        
        for item in jsonArray {
            let device = self.getDeviceFromJson(data: item.1)
            deviceArray.append(device)
        }
        
        if deviceArray.count == 0 {
            LogService.sharedService.debug(data: "Get Devices In Range Failed - NO_DEVICES")
            completion(FIND_DEVICE_RESULT.FAILED_NO_DEVICE, nil)
        }
        else {
            LogService.sharedService.debug(data: "Get Devices In Range Success")
            completion(FIND_DEVICE_RESULT.SUCCESS, deviceArray)
        }
    })
}



 private func findNearByDevices(completion:@escaping FindDeviceCompletionHandler) {
    self.findDeviceTimer = Timer.scheduledTimer(withTimeInterval: Constants.FIND_DEVICE_TIME_INTERVAL, repeats: true, block: {
        t in
        self.getDevicesInRange(completion: {
            result, devices in
            
            if result != FIND_DEVICE_RESULT.SUCCESS {
                if self.maxRetryCount == 0 {
                    self.maxRetryCount = 6
                    self.findDeviceTimer?.invalidate()
                    self.findDeviceTimer = nil
                    completion(result, nil)
                    return
                }
                else {
                    self.maxRetryCount -= 1
                    LogService.sharedService.debug(data: "Left Find Devices Retry Count - \(self.maxRetryCount)")
                    return
                }
            }
            
            self.findDeviceTimer?.invalidate()
            self.findDeviceTimer = nil
            self.model?.setDevices(devices: devices!)
            completion(FIND_DEVICE_RESULT.SUCCESS, devices)
        })
    })
}
user
  • 61
  • 7

0 Answers0