@@ -2,28 +2,91 @@ import Flutter
2
2
import UIKit
3
3
4
4
//https://flutterdevs.com/blog/creating-a-flutter-plugin-for-android-and-ios-image-gallery/
5
- /*
6
- public class SwiftProximitySensorPlugin: NSObject, FlutterPlugin {
7
-
8
- public static func register(with registrar: FlutterPluginRegistrar) {
9
- //UIDevice.current.isProximityMonitoringEnabled = true
5
+
6
+ public class SwiftProximityStreamHandler : NSObject , FlutterStreamHandler
7
+ {
8
+ public func onListen( withArguments arguments: Any ? , eventSink events: @escaping FlutterEventSink ) -> FlutterError ? {
9
+ let device = UIDevice . current
10
+ device. isProximityMonitoringEnabled = true
11
+ //NotificationCenter.default.addObserver(forName: <#T##NSNotification.Name?#>, object: <#T##Any?#>, queue: <#T##OperationQueue?#>, using: <#T##(Notification) -> Void#>)
12
+ NotificationCenter . default. addObserver ( forName: UIDevice . proximityStateDidChangeNotification, object: device, queue: nil )
13
+ { /* [weak self] */ ( notification) in
14
+ //guard let strongSelf = self else { return }
15
+ if let device = notification. object as? UIDevice {
16
+ print ( " from swift ... " )
17
+ // Int32 type 인 경우 dart 에서 수신하는 것은 4 byte: this is dart code --> [0, 0, 0, 0]
18
+ // --> dart 에서 처리시 list 로! 즉, return new ProximityEvent(event.cast<int>()[0]);
19
+ // Int8 type 인 경우 dart 에서 수신하는 것은 1 byte: this is dart code --> [0]
20
+ // --> 이경우에는 dart 코드에서는 리스트로 처리 안해도 됨 !
21
+ // return new ProximityEvent(event.cast<int>());
22
+ let onoff : Int8 = device. proximityState ? 0 : 1
23
+ print ( onoff)
24
+ var data = Data ( )
25
+ withUnsafeBytes ( of: onoff) { buffer in data. append ( buffer. bindMemory ( to: Int8 . self) ) }
26
+ events ( data)
27
+ }
28
+ }
29
+ ////////////////
30
+ /*
31
+ NotificationCenter.default.addObserver(self, selector: #selector(proximityStateDidChange),
32
+ name: UIDevice.proximityStateDidChangeNotification,
33
+ object: device)
34
+ */
35
+ return nil
36
+ }
10
37
11
- let channel = FlutterMethodChannel(name: "proximity_sensor", binaryMessenger: registrar.messenger())
12
- let instance = SwiftProximitySensorPlugin()
13
- registrar.addMethodCallDelegate(instance, channel: channel)
14
- }
38
+ public func onCancel( withArguments arguments: Any ? ) -> FlutterError ? {
39
+ //TODO
40
+ return nil
41
+ }
42
+ /*
43
+ @objc func proximityStateDidChange(notification: Notification)
44
+ {
45
+ if let device = notification.object as? UIDevice {
46
+ print("from swift ...")
47
+ print( device)
48
+ }
49
+ }
50
+ */
51
+ }
52
+
53
+ public class SwiftProximitySensorPlugin : NSObject , FlutterPlugin
54
+ {
55
+ //static var observer:NotificationCenter = NotificationCenter.default
56
+ static var stream_handler : SwiftProximityStreamHandler = SwiftProximityStreamHandler ( )
57
+ static var channel : FlutterEventChannel = FlutterEventChannel ( )
15
58
16
- //////////////////////////////////////////////////////////////////////////////
17
- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
18
- //result("iOS " + UIDevice.current.systemVersion)
19
- //getProximityOnOff
20
- //result(UIDevice.current.proximityState)
59
+ public static func register( with registrar: FlutterPluginRegistrar )
60
+ {
61
+ //---------------------------- FlutterEventChannel
62
+ let channel = FlutterEventChannel . init ( name: " proximity_sensor " , binaryMessenger: registrar. messenger ( ) )
63
+ channel. setStreamHandler ( stream_handler)
64
+ //----------------------------
65
+ //auto generated code ...!
66
+ /*
67
+ let channel = FlutterMethodChannel(name: "proximity_sensor", binaryMessenger: registrar.messenger())
68
+ let instance = SwiftProximitySensorPlugin()
69
+ registrar.addMethodCallDelegate(instance, channel: channel)
70
+ */
71
+ }
21
72
22
- if call.method == "getProximityOnOff" {
23
- result(UIDevice.current.proximityState)
73
+ /*
74
+ public func detachFromEngine(for registrar: FlutterPluginRegistrar) {
75
+ let device = UIDevice.current
76
+ device.isProximityMonitoringEnabled = false
77
+ NotificationCenter.default.removeObserver(observer)
78
+ }
79
+ */
80
+ //////////////////////////////////////////////////////////////////////////////
81
+ // - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
82
+ // Called if this plugin has been registered to receive `FlutterMethodCall`s.
83
+ // @param call The method call command object.
84
+ // @param result A callback for submitting the result of the call.
85
+ public func handle( _ call: FlutterMethodCall , result: @escaping FlutterResult ) {
86
+ NSLog ( call. method) //"listen"
87
+ if call. method == " listen " {
88
+ }
89
+ //result("iOS " + UIDevice.current.systemVersion)
24
90
}
25
-
26
- //FlutterStreamHandler
27
- }
28
91
}
29
- */
92
+
0 commit comments