Skip to content

Feature/multipeer #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added ios/.DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions ios/RCTARKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#import "RCTARKitDelegate.h"
#import "RCTARKitNodes.h"
#import "RCTMultiPeer.h"

typedef void (^RCTBubblingEventBlock)(NSDictionary *body);
typedef void (^RCTARKitResolve)(id result);
Expand All @@ -22,16 +23,19 @@ typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error

+ (instancetype)sharedInstance;
+ (bool)isInitialized;
- (instancetype)initWithARViewAndBrowser:(ARSCNView *)arView multipeer:(MultipeerConnectivity *)multipeer;
- (instancetype)initWithARView:(ARSCNView *)arView;


@property (nonatomic, strong) NSMutableArray<id<RCTARKitTouchDelegate>> *touchDelegates;
@property (nonatomic, strong) NSMutableArray<id<RCTARKitRendererDelegate>> *rendererDelegates;
@property (nonatomic, strong) NSMutableArray<id<RCTARKitSessionDelegate>> *sessionDelegates;
@property (nonatomic, strong) NSMutableArray<id<MultipeerConnectivityDelegate>> *multipeerDelegate;


#pragma mark - Properties
@property (nonatomic, strong) ARSCNView *arView;
@property (nonatomic, strong) MultipeerConnectivity *multipeer;
@property (nonatomic, strong) RCTARKitNodes *nodeManager;

@property (nonatomic, assign) BOOL debug;
Expand Down Expand Up @@ -60,6 +64,14 @@ typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error
@property (nonatomic, copy) RCTBubblingEventBlock onEvent;
@property (nonatomic, copy) RCTBubblingEventBlock onARKitError;

@property (nonatomic, copy) RCTBubblingEventBlock onPeerConnected;
@property (nonatomic, copy) RCTBubblingEventBlock onPeerConnecting;
@property (nonatomic, copy) RCTBubblingEventBlock onPeerDisconnected;

@property (nonatomic, copy) RCTBubblingEventBlock onMultipeerJsonDataReceived;




@property NSMutableDictionary *planes; // plane detected

Expand All @@ -70,6 +82,7 @@ typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error
- (void)resume;
- (void)reset;
- (void)hitTestPlane:(CGPoint)tapPoint types:(ARHitTestResultType)types resolve:(RCTARKitResolve)resolve reject:(RCTARKitReject)reject;
- (void)getCurrentWorldMap:(RCTARKitResolve)resolve reject:(RCTARKitReject)reject;
- (void)hitTestSceneObjects:(CGPoint)tapPoint resolve:(RCTARKitResolve) resolve reject:(RCTARKitReject)reject;
- (SCNVector3)projectPoint:(SCNVector3)point;
- (float)getCameraDistanceToPoint:(SCNVector3)point;
Expand Down
71 changes: 68 additions & 3 deletions ios/RCTARKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

#import "RCTARKit.h"
#import "RCTConvert+ARKit.h"
#import "RCTMultiPeer.h"

@import CoreLocation;

