This commit is contained in:
Prerak Mann 2021-02-28 21:32:51 +05:30
parent 749075fe40
commit 366517fd57
2 changed files with 8 additions and 10 deletions

View File

@ -3,6 +3,7 @@ import MultipeerConnectivity
import SwiftyJSON import SwiftyJSON
class Device: NSObject { class Device: NSObject {
var endpointId: Int
let peerID: MCPeerID let peerID: MCPeerID
var session: MCSession? var session: MCSession?
var state = MCSessionState.notConnected var state = MCSessionState.notConnected
@ -12,6 +13,7 @@ class Device: NSObject {
init(peerID: MCPeerID) { init(peerID: MCPeerID) {
self.peerID = peerID self.peerID = peerID
self.UUID = UUID().uuidString
super.init() super.init()
} }

View File

@ -32,11 +32,7 @@ class MPCManager: NSObject {
var localPeerID: MCPeerID! var localPeerID: MCPeerID!
var enterbackgroundNotification: NSObjectProtocol! var enterbackgroundNotification: NSObjectProtocol!
private var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid private var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
var devices: [Device] = [] { var devices: [Device] = []
didSet {
deviceDidChange?()
}
}
var deviceDidChange: (() -> Void)? var deviceDidChange: (() -> Void)?
@ -76,7 +72,6 @@ class MPCManager: NSObject {
func startAdvertisingPeer() { func startAdvertisingPeer() {
self.advertiser.startAdvertisingPeer() self.advertiser.startAdvertisingPeer()
} }
func startBrowsingForPeers() { func startBrowsingForPeers() {
@ -91,9 +86,9 @@ class MPCManager: NSObject {
self.browser.stopBrowsingForPeers() self.browser.stopBrowsingForPeers()
} }
func invitePeer(deviceID: String) { func invitePeer(endpointId: String) {
do { do {
let device = MPCManager.instance.findDevice(for: deviceID) let device = MPCManager.instance.findDevice(for: endpointId)
if(device?.state == MCSessionState.notConnected){ if(device?.state == MCSessionState.notConnected){
device?.invite(with: self.browser) device?.invite(with: self.browser)
} }
@ -118,9 +113,9 @@ class MPCManager: NSObject {
return device return device
} }
func findDevice(for deviceId: String) -> Device? { func findDevice(for endpointId: String) -> Device? {
for device in self.devices { for device in self.devices {
if device.peerID.displayName == deviceId { return device } if device.endpointId == endpointId { return device }
} }
return nil return nil
} }
@ -168,6 +163,7 @@ extension MPCManager: MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) { func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
// found peer, create a device with this peerID // found peer, create a device with this peerID
addNewDevice(for: peerID) addNewDevice(for: peerID)
} }
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) { func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {