多点连接 - 如何查看找到的设备
Posted
技术标签:
【中文标题】多点连接 - 如何查看找到的设备【英文标题】:Multipeer connectivity- how to view the found devices 【发布时间】:2017-12-20 20:40:35 【问题描述】:我在 Swift 4 中使用 Multipeer Connectivity,我想知道如何在程序运行时查看找到的设备,我得到了一点帮助,但我还是不太明白。我也不知道该放在哪里:
extension viewcontroller : MCBrowserViewControllerDelegate
func browserViewControllerDidFinish(_ browserViewController:
MCBrowserViewController)
serviceBrowser.dismiss(animated: true, completion: nil)
func browserViewControllerWasCancelled(_ browserViewController:
MCBrowserViewController)
serviceBrowser.dismiss(animated: true, completion: nil)
这是我的完整代码,如果需要请更正任何代码,我还将扩展名放在下面代码的底部:
import UIKit
import PlaygroundSupport
import MultipeerConnectivity
PlaygroundPage.current.needsIndefiniteExecution = true
var peerID: MCPeerID!
var mcSession: MCSession!
var mcAdvertiserAssistant: MCAdvertiserAssistant!
var foundPeers = [MCPeerID]()
class viewcontroller: UIViewController
override func viewDidLoad()
var serviceBrowser : MCBrowserViewController!
func startBrowsingForPeer()
serviceBrowser = MCBrowserViewController(serviceType: "lightning417techa", session: mcSession)
serviceBrowser.delegate.self
peerID = MCPeerID(displayName: UIDevice.current.name)
mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .none)
mcSession.delegate.self
class ViewController: UIViewController, MCSessionDelegate, MCBrowserViewControllerDelegate
let session = MCSession(peer: peerID,
securityIdentity: nil,
encryptionPreference: .none)
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState)
switch state
case MCSessionState.connected:
print("Connected: \(peerID.displayName)")
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!)
foundPeers.append(peerID)
print(browser)
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID)
if let image = UIImage(data: data)
DispatchQueue.main.async [unowned self] in
print("image recieved")
func sendImage(img: UIImage)
if mcSession.connectedPeers.count > 0
if let imageData = UIImagePNGRepresentation(img)
do
try mcSession.send(imageData, toPeers: mcSession.connectedPeers, with: .reliable)
catch let error as NSError
let ac = UIAlertController(title: "Send error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID)
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress)
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?)
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController)
dismiss(animated: true)
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController)
dismiss(animated: true)
func startHosting(action: UIAlertAction!)
mcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "hws-ridleyn", discoveryInfo: nil, session: mcSession)
mcAdvertiserAssistant.start()
func joinSession(action: UIAlertAction!)
let mcBrowser = MCBrowserViewController(serviceType: "hws-ridleyn", session: mcSession)
mcBrowser.delegate = self
present(mcBrowser, animated: true)
let vc = UIToolbar(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
// your view controller here
let vcc = ViewController()
vcc.startHosting(action: nil)
ViewController.startHosting(vcc)
PlaygroundPage.current
extension viewcontroller : MCBrowserViewControllerDelegate
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController)
serviceBrowser.dismiss(animated: true, completion: nil)
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController)
serviceBrowser.dismiss(animated: true, completion: nil)
我刚刚编辑了扩展,但它说serviceBrowser
不知何故不正确,它想用NetServiceBrowser
替换它。所以当我这样做的时候,NetServiceBrowser
没有成员 dismiss
。
【问题讨论】:
【参考方案1】:MCSession 包含一个名为 connectedPeers 的变量。
var connectedPeers: [MCPeerID] 当前连接到此会话的所有对等方的数组。
见Apple documentation - MCSession class
【讨论】:
【参考方案2】:如果它是您需要的已找到对等点的列表,请将存储的属性修改为
var foundPeers = [MCPeerID]()
didSet
let peers = foundPeers.map( $0.displayName )
yourFoundPeerLabel.text = String(format: "Found Peers: %@", peers.joined(separator: ", "))
每次找到设备时都会更新 storedProperty,并且当新的对等方附加到列表时,您的标签将自动更新。请参考您的委托方法的实现
func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!);
PS:如果你需要连接设备的列表,创建一个类似的存储属性并使用委托方法
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState);
将设备添加到列表中。逻辑保持不变。
【讨论】:
以上是关于多点连接 - 如何查看找到的设备的主要内容,如果未能解决你的问题,请参考以下文章