@interface RCTARKit () <ARSCNViewDelegate, ARSessionDelegate, UIGestureRecognizerDelegate> {
@interface RCTARKit () <ARSCNViewDelegate, ARSessionDelegate, UIGestureRecognizerDelegate, MultipeerConnectivityDelegate> {
RCTARKitResolve _resolve;
}

@property (nonatomic, strong) ARSession* session;
@property (nonatomic, strong) ARWorldTrackingConfiguration *configuration;
@property (nonatomic, strong) ARWorldMap *worldMap;

@end

Expand Down Expand Up @@ -49,15 +51,17 @@ + (instancetype)sharedInstance {
dispatch_once_on_main_thread(&onceToken, ^{
if (instance == nil) {
ARSCNView *arView = [[ARSCNView alloc] init];
MultipeerConnectivity *multipeer = [[MultipeerConnectivity alloc] init];
instance = [[self alloc] initWithARView:arView];
multipeer.delegate = instance;
instance.multipeer = multipeer;
}
});

return instance;
}

- (bool)isMounted {

return self.superview != nil;
}

Expand Down Expand Up @@ -102,7 +106,55 @@ - (instancetype)initWithARView:(ARSCNView *)arView {
return self;
}


- (void)receivedDataHandler:(NSData *)data PeerID:(MCPeerID *)peerID
{
id parsedJSON;
@try {
NSError *error = nil;
parsedJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
} @catch (NSException *exception) {
// TODO: make a onMultipeerDataFailure callback
} @finally {

}

if (parsedJSON) {
if (self.onMultipeerJsonDataReceived) {
dispatch_async(dispatch_get_main_queue(), ^{
self.onMultipeerJsonDataReceived(@{
@"data": parsedJSON,
});
});
}
} else {
id unarchived = [NSKeyedUnarchiver unarchivedObjectOfClass:[ARWorldMap classForKeyedUnarchiver] fromData:data error:nil];

if ([unarchived isKindOfClass:[ARWorldMap class]]) {
NSLog(@"[unarchived class]====%@",[unarchived class]);
ARWorldMap *worldMap = unarchived;
self.configuration = [[ARWorldTrackingConfiguration alloc] init];
self.configuration.worldAlignment = ARWorldAlignmentGravity;
self.configuration.planeDetection = ARPlaneDetectionHorizontal|ARPlaneDetectionVertical;
self.configuration.initialWorldMap = worldMap;
[self.arView.session runWithConfiguration:self.configuration options:ARSessionRunOptionResetTracking|ARSessionRunOptionRemoveExistingAnchors];

return;
}

unarchived = [NSKeyedUnarchiver unarchivedObjectOfClass:[ARAnchor classForKeyedUnarchiver] fromData:data error:nil];

if ([unarchived isKindOfClass:[ARAnchor class]]) {
NSLog(@"[unarchived class]====%@",[unarchived class]);
ARAnchor *anchor = unarchived;

[self.arView.session addAnchor:anchor];

return;
}

NSLog(@"unknown data recieved from \(%@)",peerID.displayName);
}
}


- (void)layoutSubviews {
Expand All @@ -128,6 +180,19 @@ - (void)session:(ARSession *)session didFailWithError:(NSError *)error {
}

}

- (void)getCurrentWorldMap:(RCTARKitResolve)resolve reject:(RCTARKitReject)reject {
[self.arView.session getCurrentWorldMapWithCompletionHandler:^(ARWorldMap * _Nullable worldMap, NSError * _Nullable error) {
NSLog(@"got the current world map!!!");
if (error) {
NSLog(@"error====%@",error);
}

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:worldMap requiringSecureCoding:true error:nil];
[[ARKit sharedInstance].multipeer sendToAllPeers:data];
}];
}

