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 {
|
struct Notifications {
|
||||||
static let deviceDidChangeState = Notification.Name("deviceDidChangeState")
|
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()
|
static let instance = MPCManager()
|
||||||
@ -30,25 +40,13 @@ class MPCManager: NSObject {
|
|||||||
|
|
||||||
var deviceDidChange: (() -> Void)?
|
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{
|
deinit{
|
||||||
if let taskEnterBackground = enterbackgroundNotification {
|
if let taskEnterBackground = enterbackgroundNotification {
|
||||||
NotificationCenter.default.removeObserver(taskEnterBackground)
|
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 {
|
if let data = UserDefaults.standard.data(forKey: deviceName), let id = NSKeyedUnarchiver.unarchiveObject(with: data) as? MCPeerID {
|
||||||
self.localPeerID = id
|
self.localPeerID = id
|
||||||
} else {
|
} else {
|
||||||
@ -57,12 +55,14 @@ class MPCManager: NSObject {
|
|||||||
UserDefaults.standard.set(data, forKey: deviceName)
|
UserDefaults.standard.set(data, forKey: deviceName)
|
||||||
self.localPeerID = peerID
|
self.localPeerID = peerID
|
||||||
}
|
}
|
||||||
|
if(isAdvert){
|
||||||
self.advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: serviceType)
|
self.advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: serviceType)
|
||||||
self.advertiser.delegate = self
|
self.advertiser.delegate = self
|
||||||
|
}
|
||||||
|
else{
|
||||||
self.browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: serviceType)
|
self.browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: serviceType)
|
||||||
self.browser.delegate = self
|
self.browser.delegate = self
|
||||||
|
}
|
||||||
enterbackgroundNotification = NotificationCenter.default.addObserver(
|
enterbackgroundNotification = NotificationCenter.default.addObserver(
|
||||||
forName: UIApplication.didEnterBackgroundNotification,
|
forName: UIApplication.didEnterBackgroundNotification,
|
||||||
object: nil,
|
object: nil,
|
||||||
@ -88,9 +88,6 @@ class MPCManager: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func stopBrowsingForPeers() {
|
func stopBrowsingForPeers() {
|
||||||
for device in self.devices {
|
|
||||||
device.disconnect()
|
|
||||||
}
|
|
||||||
self.browser.stopBrowsingForPeers()
|
self.browser.stopBrowsingForPeers()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,8 +107,14 @@ class MPCManager: NSObject {
|
|||||||
device?.disconnect()
|
device?.disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func disconnectAllPeers(){
|
||||||
|
for device in self.devices {
|
||||||
|
device.disconnect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func addNewDevice(for id: MCPeerID) -> Device {
|
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)
|
let device = Device(peerID: id)
|
||||||
self.devices.append(device)
|
self.devices.append(device)
|
||||||
return device
|
return device
|
||||||
|
@ -31,8 +31,8 @@ struct ReceivedResponse {
|
|||||||
var message: MessageReponse
|
var message: MessageReponse
|
||||||
|
|
||||||
init(json: JSON) {
|
init(json: JSON) {
|
||||||
deviceID = json["deviceID"].string
|
deviceID = json["endpointId"].string
|
||||||
message = MessageReponse(json: json["message"])
|
message = MessageReponse(json: json["bytes"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,21 +4,30 @@ import MultipeerConnectivity
|
|||||||
import SwiftyJSON
|
import SwiftyJSON
|
||||||
|
|
||||||
let SERVICE_TYPE = "nearby_connections"
|
let SERVICE_TYPE = "nearby_connections"
|
||||||
let INVOKE_CHANGE_STATE_METHOD = "invoke_change_state_method"
|
let INVOKE_CHANGE_STATE_METHOD = "ios.stateChanged"
|
||||||
let INVOKE_MESSAGE_RECEIVE_METHOD = "invoke_message_receive_method"
|
let INVOKE_MESSAGE_RECEIVE_METHOD = "ios.messageReceived"
|
||||||
|
|
||||||
enum MethodCall: String {
|
enum MethodCall: String {
|
||||||
case initNearbyService = "init_nearby_service"
|
case startAdvertisingPeer = "startAdvertising"
|
||||||
case startAdvertisingPeer = "start_advertising_peer"
|
case startBrowsingForPeers = "startDiscovery"
|
||||||
case startBrowsingForPeers = "start_browsing_for_peers"
|
|
||||||
|
|
||||||
case stopAdvertisingPeer = "stop_advertising_peer"
|
case adOnConnectionInitiated = "ad.onConnectionInitiated"
|
||||||
case stopBrowsingForPeers = "stop_browsing_for_peers"
|
case adOnConnectionResult = "ad.onConnectionResult"
|
||||||
|
case adOnDisconnected = "ad.onConnectionResult"
|
||||||
|
|
||||||
case invitePeer = "invite_peer"
|
case disOnConnectionInitiated = "dis.onConnectionInitiated"
|
||||||
case disconnectPeer = "disconnect_peer"
|
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 {
|
public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
||||||
@ -39,30 +48,37 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
|||||||
|
|
||||||
func toStringAnyObject() -> [String: Any] {
|
func toStringAnyObject() -> [String: Any] {
|
||||||
return [
|
return [
|
||||||
"deviceId": deviceId,
|
"endpointId": deviceId,
|
||||||
"deviceName": deviceName,
|
"userNickName": deviceName,
|
||||||
"state": state
|
"state": state
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MessageJson {
|
|
||||||
var deviceId:String
|
|
||||||
var message:String
|
|
||||||
|
|
||||||
func toStringAnyObject() -> [String: Any] {
|
|
||||||
return [
|
|
||||||
"deviceId": deviceId,
|
|
||||||
"message": message
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func stateChanged(){
|
@objc func stateChanged(){
|
||||||
let devices = MPCManager.instance.devices.compactMap({return DeviceJson(deviceId: $0.peerID.displayName, deviceName: $0.peerID.displayName, state: $0.state.rawValue)})
|
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())
|
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) {
|
@objc func messageReceived(notification: Notification) {
|
||||||
do {
|
do {
|
||||||
if let data = notification.userInfo?["data"] as? Data, let stringData = JSON(data).rawString() {
|
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(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)
|
NotificationCenter.default.addObserver(self, selector: #selector(messageReceived), name: Device.messageReceivedNotification, object: nil)
|
||||||
|
|
||||||
MPCManager.instance.deviceDidChange = {[weak self] in
|
MPCManager.instance.deviceDidChange = {[weak self] in
|
||||||
@ -100,24 +126,34 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
|||||||
|
|
||||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||||
switch MethodCall(rawValue: call.method) {
|
switch MethodCall(rawValue: call.method) {
|
||||||
case .initNearbyService:
|
case .startAdvertisingPeer:
|
||||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||||
result(false)
|
result(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let serviceType:String = data["serviceType"] as? String ?? SERVICE_TYPE
|
let serviceType:String = data["serviceId"] as? String ?? SERVICE_TYPE
|
||||||
var deviceName:String = data["deviceName"] as? String ?? ""
|
var deviceName:String = data["userNickName"] as? String ?? ""
|
||||||
if (deviceName.isEmpty){
|
if (deviceName.isEmpty){
|
||||||
deviceName = UIDevice.current.name
|
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)
|
currentReceivedDevice = Device(peerID: MPCManager.instance.localPeerID)
|
||||||
result(true)
|
|
||||||
case .startAdvertisingPeer:
|
|
||||||
MPCManager.instance.startAdvertisingPeer()
|
MPCManager.instance.startAdvertisingPeer()
|
||||||
result(true)
|
result(true)
|
||||||
case .startBrowsingForPeers:
|
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()
|
MPCManager.instance.startBrowsingForPeers()
|
||||||
result(true)
|
result(true)
|
||||||
case .stopAdvertisingPeer:
|
case .stopAdvertisingPeer:
|
||||||
@ -130,9 +166,8 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
|||||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||||
result(false)
|
result(false)
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
guard let deviceId: String = data["deviceId"] as? String else {
|
guard let deviceId: String = data["endpointId"] as? String else {
|
||||||
result(false)
|
result(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -143,9 +178,8 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
|||||||
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
|
||||||
result(false)
|
result(false)
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
let deviceId:String? = data["deviceId"] as? String ?? nil
|
let deviceId:String? = data["endpointId"] as? String ?? nil
|
||||||
if (deviceId != nil) {
|
if (deviceId != nil) {
|
||||||
MPCManager.instance.disconnectPeer(deviceID: deviceId!)
|
MPCManager.instance.disconnectPeer(deviceID: deviceId!)
|
||||||
result(true)
|
result(true)
|
||||||
@ -158,8 +192,8 @@ public class SwiftNearbyConnectionsPlugin: NSObject, FlutterPlugin {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
|
let jsonData = try JSONSerialization.data(withJSONObject: dict["bytes"])
|
||||||
if let device = MPCManager.instance.findDevice(for: dict["deviceId"] as! String) {
|
if let device = MPCManager.instance.findDevice(for: dict["endpointId"] as! String) {
|
||||||
currentReceivedDevice = device
|
currentReceivedDevice = device
|
||||||
try device.send(data: jsonData)
|
try device.send(data: jsonData)
|
||||||
result(true)
|
result(true)
|
||||||
|
Loading…
Reference in New Issue
Block a user