无法通过 Multipeer Connectivity 连接蓝牙

Posted

技术标签:

【中文标题】无法通过 Multipeer Connectivity 连接蓝牙【英文标题】:Cannot connect Bluetooth via Multipeer Connectivity 【发布时间】:2016-09-30 17:46:37 【问题描述】:

斯威夫特 3.0

我可以使用 Multipeer Connectivity 通过 Wifi 将数据从 iPhone 共享到 iPhone,但不能通过蓝牙,而且在我看来 Wifi 不稳定。 Wifi 将连接一半的时间,蓝牙将看到对等方但无法连接。 将 iPhone 连接到 iPhone 以及将 iPhone 连接到本地任何其他类型的电话的最佳方式是什么。一个例子是没有互联网或没有 Wifi。 我最终还将向服务器发送数据,并从该服务器接收数据。

这会给我 3 个连接选择:

1.私人使用(蓝牙)?并排站立 2.半私人使用(Wifi)办公室到办公室 3.全球公共使用(互联网) 这段代码对蓝牙连接有错误吗?

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 本身和蓝牙。

【讨论】:

我遇到了同样的问题,现在这个解决方案对我有用。你知道为什么 Wifi 关闭时蓝牙不工作吗?有没有办法让它在代码中工作? 我现在有一个新的 iphone8 并且当我运行测试时。我 phone8 wifi 关闭,我 phone5 wifi on it 工作。我 phone5 wifi 关闭,我 phone8 wifi on it 将无法正常工作。 Apple 可能已在我不知道的更新中修复/更改了此问题。检查wifi连接【参考方案2】:

我发现这是为了获取 wi-fi ID。 这可能对你有用。如果有 ID 则执行其他操作,如果 nil 执行其他操作。

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

【讨论】:

以上是关于无法通过 Multipeer Connectivity 连接蓝牙的主要内容,如果未能解决你的问题,请参考以下文章

Swift2.0 Multipeer Connectivity 无法连接

通过 Multipeer Connectivity 发送 MPMediaItem

我可以通过 iOS Multipeer Connectivity 发送多大的消息?

通过 Multipeer Framework 临时同步两个客户端

iOS8 AVAudioEngine如何通过Multipeer Connectivity发送麦克风数据?

iOS 7:通过 wi-fi 或对等 wi-fi 使用 Multipeer Connectivity 连接设备