Skip to content

Commit 057df5f

Browse files
committed
Merge branch 'master' of github.com:nicoring/HelperNetAndroid
2 parents c7d1319 + 9b916aa commit 057df5f

16 files changed

+833
-13
lines changed

index.ios.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ var {
1010
StyleSheet,
1111
Text,
1212
View,
13+
NativeModules,
14+
NativeAppEventEmitter
1315
} = React;
1416

17+
console.log(NativeModules.P2PKit)
18+
NativeModules.P2PKit.enable()
19+
NativeModules.P2PKit.startDiscovery()
20+
21+
NativeAppEventEmitter.addListener(
22+
'p2pStateChanged',
23+
(body) => console.log(body)
24+
)
25+
26+
1527
var HelperNet = React.createClass({
1628
render: function() {
1729
return (

ios/HelperNet.xcodeproj/project.pbxproj

Lines changed: 61 additions & 13 deletions
Large diffs are not rendered by default.

ios/HelperNet/AppDelegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#import <UIKit/UIKit.h>
1111

12+
1213
@interface AppDelegate : UIResponder <UIApplicationDelegate>
1314

1415
@property (nonatomic, strong) UIWindow *window;

ios/HelperNet/AppDelegate.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
@implementation AppDelegate
1515

16+
1617
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1718
{
1819
NSURL *jsCodeLocation;
@@ -45,6 +46,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4546

4647
// jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
4748

49+
4850
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
4951
moduleName:@"HelperNet"
5052
initialProperties:nil

ios/HelperNet/Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
<false/>
3939
<key>NSLocationWhenInUseUsageDescription</key>
4040
<string></string>
41+
<key>UIBackgroundModes</key>
42+
<array>
43+
<string>bluetooth-central</string>
44+
<string>bluetooth-peripheral</string>
45+
</array>
4146
<key>NSAppTransportSecurity</key>
4247
<dict>
4348
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/-->

ios/HelperNet/P2PKit.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// P2PKit.h
3+
// HelperNet
4+
//
5+
// Created by Sven Mischkewitz on 14/10/15.
6+
// Copyright © 2015 Facebook. All rights reserved.
7+
//
8+
9+
#ifndef P2PKit_h
10+
#define P2PKit_h
11+
12+
#import "RCTBridgeModule.h"
13+
#import "P2PKitService.h"
14+
15+
@interface P2PKit : NSObject <RCTBridgeModule>
16+
17+
@property P2PKitService *sharedService;
18+
19+
@end
20+
21+
22+
#endif /* P2PKit_h */

ios/HelperNet/P2PKit.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// P2PKit.m
3+
// HelperNet
4+
//
5+
// Created by Sven Mischkewitz on 14/10/15.
6+
// Copyright © 2015 Facebook. All rights reserved.
7+
//
8+
9+
10+
#import "P2PKit.h"
11+
#import "RCTEventDispatcher.h"
12+
13+
@implementation P2PKit
14+
15+
@synthesize bridge = _bridge;
16+
17+
- (id)init
18+
{
19+
self = [super init];
20+
21+
// setup services
22+
_sharedService = [P2PKitService sharedService];
23+
_sharedService.p2pKitModule = self;
24+
_sharedService.stateChangedCallback = @selector(stateChanged:);
25+
_sharedService.lostCallback = @selector(lost:);
26+
_sharedService.didUpdateCallback = @selector(didUpdate:with:);
27+
_sharedService.discoveredCallback = @selector(discovered:with:);
28+
29+
return self;
30+
}
31+
32+
33+
#pragma Event Callbacks
34+
35+
-(void)stateChanged:(NSString*)state {
36+
[self.bridge.eventDispatcher sendAppEventWithName:@"p2pStateChanged" body:@{@"state": state}];
37+
}
38+
39+
-(void)lost:(NSString*)peerID {
40+
[self.bridge.eventDispatcher sendAppEventWithName:@"p2pPeerLost" body:@{@"peerID": peerID}];
41+
}
42+
43+
-(void)discovered:(NSString*)peerID with:(NSString*)info {
44+
[self.bridge.eventDispatcher sendAppEventWithName:@"p2pDiscovered" body:@{@"peerID": peerID, @"info": info}];
45+
}
46+
47+
-(void)didUpdate:(NSString*)peerID with:(NSString*)info {
48+
[self.bridge.eventDispatcher sendAppEventWithName:@"p2pDidUpdate" body:@{@"peerID": peerID, @"info": info}];
49+
}
50+
51+
52+
#pragma Module Exports
53+
54+
RCT_EXPORT_MODULE();
55+
56+
RCT_EXPORT_METHOD(enable)
57+
{
58+
[_sharedService enableKit];
59+
}
60+
61+
RCT_EXPORT_METHOD(disable)
62+
{
63+
[_sharedService disableKit];
64+
}
65+
66+
RCT_EXPORT_METHOD(startDiscovery)
67+
{
68+
[_sharedService startDiscovery];
69+
}
70+
71+
RCT_EXPORT_METHOD(startDiscoveryWithInfo:(NSString*) stringData)
72+
{
73+
NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
74+
[_sharedService startDiscoveryWithInfo:data];
75+
}
76+
77+
RCT_EXPORT_METHOD(stopDiscovery)
78+
{
79+
[_sharedService stopDiscovery];
80+
}
81+
82+
RCT_EXPORT_METHOD(pushDiscoveryInfo:(NSString*) stringData)
83+
{
84+
NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
85+
[_sharedService pushDiscoveryInfo:data];
86+
}
87+
88+
@end

ios/HelperNet/P2PKitService.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// P2PKitService.h
3+
// HelperNet
4+
//
5+
// Created by Sven Mischkewitz on 14/10/15.
6+
// Copyright © 2015 Facebook. All rights reserved.
7+
//
8+
9+
#ifndef P2PKitService_h
10+
#define P2PKitService_h
11+
12+
#import <foundation/Foundation.h>
13+
#import <P2PKit/P2Pkit.h>
14+
15+
@interface P2PKitService : NSObject <PPKControllerDelegate>
16+
17+
// callbacks
18+
@property SEL discoveredCallback;
19+
@property SEL stateChangedCallback;
20+
@property SEL lostCallback;
21+
@property SEL didUpdateCallback;
22+
23+
// module referance for selector perform
24+
@property id p2pKitModule;
25+
26+
// instance
27+
-(void)enableKit;
28+
-(void)disableKit;
29+
-(void)startDiscovery;
30+
-(void)startDiscoveryWithInfo: (NSData*)info;
31+
-(void)stopDiscovery;
32+
-(void)pushDiscoveryInfo: (NSData*)info;
33+
34+
// singletons
35+
+(id)sharedService;
36+
37+
@end
38+
39+
#endif /* P2PKitService_h */

ios/HelperNet/P2PKitService.m

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// P2PKitService.m
3+
// HelperNet
4+
//
5+
// Created by Sven Mischkewitz on 14/10/15.
6+
// Copyright © 2015 Facebook. All rights reserved.
7+
//
8+
9+
#import "P2PKitService.h"
10+
11+
@implementation P2PKitService
12+
13+
static NSString *const API_KEY = @"eyJzaWduYXR1cmUiOiJieXBGVEtaR1NVNVAzWW56ZmlDMDZ5TnRnRDZTVGRLbWNGQTFpYlpWVllCWGVpUllIazEvbEVpcG8xZVJzMTlyaW5Mb1VFNU53c3ozSk5xNkpYT0hwUVQ0YSthc1RWbHNJM3ZPS3dsOGhSZ2dzNE9zb09FUGY1UmdHZU9raEkvZHoxUzdvWGN3bUxScW45dVAydkF5NWI4anZYZ2xHZ2paajZ6YVBuVTFmb2M9IiwiYXBwSWQiOjEyODgsInZhbGlkVW50aWwiOjE2ODAwLCJhcHBVVVVJRCI6IkQ3MkIxNUM0LThGRjMtNEVDRi04RjY4LUIwQzhBNjEwRkRFMSJ9";
14+
15+
static BOOL isEnabled = false;
16+
17+
18+
#pragma Delegate Methods
19+
20+
-(void)PPKControllerInitialized {}
21+
22+
-(void)p2pPeerDiscovered: (PPKPeer*)peer {
23+
NSString *discoveryInfoString = [[NSString alloc] initWithData:peer.discoveryInfo encoding:NSUTF8StringEncoding];
24+
25+
if (_discoveredCallback != nil) {
26+
[_p2pKitModule performSelector:_discoveredCallback withObject:peer.peerID withObject: discoveryInfoString];
27+
}
28+
}
29+
30+
-(void)p2pPeerLost: (PPKPeer*)peer {
31+
if (_lostCallback != nil) {
32+
[_p2pKitModule performSelector:_lostCallback withObject:peer.peerID];
33+
}
34+
}
35+
36+
-(void)didUpdateP2PDiscoveryInfoForPeer: (PPKPeer*)peer {
37+
NSString *discoveryInfo = [[NSString alloc] initWithData:peer.discoveryInfo encoding:NSUTF8StringEncoding];
38+
if (_didUpdateCallback != nil) {
39+
[_p2pKitModule performSelector:_didUpdateCallback withObject:peer.peerID withObject:discoveryInfo];
40+
}
41+
}
42+
43+
-(void)p2pDiscoveryStateChanged: (PPKPeer2PeerDiscoveryState)state {
44+
// get state string
45+
NSString *discoveryState;
46+
if (state == PPKPeer2PeerDiscoveryStopped) {
47+
discoveryState = @"stopped";
48+
} else if (state == PPKPeer2PeerDiscoverySuspended) {
49+
discoveryState = @"suspended";
50+
} else if (state == PPKPeer2PeerDiscoveryRunning) {
51+
discoveryState = @"running";
52+
}
53+
54+
if (_stateChangedCallback != nil) {
55+
[_p2pKitModule performSelector:_stateChangedCallback withObject:discoveryState];
56+
}
57+
}
58+
59+
60+
#pragma Instance Methods
61+
62+
-(void)enableKit {
63+
if (isEnabled) {
64+
return;
65+
}
66+
67+
[PPKController enableWithConfiguration:API_KEY observer:self];
68+
isEnabled = true;
69+
}
70+
71+
-(void)disableKit {
72+
if (isEnabled) {
73+
return;
74+
}
75+
76+
[PPKController disable];
77+
isEnabled = false;
78+
}
79+
80+
-(void)startDiscovery {
81+
[PPKController startP2PDiscovery];
82+
}
83+
84+
-(void)startDiscoveryWithInfo: (NSData *)info {
85+
[PPKController startP2PDiscoveryWithDiscoveryInfo: info];
86+
}
87+
88+
-(void)stopDiscovery {
89+
[PPKController stopP2PDiscovery];
90+
}
91+
92+
-(void)pushDiscoveryInfo: (NSData*)info {
93+
[PPKController pushNewP2PDiscoveryInfo: info];
94+
}
95+
96+
#pragma Singleton Methods
97+
98+
+(id)sharedService {
99+
static P2PKitService *sharedService = nil;
100+
static dispatch_once_t onceToken;
101+
102+
dispatch_once(&onceToken, ^{
103+
sharedService = [[self alloc] init];
104+
});
105+
106+
return sharedService;
107+
}
108+
109+
-(id)init {
110+
self = [super init];
111+
return self;
112+
}
113+
114+
- (void)dealloc {
115+
// Should never be called, but just here for clarity really.
116+
}
117+
118+
@end
Binary file not shown.

ios/P2PKit.framework/Headers/P2PKit.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* P2PKit.h
3+
* P2PKit
4+
*
5+
* Copyright (c) 2015 by Uepaa AG, Zürich, Switzerland.
6+
* All rights reserved.
7+
*
8+
* We reserve all rights in this document and in the information contained therein.
9+
* Reproduction, use, transmission, dissemination or disclosure of this document and/or
10+
* the information contained herein to third parties in part or in whole by any means
11+
* is strictly prohibited, unless prior written permission is obtained from Uepaa AG.
12+
*
13+
*/
14+
15+
16+
#import <Foundation/Foundation.h>
17+
#import <P2PKit/PPKPeer.h>
18+
#import <P2PKit/PPKController.h>

0 commit comments

Comments
 (0)