|
| 1 | + |
| 2 | +#import <Foundation/Foundation.h> |
1 | 3 | #import "ProximitySensorPlugin.h"
|
2 |
| -#if __has_include(<proximity_sensor/proximity_sensor-Swift.h>) |
3 |
| -#import <proximity_sensor/proximity_sensor-Swift.h> |
4 |
| -#else |
| 4 | + |
| 5 | +//#if __has_include(<proximity_sensor/proximity_sensor-Swift.h>) |
| 6 | +//#import <proximity_sensor/proximity_sensor-Swift.h> |
| 7 | +//#else |
5 | 8 | // Support project import fallback if the generated compatibility header
|
6 | 9 | // is not copied when this plugin is created as a library.
|
7 | 10 | // https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
|
8 |
| -#import "proximity_sensor-Swift.h" |
9 |
| -#endif |
| 11 | +//#import "proximity_sensor-Swift.h" |
| 12 | +//#endif |
10 | 13 |
|
| 14 | +//////////////////////////////////////////////////////////////////////////////// |
11 | 15 | @implementation ProximitySensorPlugin
|
12 | 16 | + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
13 |
| - [SwiftProximitySensorPlugin registerWithRegistrar:registrar]; |
| 17 | + |
| 18 | + ProximityStreamHandler* handler = [[ProximityStreamHandler alloc] init]; |
| 19 | + FlutterEventChannel* channel = [FlutterEventChannel eventChannelWithName:@"proximity_sensor" |
| 20 | + binaryMessenger:[registrar messenger]]; |
| 21 | + [channel setStreamHandler: handler]; |
| 22 | + //messenger returns a `FlutterBinaryMessenger` for creating Dart/iOS communication |
| 23 | + //channels to be used by the plugin. |
| 24 | + |
| 25 | + //---------------------------------------------------------- TODO |
| 26 | + //[SwiftProximitySensorPlugin registerWithRegistrar:registrar]; |
| 27 | +} |
| 28 | +@end |
| 29 | + |
| 30 | +//////////////////////////////////////////////////////////////////////////////// |
| 31 | +NSNotificationCenter* observer; |
| 32 | +@implementation ProximityStreamHandler |
| 33 | + |
| 34 | + //Sets up an event stream and begin emitting events. |
| 35 | + //Invoked when the first listener is registered with the Stream associated to |
| 36 | + //this channel on the Flutter side. |
| 37 | + |
| 38 | +- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events { |
| 39 | + |
| 40 | + UIDevice* device = [UIDevice currentDevice]; |
| 41 | + //먼저 센서 값을 일단 보내고.. |
| 42 | + device.proximityMonitoringEnabled = YES; |
| 43 | + int onoff = device.proximityState ? 0 : 1 ; |
| 44 | + NSMutableData * data = [NSMutableData dataWithCapacity: 1 * sizeof(int)]; |
| 45 | + [data appendBytes: &onoff length:sizeof(int)]; |
| 46 | + events([FlutterStandardTypedData typedDataWithInt32:data]); //function pointer.. |
| 47 | + |
| 48 | + //queue 에 저장해서 계속 전송하게 한다 |
| 49 | + NSOperationQueue * sensor_queue = [ NSOperationQueue mainQueue]; //Returns the operation queue associated with the main thread. |
| 50 | + observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceProximityStateDidChangeNotification |
| 51 | + object:nil |
| 52 | + queue:sensor_queue |
| 53 | + //queue:nil //TEST XXX |
| 54 | + usingBlock: ^(NSNotification* noti){ |
| 55 | + UIDevice* device = [UIDevice currentDevice]; |
| 56 | + int onoff = device.proximityState ? 0 : 1 ; |
| 57 | + NSLog(@"onoff=%d\n", onoff); |
| 58 | + |
| 59 | + |
| 60 | + NSMutableData * data = [NSMutableData dataWithCapacity: 1 * sizeof(int)]; |
| 61 | + [data appendBytes: &onoff length:sizeof(int)]; |
| 62 | + events([FlutterStandardTypedData typedDataWithInt32:data]); //function pointer.. |
| 63 | + } ] ; |
| 64 | + return nil; |
| 65 | +} |
| 66 | + |
| 67 | +//Tears down an event stream. |
| 68 | +//Invoked when the last listener is deregistered from the Stream associated to |
| 69 | +//this channel on the Flutter side. |
| 70 | +- (FlutterError *)onCancelWithArguments:(id)arguments{ |
| 71 | + UIDevice* device = [UIDevice currentDevice]; |
| 72 | + device.proximityMonitoringEnabled = NO; |
| 73 | + [[NSNotificationCenter defaultCenter] removeObserver:observer |
| 74 | + name:UIDeviceProximityStateDidChangeNotification |
| 75 | + object:nil]; |
| 76 | + return nil; |
14 | 77 | }
|
15 | 78 | @end
|
0 commit comments