2016-09-30 173 views
2

雨燕3.0無法通過Multipeer連接

我可以從iPhone通過WiFi使用Multipeer連接而不是通過藍牙共享數據將iPhone連接藍牙,它看起來對我來說,無線網絡連接不穩定。
Wifi將連接一半時間,藍牙將看到對等點,但不會連接。
將IPhone連接到iPhone和將IPhone連接到本地任何其他類型的電話的最佳方式是什麼?一個例子就是在沒有互聯網或沒有Wifi的情況下。
我也將最終將數據發送到服務器,並從該服務器接收數據。

這會給我3個連接選擇:

1.私人使用(藍牙)?
2.Semi私人使用(WiFi版)辦公室到辦公室
3.公共使用站立並排側(Internet)的全球

這段代碼錯誤的藍牙連接?

import UIKit 
import MultipeerConnectivity 



class ViewController: UIViewController, MCSessionDelegate, MCBrowserViewControllerDelegate, UINavigationControllerDelegate , UIImagePickerControllerDelegate{ 



//MARK: - Variables 
var myDictionary:NSDictionary = [:] 

//Variables for Peer to Peer. 
var browser : MCBrowserViewController! 
var assistant : MCAdvertiserAssistant! 
var session : MCSession! 
var peerID : MCPeerID! 

//Variables for Peer to Peer. 
let imagePicker = UIImagePickerController() 
var imageDataVar: NSData! 



//MARK: - Labels 
@IBOutlet weak var firstNameLabel: UILabel! 
@IBOutlet weak var lastNameLabel: UILabel! 

//MARK: - TextFields 
@IBOutlet weak var fistNameTextField: UITextField! 
@IBOutlet weak var lastNameTextField: UITextField! 

//MARK: - Outlets 

@IBOutlet weak var contactImageView: UIImageView! 

//MARK: - Buttons 
@IBAction func openPortButton(_ sender: AnyObject) { 
    self.present(self.browser, animated: true, completion: nil) 
} 

@IBAction func sendButton(_ sender: AnyObject) { 
    sendInfo() 
} 

@IBAction func getImage(_ sender: AnyObject) { 
    chooseImageContact() 
} 



//MARK: - ViewDidLoad 
override func viewDidLoad() { 
    super.viewDidLoad() 
    loadPeerToPeer() 
} 


//MARK: - Functions 
func sendInfo() { 
    if self.session.connectedPeers.count > 0 { 
     let firstNameVar = fistNameTextField.text! 
     let lastNameVar = lastNameTextField.text! 
      myDictionary = ["itemA" : "\(firstNameVar)", "itemB" : "\(lastNameVar)", "itemC" : imageDataVar] 
     do { 
      let data = NSKeyedArchiver.archivedData(withRootObject: myDictionary) 
      try self.session.send(data, toPeers: self.session.connectedPeers, with: MCSessionSendDataMode.unreliable) 
     } catch let error as NSError { 
      let ac = UIAlertController(title: "Send error", message: error.localizedDescription, preferredStyle: .alert) 
      ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 
      present(ac, animated: true, completion: nil) 
     } 
    } 
} 


// Called when a peer sends an NSData to us 
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { 

    // This needs to run on the main queue 
    DispatchQueue.main.async { 
     self.myDictionary = NSKeyedUnarchiver.unarchiveObject(with: data) as! NSDictionary 

     self.firstNameLabel.text = self.myDictionary.value(forKey: "itemA") as? String 
     self.lastNameLabel.text  = self.myDictionary.value(forKey: "itemB") as? String 
     let image     = self.myDictionary.value(forKey: "itemC") as? NSData 
     let newContactImage:UIImage = UIImage(data: image! as Data)! 
     let smallPicture   = self.scaleContactImageWith(newContactImage, newSize: CGSize(width: 100, height: 100)) 
     var sizeOfImage:CGRect  = self.contactImageView.frame 
      sizeOfImage.size  = smallPicture.size 
     self.contactImageView.frame = sizeOfImage 
     self.contactImageView.image = smallPicture 
    } 
} 


func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) { 
    dismiss(animated: true, completion: nil) 
} 
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) { 
    dismiss(animated: true, completion: nil) 
} 
func browserViewController(_ browserViewController: MCBrowserViewController, shouldPresentNearbyPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) -> Bool { 
    return true 
} 

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 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 session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    textField.resignFirstResponder() 
    return true 
} 



func loadPeerToPeer(){ 
    self.peerID = MCPeerID(displayName: UIDevice.current.name) 
    self.session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required) 
    self.session = MCSession(peer: self.peerID) 
    self.session.delegate = self 
    self.assistant = MCAdvertiserAssistant(serviceType:"VBC-ShareCard", discoveryInfo:nil, session:self.session) 
    self.assistant.start() 
    self.browser = MCBrowserViewController(serviceType: "VBC-ShareCard", session: self.session) 
    self.browser.delegate = self 
} 

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
    picker.dismiss(animated: true, completion: nil) 
} 


// Picking the image 
func chooseImageContact(){ 
    let imagePicker   = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary 
    self.present(imagePicker, animated: true, completion: nil) 
} 


//Scalling the image 
func scaleContactImageWith(_ image:UIImage, newSize:CGSize)->UIImage{ 
     UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 
     image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) 
    let newContactImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
    return newContactImage 
} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    //This gets the Contact image inside the imagePickerController 
    let pickedImage:UIImage  = info[UIImagePickerControllerOriginalImage] as! UIImage 
    let smallPicture   = scaleContactImageWith(pickedImage, newSize: CGSize(width: 100, height: 100)) 
    var sizeOfImage:CGRect  = contactImageView.frame 
     sizeOfImage.size  = smallPicture.size 
     contactImageView.frame = sizeOfImage 
     picker.dismiss(animated: true, completion: nil) 
     contactImageView.image = smallPicture as UIImage 
    let test : Data    = UIImagePNGRepresentation(smallPicture)! 
     imageDataVar   = test as NSData! 
} 

} 

回答

1

藍牙問題是藍牙連接而不是代碼的測試。 我發現在Swift 3.0(我正在運行)中,您無法關閉Wifi並進行測試,您必須進入設置> Wi-Fi,然後選擇連接到的Wi-Fi /網絡。然後選擇忘記此網絡。

即使您現在沒有Wi-Fi /網絡,WiFi本身和藍牙也必須打開。

+0

我有同樣的問題,現在這個解決方案適合我。你知道爲什麼藍牙在Wifi關閉時不工作,並且有辦法讓它在代碼中工作嗎? – Jacolack

+1

我現在有一個新的我phone8,當我跑了一個測試。 我phone8無線關閉和我的手機5無線上它的作品。 我的手機5無線關閉和我的手機8無線上無法正常工作。 蘋果可能已經修復/改變了我不知道的更新。 檢查WiFi連接 – dscrown

0

我發現這個來得到wi-fi ID。 這可能適合你。如果有一個ID做其他事情,如果沒有做任何事情。

func getWiFiSsid() -> String? { 
    var ssid: String? 
    if let interfaces = CNCopySupportedInterfaces() as NSArray? { 
     for interface in interfaces { 
      if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? { 
       ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String 
       break 
      } 
     } 
    } 
    print(ssid as Any, "This is/is not the ssid" as Any) 
    return ssid 
}