- (void)reset {
if (ARWorldTrackingConfiguration.isSupported) {
[self.session runWithConfiguration:self.configuration options:ARSessionRunOptionRemoveExistingAnchors | ARSessionRunOptionResetTracking];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
86 changes: 86 additions & 0 deletions ios/RCTARKitManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import "color-grabber.h"
#import "RCTMultiPeer.h"

@interface RCTARKitManager () <MCBrowserViewControllerDelegate>

@end
@implementation RCTARKitManager

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -172,13 +176,21 @@ - (NSDictionary *)constantsToExport
RCT_EXPORT_VIEW_PROPERTY(onAnchorUpdated, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAnchorRemoved, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onMultipeerJsonDataReceived, RCTBubblingEventBlock)

// TODO: Option to lock these three below down for host only
RCT_EXPORT_VIEW_PROPERTY(onPeerConnected, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPeerConnecting, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPeerDisconnected, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onTrackingState, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onFeaturesDetected, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLightEstimation, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onTapOnPlaneUsingExtent, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onTapOnPlaneNoExtent, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onEvent, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onARKitError, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(worldMap, NSObject);

RCT_EXPORT_METHOD(pause:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[[ARKit sharedInstance] pause];
Expand All @@ -199,6 +211,43 @@ - (NSDictionary *)constantsToExport
resolve(@([ARKit isInitialized]));
}

RCT_EXPORT_METHOD(openMultipeerBrowser:(NSString *)serviceType resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[[ARKit sharedInstance].multipeer openMultipeerBrowser:serviceType];
}

RCT_EXPORT_METHOD(startBrowsingForPeers:(NSString *)serviceType resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[[ARKit sharedInstance].multipeer startBrowsingForPeers:serviceType];
}

RCT_EXPORT_METHOD(advertiseReadyToJoinSession:(NSString *)serviceType resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[[ARKit sharedInstance].multipeer advertiseReadyToJoinSession:serviceType];
}

// TODO: Should be optionally to only be available to host
RCT_EXPORT_METHOD(sendDataToAllPeers:(NSDictionary *)data resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[self sendData:[RCTARKit sharedInstance].multipeer.connectedPeers data:data callback:resolve];
}

// TODO: Should be optional to lock it down so peers can only send to host
RCT_EXPORT_METHOD(sendDataToPeers:(NSDictionary *)data recepientPeerIDs:(NSArray *)recepientPeerIDs resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
NSError *error = nil;
NSMutableArray *peers = [NSMutableArray array];
for (NSString *peerUUID in recepientPeerIDs) {
[peers addObject:[[RCTARKit sharedInstance].multipeer.connectedPeers valueForKey:peerUUID]];
}
[self sendData:[RCTARKit sharedInstance].multipeer.connectedPeers data:data callback:resolve];
}

// TODO: Should be optional to only be available to host
RCT_EXPORT_METHOD(sendWorldmapData:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[[ARKit sharedInstance] getCurrentWorldMap:resolve reject:reject];
}

// TODO: Should be optional to only be available to host
RCT_EXPORT_METHOD(getAllConnectedPeers:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
//TODO: get all peer ids
}

RCT_EXPORT_METHOD(isMounted:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
if( [ARKit isInitialized]) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -228,6 +277,23 @@ - (NSDictionary *)constantsToExport
[[ARKit sharedInstance] hitTestSceneObjects:point resolve:resolve reject:reject];
}

- (void)sendData:(NSArray *)recipients data:(NSDictionary *)data callback:(RCTResponseSenderBlock)callback {
NSError *error = nil;
NSMutableArray *peers = [NSMutableArray array];
// for (NSString *peerUUID in recipients) {
// [peers addObject:[[ARKit sharedInstance].multipeer.session.connectedPeers valueForKey:peerUUID]];
// }
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
[[ARKit sharedInstance].multipeer.session sendData:jsonData toPeers:recipients withMode:MCSessionSendDataReliable error:&error];
NSLog(@"Sending data...");
if (error == nil) {
callback(@[[NSNull null]]);
}
else {
callback(@[[error description]]);
}
}




Expand Down Expand Up @@ -398,4 +464,24 @@ - (void)storeImage:(UIImage *)image options:(NSDictionary *)options reject:(RCTP
resolve(@{});
}

- (void)browserViewControllerDidFinish:(nonnull MCBrowserViewController *)browserViewController {
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

[rootViewController dismissViewControllerAnimated:YES completion:^{

}];
});
}

- (void)browserViewControllerWasCancelled:(nonnull MCBrowserViewController *)browserViewController {
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

[rootViewController dismissViewControllerAnimated:YES completion:^{

}];
});
}

@end
54 changes: 54 additions & 0 deletions ios/RCTMultiPeer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// MultipeerConnectivity.h
// ARKit
//
// Created by Mac on 2018/6/5.
// Copyright © 2018年 AR. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MultipeerConnectivity/MultipeerConnectivity.h>

NS_ASSUME_NONNULL_BEGIN

typedef void (^RCTBubblingEventBlock)(NSDictionary *body);
typedef void (^RCTARKitResolve)(id result);
typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error);

@protocol MultipeerConnectivityDelegate <NSObject>
@optional

- (void)receivedDataHandler:(NSData *)data PeerID:(MCPeerID *)peerID;

@end

@interface MultipeerConnectivity : UIView

@property(nonatomic, strong)MCPeerID *myPeerID;

@property(nonatomic, strong)MCSession *session;

@property(nonatomic, strong)MCNearbyServiceAdvertiser *serviceAdvertiser;

@property(nonatomic, strong)MCNearbyServiceBrowser *serviceBrowser;
@property(nonatomic, strong)MCBrowserViewController *mpBrowser;

@property(nonatomic, strong)NSMutableDictionary *connectedPeersDictionary;

@property(nonatomic, weak)id <MultipeerConnectivityDelegate> delegate;

- (void)sendToAllPeers:(NSData *)data;

- (void)startBrowsingForPeers:(NSString *)serviceType;
- (void)advertiseReadyToJoinSession:(NSString *)serviceType;
- (void)openMultipeerBrowser:(NSString *)serviceType;

//@property (nonatomic, copy) RCTBubblingEventBlock onPeerConnected;
//@property (nonatomic, copy) RCTBubblingEventBlock onPeerConnecting;
//@property (nonatomic, copy) RCTBubblingEventBlock onPeerDisconnected;

- (NSArray<MCPeerID *> *)connectedPeers;

@end

NS_ASSUME_NONNULL_END
Loading