mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
Add basic placeholder callbacks
This commit is contained in:
parent
aae94e8e9b
commit
84e27adcbf
@ -15,6 +15,16 @@ class MPCManager: NSObject {
|
||||
|
||||
struct Notifications {
|
||||
static let deviceDidChangeState = Notification.Name("deviceDidChangeState")
|
||||
|
||||
static let adOnConnectionInitiated = Notification.Name("adOnConnectionInitiated")
|
||||
static let adOnConnectionResult = Notification.Name("adOnConnectionResult")
|
||||
static let adOnDisconnected = Notification.Name("adOnDisconnected")
|
||||
|
||||
static let disOnConnectionInitiated = Notification.Name("disOnConnectionInitiated")
|
||||
static let disOnConnectionResult = Notification.Name("disOnConnectionResult")
|
||||
static let disOnDisconnected = Notification.Name("disOnDisconnected")
|
||||
static let disOnEndpointFound = Notification.Name("disOnEndpointFound")
|
||||
static let disOnEndpointLost = Notification.Name("disOnEndpointLost")
|
||||
}
|
||||
|
||||
static let instance = MPCManager()
|
||||
@ -30,25 +40,13 @@ class MPCManager: NSObject {
|
||||
|
||||
var deviceDidChange: (() -> Void)?
|
||||
|
||||
// override init() {
|
||||
// if let data = UserDefaults.standard.data(forKey: "peerID"), let id = NSKeyedUnarchiver.unarchiveObject(with: data) as? MCPeerID {
|
||||
// self.localPeerID = id
|
||||
// } else {
|
||||
// let peerID = MCPeerID(displayName: UIDevice.current.name)
|
||||
// let data = NSKeyedArchiver.archivedData(withRootObject: peerID)
|
||||
// UserDefaults.standard.set(data, forKey: "peerID")
|
||||
// self.localPeerID = peerID
|
||||
// }
|
||||
// super.init()
|
||||
// }
|
||||
|
||||
deinit{
|
||||
if let taskEnterBackground = enterbackgroundNotification {
|
||||
NotificationCenter.default.removeObserver(taskEnterBackground)
|
||||
}
|
||||
}
|
||||
|
||||
func setup(serviceType: String, deviceName: String) {
|
||||
func setup(serviceType: String, deviceName: String, isAdvert: Bool) {
|
||||
if let data = UserDefaults.standard.data(forKey: deviceName), let id = NSKeyedUnarchiver.unarchiveObject(with: data) as? MCPeerID {
|
||||
self.localPeerID = id
|
||||
} else {
|
||||
@ -57,12 +55,14 @@ class MPCManager: NSObject {
|
||||
UserDefaults.standard.set(data, forKey: deviceName)
|
||||
self.localPeerID = peerID
|
||||
}
|
||||
|
||||
self.advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: serviceType)
|
||||
self.advertiser.delegate = self
|
||||
|
||||
self.browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: serviceType)
|
||||
self.browser.delegate = self
|
||||
if(isAdvert){
|
||||
self.advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: serviceType)
|
||||
self.advertiser.delegate = self
|
||||
}
|
||||
else{
|
||||
self.browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: serviceType)
|
||||
self.browser.delegate = self
|
||||
}
|
||||
enterbackgroundNotification = NotificationCenter.default.addObserver(
|
||||
forName: UIApplication.didEnterBackgroundNotification,
|
||||
object: nil,
|
||||
@ -88,9 +88,6 @@ class MPCManager: NSObject {
|
||||
}
|
||||
|
||||
func stopBrowsingForPeers() {
|
||||
for device in self.devices {
|
||||
device.disconnect()
|
||||
}
|
||||
self.browser.stopBrowsingForPeers()
|
||||
}
|
||||
|
||||
@ -110,8 +107,14 @@ class MPCManager: NSObject {
|
||||
device?.disconnect()
|
||||
}
|
||||
|
||||
func disconnectAllPeers(){
|
||||
for device in self.devices {
|
||||
device.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
func addNewDevice(for id: MCPeerID) -> Device {
|
||||
devices = devices.filter{$0.peerID.displayName != id.displayName}
|
||||
// devices = devices.filter{$0.peerID.displayName != id.displayName}
|
||||
let device = Device(peerID: id)
|
||||
self.devices.append(device)
|
||||
return device
|
||||
|
@ -31,8 +31,8 @@ struct ReceivedResponse {
|
||||
var message: MessageReponse
|
||||
|
||||
init(json: JSON) {
|
||||
deviceID = json["deviceID"].string
|
||||
message = MessageReponse(json: json["message"])
|
||||
deviceID = json["endpointId"].string
|
||||
message = MessageReponse(json: json["bytes"])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,21 +4,30 @@ import MultipeerConnectivity
|
||||
import SwiftyJSON
|
||||
|
||||
let SERVICE_TYPE = "nearby_connections"
|
||||
let INVOKE_CHANGE_STATE_METHOD = "invoke_change_state_method"
|
||||
let INVOKE_MESSAGE_RECEIVE_METHOD = "invoke_message_receive_method"
|
||||
let INVOKE_CHANGE_STATE_METHOD = "ios.stateChanged"
|
||||
let INVOKE_MESSAGE_RECEIVE_METHOD = "ios.messageReceived"
|
||||
|
||||
enum MethodCall: String {
|
||||
case initNearbyService = "init_nearby_service"
|
||||
case startAdvertisingPeer = "start_advertising_peer"
|
||||
case startBrowsingForPeers = "start_browsing_for_peers"
|
||||
case startAdvertisingPeer = "startAdvertising"
|
||||
case startBrowsingForPeers = "startDiscovery"
|
||||
|
||||
case stopAdvertisingPeer = "stop_advertising_peer"
|
||||
case stopBrowsingForPeers = "stop_browsing_for_peers"
|
||||
case adOnConnectionInitiated = "ad.onConnectionInitiated"
|
||||
case adOnConnectionResult = "ad.onConnectionResult"
|
||||
case adOnDisconnected = "ad.onConnectionResult"
|
||||
|
||||
case invitePeer = "invite_peer"
|
||||
case disconnectPeer = "disconnect_peer"
|
||||
case disOnConnectionInitiated = "dis.onConnectionInitiated"
|
||||
case disOnConnectionResult = "dis.onConnectionResult"
|
||||
case disOnDisconnected = "dis.onConnectionResult"
|
||||
case disOnEndpointFound = "dis.onEndpointFound"
|
||||
case disOnEndpointLost = "dis.onEndpointLost"
|
||||
|
||||
case sendMessage = "send_message"
|
||||
case stopAdvertisingPeer = "stopAdvertising"
|
||||
case stopBrowsingForPeers = "stopDiscovery"
|
||||
|
||||
case invitePeer = "requestConnection"
|
||||
case disconnectPeer = "disconnectFromEndpoint"
|
||||
|
||||
case sendMessage = "sendPayload"
|
||||
}
|
||||
|
||||
public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
@ -39,30 +48,37 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
|
||||
func toStringAnyObject() -> [String: Any] {
|
||||
return [
|
||||
"deviceId": deviceId,
|
||||
"deviceName": deviceName,
|
||||
"endpointId": deviceId,
|
||||
"userNickName": deviceName,
|
||||
"state": state
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
struct MessageJson {
|
||||
var deviceId:String
|
||||
var message:String
|
||||
|
||||
func toStringAnyObject() -> [String: Any] {
|
||||
return [
|
||||
"deviceId": deviceId,
|
||||
"message": message
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@objc func stateChanged(){
|
||||
let devices = MPCManager.instance.devices.compactMap({return DeviceJson(deviceId: $0.peerID.displayName, deviceName: $0.peerID.displayName, state: $0.state.rawValue)})
|
||||
channel.invokeMethod(INVOKE_CHANGE_STATE_METHOD, arguments: JSON(devices.compactMap({return $0.toStringAnyObject()})).rawString())
|
||||
}
|
||||
|
||||
@objc func adOnConnectionInitiated(){
|
||||
}
|
||||
@objc func adOnConnectionResult(){
|
||||
}
|
||||
@objc func adOnDisconnected(){
|
||||
}
|
||||
|
||||
|
||||
@objc func disOnConnectionInitiated(){
|
||||
}
|
||||
@objc func disOnConnectionResult(){
|
||||
}
|
||||
@objc func disOnDisconnected(){
|
||||
}
|
||||
@objc func disOnEndpointFound(){
|
||||
}
|
||||
@objc func disOnEndpointLost(){
|
||||
}
|
||||
|
||||
@objc func messageReceived(notification: Notification) {
|
||||
do {
|
||||
if let data = notification.userInfo?["data"] as? Data, let stringData = JSON(data).rawString() {
|
||||
@ -91,6 +107,16 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(stateChanged), name: MPCManager.Notifications.deviceDidChangeState, object: nil)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(adOnConnectionInitiated), name: MPCManager.Notifications.adOnConnectionInitiated, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(adOnConnectionResult), name: MPCManager.Notifications.adOnConnectionResult, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(adOnDisconnected), name: MPCManager.Notifications.adOnDisconnected, object: nil)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(disOnConnectionInitiated), name: MPCManager.Notifications.disOnConnectionInitiated, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(disOnConnectionResult), name: MPCManager.Notifications.disOnConnectionResult, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(disOnDisconnected), name: MPCManager.Notifications.disOnDisconnected, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(disOnEndpointFound), name: MPCManager.Notifications.disOnEndpointFound, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(disOnEndpointLost), name: MPCManager.Notifications.disOnEndpointLost, object: nil)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(messageReceived), name: Device.messageReceivedNotification, object: nil)
|
||||
|
||||
MPCManager.instance.deviceDidChange = {[weak self] in
|
||||
@ -100,24 +126,34 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
|
||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
switch MethodCall(rawValue: call.method) {
|
||||
case .initNearbyService:
|
||||
case .startAdvertisingPeer:
|
||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||
result(false)
|
||||
return
|
||||
}
|
||||
let serviceType:String = data["serviceType"] as? String ?? SERVICE_TYPE
|
||||
var deviceName:String = data["deviceName"] as? String ?? ""
|
||||
let serviceType:String = data["serviceId"] as? String ?? SERVICE_TYPE
|
||||
var deviceName:String = data["userNickName"] as? String ?? ""
|
||||
if (deviceName.isEmpty){
|
||||
deviceName = UIDevice.current.name
|
||||
}
|
||||
|
||||
MPCManager.instance.setup(serviceType: serviceType, deviceName: deviceName)
|
||||
MPCManager.instance.setup(serviceType: serviceType, deviceName: deviceName, isAdvert: true)
|
||||
currentReceivedDevice = Device(peerID: MPCManager.instance.localPeerID)
|
||||
result(true)
|
||||
case .startAdvertisingPeer:
|
||||
MPCManager.instance.startAdvertisingPeer()
|
||||
result(true)
|
||||
case .startBrowsingForPeers:
|
||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||
result(false)
|
||||
return
|
||||
}
|
||||
let serviceType:String = data["serviceId"] as? String ?? SERVICE_TYPE
|
||||
var deviceName:String = data["userNickName"] as? String ?? ""
|
||||
if (deviceName.isEmpty){
|
||||
deviceName = UIDevice.current.name
|
||||
}
|
||||
|
||||
MPCManager.instance.setup(serviceType: serviceType, deviceName: deviceName, isAdvert: false)
|
||||
currentReceivedDevice = Device(peerID: MPCManager.instance.localPeerID)
|
||||
MPCManager.instance.startBrowsingForPeers()
|
||||
result(true)
|
||||
case .stopAdvertisingPeer:
|
||||
@ -130,9 +166,8 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||
result(false)
|
||||
return
|
||||
|
||||
}
|
||||
guard let deviceId: String = data["deviceId"] as? String else {
|
||||
guard let deviceId: String = data["endpointId"] as? String else {
|
||||
result(false)
|
||||
return
|
||||
}
|
||||
@ -143,9 +178,8 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||
result(false)
|
||||
return
|
||||
|
||||
}
|
||||
let deviceId:String? = data["deviceId"] as? String ?? nil
|
||||
let deviceId:String? = data["endpointId"] as? String ?? nil
|
||||
if (deviceId != nil) {
|
||||
MPCManager.instance.disconnectPeer(deviceID: deviceId!)
|
||||
result(true)
|
||||
@ -158,8 +192,8 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||
return
|
||||
}
|
||||
do {
|
||||
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
|
||||
if let device = MPCManager.instance.findDevice(for: dict["deviceId"] as! String) {
|
||||
let jsonData = try JSONSerialization.data(withJSONObject: dict["bytes"])
|
||||
if let device = MPCManager.instance.findDevice(for: dict["endpointId"] as! String) {
|
||||
currentReceivedDevice = device
|
||||
try device.send(data: jsonData)
|
||||
result(true)
|
||||
|
Loading…
Reference in New Issue
Block a user