Commit 581970cc authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- first workable version. The behaviour is similar to iOS MC framework.

parent 3d01367c
......@@ -73,6 +73,15 @@
9B1F83DC1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F83DB1D19CFCE00837DA1 /* NCMCMessageData.m */; };
9B1F83DD1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F83DB1D19CFCE00837DA1 /* NCMCMessageData.m */; };
9B1F83DE1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F83DB1D19CFCE00837DA1 /* NCMCMessageData.m */; };
9B1F83EC1D19E1E400837DA1 /* NCMCPeripheralInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F83EB1D19E1E400837DA1 /* NCMCPeripheralInfo.m */; };
9B1F83ED1D19E1E400837DA1 /* NCMCPeripheralInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F83EB1D19E1E400837DA1 /* NCMCPeripheralInfo.m */; };
9B1F83EE1D19E1E400837DA1 /* NCMCPeripheralInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B1F83EB1D19E1E400837DA1 /* NCMCPeripheralInfo.m */; };
9B24307E1D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B24307D1D1C849D00F5DFC1 /* NCMCDeviceInfo.m */; };
9B24307F1D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B24307D1D1C849D00F5DFC1 /* NCMCDeviceInfo.m */; };
9B2430801D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B24307D1D1C849D00F5DFC1 /* NCMCDeviceInfo.m */; };
9B28F0261D219148008B35B3 /* NCMCAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B28F0251D219148008B35B3 /* NCMCAlertView.m */; };
9B28F0271D219148008B35B3 /* NCMCAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B28F0251D219148008B35B3 /* NCMCAlertView.m */; };
9B28F0281D219148008B35B3 /* NCMCAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B28F0251D219148008B35B3 /* NCMCAlertView.m */; };
9B6B1ED11D18944F0008B45D /* LoginScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B6B1ED01D18944F0008B45D /* LoginScene.m */; };
9B6B1ED21D18944F0008B45D /* LoginScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B6B1ED01D18944F0008B45D /* LoginScene.m */; };
9B6B1ED31D18944F0008B45D /* LoginScene.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B6B1ED01D18944F0008B45D /* LoginScene.m */; };
......@@ -210,6 +219,13 @@
9B1F83DA1D19CFCE00837DA1 /* NCMCMessageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NCMCMessageData.h; path = Source/NCMultipeerConnectiviy/Core/NCMCMessageData.h; sourceTree = "<group>"; };
9B1F83DB1D19CFCE00837DA1 /* NCMCMessageData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NCMCMessageData.m; path = Source/NCMultipeerConnectiviy/Core/NCMCMessageData.m; sourceTree = "<group>"; };
9B1F83DF1D19D5E100837DA1 /* NCMCPeripheralService+Core.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NCMCPeripheralService+Core.h"; path = "Source/NCMultipeerConnectiviy/Core/NCMCPeripheralService+Core.h"; sourceTree = "<group>"; };
9B1F83EA1D19E1E400837DA1 /* NCMCPeripheralInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NCMCPeripheralInfo.h; path = Source/NCMultipeerConnectiviy/Core/NCMCPeripheralInfo.h; sourceTree = "<group>"; };
9B1F83EB1D19E1E400837DA1 /* NCMCPeripheralInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NCMCPeripheralInfo.m; path = Source/NCMultipeerConnectiviy/Core/NCMCPeripheralInfo.m; sourceTree = "<group>"; };
9B1F83EF1D19F68200837DA1 /* NCMCPeerID+Core.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NCMCPeerID+Core.h"; path = "Source/NCMultipeerConnectiviy/Core/NCMCPeerID+Core.h"; sourceTree = "<group>"; };
9B24307C1D1C849D00F5DFC1 /* NCMCDeviceInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NCMCDeviceInfo.h; path = Source/NCMultipeerConnectiviy/Core/NCMCDeviceInfo.h; sourceTree = "<group>"; };
9B24307D1D1C849D00F5DFC1 /* NCMCDeviceInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NCMCDeviceInfo.m; path = Source/NCMultipeerConnectiviy/Core/NCMCDeviceInfo.m; sourceTree = "<group>"; };
9B28F0241D219148008B35B3 /* NCMCAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NCMCAlertView.h; path = Source/NCMCAlertView.h; sourceTree = "<group>"; };
9B28F0251D219148008B35B3 /* NCMCAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NCMCAlertView.m; path = Source/NCMCAlertView.m; sourceTree = "<group>"; };
9B6B1ECF1D18944F0008B45D /* LoginScene.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LoginScene.h; path = Source/LoginScene.h; sourceTree = "<group>"; };
9B6B1ED01D18944F0008B45D /* LoginScene.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = LoginScene.m; path = Source/LoginScene.m; sourceTree = "<group>"; };
9B6B1ED41D1895860008B45D /* SettingScene.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SettingScene.h; path = Source/SettingScene.h; sourceTree = "<group>"; };
......@@ -485,10 +501,15 @@
9B1F83D01D19B83800837DA1 /* NCMCSession+Core.h */,
9B1F83D51D19BAAD00837DA1 /* NCMCCentralService+Core.h */,
9B1F83DF1D19D5E100837DA1 /* NCMCPeripheralService+Core.h */,
9B1F83EF1D19F68200837DA1 /* NCMCPeerID+Core.h */,
9B1F83DA1D19CFCE00837DA1 /* NCMCMessageData.h */,
9B1F83DB1D19CFCE00837DA1 /* NCMCMessageData.m */,
9B1F83EA1D19E1E400837DA1 /* NCMCPeripheralInfo.h */,
9B1F83EB1D19E1E400837DA1 /* NCMCPeripheralInfo.m */,
9B1F83CB1D19B5AF00837DA1 /* NCMCBluetoothLEManager.h */,
9B1F83CC1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m */,
9B24307C1D1C849D00F5DFC1 /* NCMCDeviceInfo.h */,
9B24307D1D1C849D00F5DFC1 /* NCMCDeviceInfo.m */,
);
name = Core;
sourceTree = "<group>";
......@@ -496,12 +517,12 @@
9B6B1EE71D1898DA0008B45D /* Scenes */ = {
isa = PBXGroup;
children = (
9B6B1ECF1D18944F0008B45D /* LoginScene.h */,
9B6B1ED01D18944F0008B45D /* LoginScene.m */,
9B6B1ED41D1895860008B45D /* SettingScene.h */,
9B6B1ED51D1895860008B45D /* SettingScene.m */,
B7378992180767190076A88C /* LobbyScene.h */,
B7378993180767190076A88C /* LobbyScene.m */,
9B6B1ECF1D18944F0008B45D /* LoginScene.h */,
9B6B1ED01D18944F0008B45D /* LoginScene.m */,
9B6B1ED91D1895A50008B45D /* ConnectionScene.h */,
9B6B1EDA1D1895A50008B45D /* ConnectionScene.m */,
);
......@@ -514,6 +535,8 @@
9B6B1EE91D1899210008B45D /* NCMultipeerConnectiviy */,
9B6B1EEA1D1899850008B45D /* MultiplayerController.h */,
9B6B1EEB1D1899850008B45D /* MultiplayerController.m */,
9B28F0241D219148008B35B3 /* NCMCAlertView.h */,
9B28F0251D219148008B35B3 /* NCMCAlertView.m */,
);
name = Controller;
sourceTree = "<group>";
......@@ -901,12 +924,15 @@
files = (
7A4036E819DE3F33007B6E8F /* LobbyScene.m in Sources */,
7A40360419DDEE84007B6E8F /* main.m in Sources */,
9B1F83EE1D19E1E400837DA1 /* NCMCPeripheralInfo.m in Sources */,
9B6B1EDD1D1895A50008B45D /* ConnectionScene.m in Sources */,
9B6B1ED31D18944F0008B45D /* LoginScene.m in Sources */,
7A59498E19E3941300F65F90 /* AppDelegate.m in Sources */,
9B2430801D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */,
9B6B1ED81D1895860008B45D /* SettingScene.m in Sources */,
9B1F83CF1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m in Sources */,
9B1F83DE1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */,
9B28F0281D219148008B35B3 /* NCMCAlertView.m in Sources */,
9B6B1EEE1D1899850008B45D /* MultiplayerController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......@@ -917,9 +943,12 @@
files = (
9B6B1EDC1D1895A50008B45D /* ConnectionScene.m in Sources */,
9B6B1ED21D18944F0008B45D /* LoginScene.m in Sources */,
9B24307F1D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */,
9B1F83ED1D19E1E400837DA1 /* NCMCPeripheralInfo.m in Sources */,
9B6B1EED1D1899850008B45D /* MultiplayerController.m in Sources */,
9B6B1ED71D1895860008B45D /* SettingScene.m in Sources */,
9B1F83DD1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */,
9B28F0271D219148008B35B3 /* NCMCAlertView.m in Sources */,
9B1F83CE1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m in Sources */,
927F619E196C771E000F43EF /* LobbyScene.m in Sources */,
5B121030197484FA004C1E1D /* NCMultipeerConnectivityActivity.m in Sources */,
......@@ -937,11 +966,14 @@
9B6B1ED11D18944F0008B45D /* LoginScene.m in Sources */,
9B1F83C91D19AEEF00837DA1 /* NCMCSession.m in Sources */,
9B1F83CD1D19B5AF00837DA1 /* NCMCBluetoothLEManager.m in Sources */,
9B24307E1D1C849D00F5DFC1 /* NCMCDeviceInfo.m in Sources */,
9B1F83C81D19AEEF00837DA1 /* NCMCPeripheralService.m in Sources */,
5B121038197487F2004C1E1D /* AppDelegate.m in Sources */,
9B1F83C61D19AEEF00837DA1 /* NCMCCentralService.m in Sources */,
9B6B1ED61D1895860008B45D /* SettingScene.m in Sources */,
9B6B1EEC1D1899850008B45D /* MultiplayerController.m in Sources */,
9B1F83EC1D19E1E400837DA1 /* NCMCPeripheralInfo.m in Sources */,
9B28F0261D219148008B35B3 /* NCMCAlertView.m in Sources */,
9B1F83DC1D19CFCE00837DA1 /* NCMCMessageData.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......
......@@ -440,7 +440,7 @@
<string>Block</string>
<key>value</key>
<array>
<string>onUserNameEntered</string>
<string>onUserNameEntered:</string>
<integer>1</integer>
</array>
</dict>
......@@ -496,6 +496,8 @@
<integer>17</integer>
</dict>
</array>
<key>selected</key>
<true/>
</dict>
<dict>
<key>UUID</key>
......@@ -1029,8 +1031,6 @@
</array>
</dict>
</array>
<key>selected</key>
<true/>
</dict>
</array>
<key>customClass</key>
......
#import "LobbyScene.h"
#import "MultiplayerController.h"
#import "NCMultipeerConnectiviy/NCMCPeerID.h"
#import "Parameters.h"
@implementation LobbyScene
{
CCLabelTTF* lbMsg;
CCLabelTTF* lbPlayer1;
CCLabelTTF* lbPlayer2;
CCLabelTTF* lbPlayer3;
CCLabelTTF* lbPlayer4;
CCButton* btnStart;
CCButton* btnBack;
}
-(void)didLoadFromCCB
{
[self resetUI];
}
-(void) onEnter {
[super onEnter];
if ([[MultiplayerController instance] isHost]) {
[[MultiplayerController instance] startHost];
} else {
[[MultiplayerController instance]startClient];
}
[self schedule: @selector(tickMe:) interval:1.0/1.0];
}
-(void) onExit {
[super onExit];
[self unschedule:@selector(tickMe:)];
}
-(void)onBack
{
CCLOG(@"onBtnBackClicked");
[self disconnectAndBack];
}
-(void)onStart
{
CCLOG(@"onBtnStartClicked");
}
-(void)disconnectAndBack
{
[[MultiplayerController instance] disconnect];
CCScene *serverClientScene = [CCBReader loadAsScene:@"SettingScene"];
[[CCDirector sharedDirector] replaceScene:serverClientScene];
}
-(void)resetUI
{
if ([[MultiplayerController instance] isHost]) {
lbMsg.string = @"Waiting for player...";
lbPlayer1.color = [CCColor whiteColor];
lbPlayer1.string = [[MultiplayerController instance] localName];
lbPlayer2.color = [CCColor darkGrayColor];
lbPlayer2.string = @"--empty--";
lbPlayer3.color = [CCColor darkGrayColor];
lbPlayer3.string = @"--empty--";
lbPlayer4.color = [CCColor darkGrayColor];
lbPlayer4.string = @"--empty--";
btnStart.enabled = NO;
btnBack.enabled = YES;
} else {
lbMsg.string = @"Searching for game...";
lbPlayer1.color = [CCColor darkGrayColor];
lbPlayer1.string = @"--empty--";
lbPlayer2.color = [CCColor darkGrayColor];
lbPlayer2.string = @"--empty--";
lbPlayer3.color = [CCColor darkGrayColor];
lbPlayer3.string = @"--empty--";
lbPlayer4.color = [CCColor darkGrayColor];
lbPlayer4.string = @"--empty--";
btnStart.enabled = NO;
btnStart.visible = NO;
btnBack.position = ccp(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.195);
}
}
-(void) tickMe: (CCTime) dt
{
[self resetUI];
NSArray* playerData = [[MultiplayerController instance] currentSessionPlayerIDs];
int i = 0;
for(NCMCPeerID* pd in playerData) {
if( i < 4) {
if (i == 0) {
lbPlayer1.string = [pd getDisplayName];
lbPlayer1.color = [CCColor whiteColor];
}
if (i == 1) {
lbPlayer2.string = [pd getDisplayName];
lbPlayer2.color = [CCColor whiteColor];
}
if (i == 2) {
lbPlayer3.string = [pd getDisplayName];
lbPlayer3.color = [CCColor whiteColor];
}
if (i == 3) {
lbPlayer4.string = [pd getDisplayName];
lbPlayer4.color = [CCColor whiteColor];
}
}
i++;
}
if(i >= 2) {
lbMsg.string = @"Ready to game";
if ([[MultiplayerController instance] isHost]) {
btnStart.enabled = YES;
}
}
else if(i < 2) {
if ([[MultiplayerController instance] isHost]) {
btnStart.enabled = NO;
}
}
}
@end
......@@ -27,8 +27,8 @@
-(void)onBtnStartClicked
{
CCLOG(@"name = %@", tfUserName.string);
[[MultiplayerController instance]setMultiplayerDisplayName:tfUserName.string];
[[MultiplayerController instance] setLocalName:tfUserName.string];
CCScene *serverClientScene = [CCBReader loadAsScene:@"SettingScene"];
[[CCDirector sharedDirector] replaceScene:serverClientScene];
......
......@@ -7,16 +7,28 @@
//
#import <Foundation/Foundation.h>
#import "NCMCSession.h"
#import "NCMCCentralService.h"
#import "NCMCPeripheralService.h"
@interface MultiplayerController : NSObject
@interface MultiplayerController : NSObject<NCMCSessionDelegate, NCMCCentralServiceDelegate, NCMCPeripheralServiceDelegate, UIAlertViewDelegate>
+ (MultiplayerController *) instance;
@property(strong, nonatomic) NCMCSession* currentSession;
@property(strong, nonatomic) NCMCCentralService* currentCentralService;
@property(strong, nonatomic) NCMCPeripheralService* currentPeripheralService;
@property(strong, nonatomic) NSMutableArray* currentSessionPlayerIDs;
@property(assign, nonatomic) Boolean isHost;
@property(strong, nonatomic) NSString *localName;
-(void)setMultiplayerDisplayName:(NSString*)name;
+ (MultiplayerController *) instance;
// SETUP FUNCS
- (void) initializeControllerForNewMatch;
- (void) createServerHostedGame;
- (void) joinServerHostedGame;
- (void) disconnect;
-(void) startHost;
-(void) startClient;
@end
......@@ -7,11 +7,14 @@
//
#import "MultiplayerController.h"
#import "NCMCPeerID.h"
#import "NCMCAlertView.h"
#import "Parameters.h"
@implementation MultiplayerController
{
NSString *displayName;
}
@synthesize currentSession, currentCentralService, currentPeripheralService, isHost, localName;
static MultiplayerController *_sharedMultiplayerController = nil;
......@@ -27,25 +30,183 @@ static MultiplayerController *_sharedMultiplayerController = nil;
return _sharedMultiplayerController;
}
-(void)setMultiplayerDisplayName:(NSString *)name
{
displayName = name;
}
-(void)initializeControllerForNewMatch
{
NCMCPeerID *peer = [[NCMCPeerID alloc]initWithDisplayName:self.localName];
self.currentSession = [[NCMCSession alloc]initWithPeer:peer andServiceID:TRANSFER_SERVICE_UUID];
self.currentSession.delegate = self;
if (self.currentPeripheralService != nil) {
[self.currentPeripheralService stopAdvertisingPeer];
self.currentPeripheralService.delegate = nil;
self.currentPeripheralService = nil;
}
if (self.currentCentralService != nil) {
[self.currentCentralService stopBrowsingForPeers];
self.currentCentralService.delegate = nil;
self.currentCentralService = nil;
}
if (self.currentSessionPlayerIDs != nil) {
[self.currentSessionPlayerIDs removeAllObjects];
} else {
self.currentSessionPlayerIDs = [[NSMutableArray alloc] init];
}
[self.currentSessionPlayerIDs addObject:self.currentSession.myPeerID];
}
-(void)joinServerHostedGame
{
[self initializeControllerForNewMatch];
self.isHost = NO;
self.currentPeripheralService = [[NCMCPeripheralService alloc]initWithSession:self.currentSession];
self.currentPeripheralService.delegate = self;
//[self.currentPeripheralService startAdvertisingPeer];
}
-(void)createServerHostedGame
{
[self initializeControllerForNewMatch];
self.isHost = YES;
self.currentCentralService = [[NCMCCentralService alloc]initWithSession:self.currentSession];
self.currentCentralService.delegate = self;
//[self.currentCentralService startBrowsingForPeers];
}
- (void)disconnect
{
[self.currentSession disconnect];
if (self.currentSessionPlayerIDs != nil) {
[self.currentSessionPlayerIDs removeAllObjects];
}
}
-(void)startHost
{
if (self.isHost) {
[self.currentCentralService startBrowsingForPeers];
}
}
-(void)startClient
{
if (!self.isHost) {
[self.currentPeripheralService startAdvertisingPeer];
}
}
// session delegate
-(void)session:(NCMCSession *)session peer:(NCMCPeerID *)peerID didChangeState:(NCMCSessionState)state
{
CCLOG(@"MCSession session peer didChangeState : %@, state : %d", [peerID getDisplayName], state);
dispatch_async(dispatch_get_main_queue(), ^{
switch (state) {
case NCMCSessionStateConnected: {
[self.currentSessionPlayerIDs addObject:peerID];
if (!self.isHost) {
if (self.currentPeripheralService) {
[self.currentPeripheralService stopAdvertisingPeer];
self.currentPeripheralService.delegate = nil;
self.currentPeripheralService = nil;
}
if (self.currentCentralService) {
[self.currentCentralService stopBrowsingForPeers];
self.currentCentralService.delegate = nil;
self.currentCentralService = nil;
}
}
break;
}
case NCMCSessionStateNotConnected: {
for (NCMCPeerID *pid in self.currentSessionPlayerIDs) {
if ([[pid getDisplayName] isEqualToString:[peerID getDisplayName]]) {
[self.currentSessionPlayerIDs removeObject:pid];
break;
}
}
break;
}
};
});
}
-(void)session:(NCMCSession *)session didReceiveData:(NSData *)data fromPeer:(NCMCPeerID *)peerID
{
}
-(void)centralService:(NCMCCentralService *)centralService foundPeer:(NCMCPeerID *)peerID
{
dispatch_async(dispatch_get_main_queue(), ^{
if (self.isHost) {
NSString* msg = [NSString stringWithFormat:@"\"%@\" would like to join your game, do you accept?", [peerID getDisplayName]];
NCMCAlertView *alert = [[NCMCAlertView alloc] initWithTitle:@"Player Request" message:msg delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
alert.target = peerID;
alert.tag = 1;
[alert show];
}
});
}
-(void)centralService:(NCMCCentralService *)centralService lostPeer:(NCMCPeerID *)peerID
{
}
-(void)centralService:(NCMCCentralService *)centralService didNotStartBrowsingForPeers:(NSError *)error
{
}
-(void)peripheralService:(NCMCPeripheralService *)peripheralService didReceiveInvitationFromPeer:(NCMCPeerID *)peerID invitationHandler:(void (^)(BOOL, NCMCSession * _Nonnull, NCMCPeerID * _Nonnull))invitationHandler
{
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.isHost) {
NSString* msg = [NSString stringWithFormat:@"The game \"%@\" was found,. Would you like to connect?", [peerID getDisplayName]];
NCMCAlertView *alert = [[NCMCAlertView alloc] initWithTitle:@"Game Found" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Connect", nil];
alert.target = peerID;
alert.handler = invitationHandler;
alert.tag = 2;
[alert show];
}
});
}
-(void)peripheralService:(NCMCPeripheralService *)peripheralService didNotStartAdvertising:(NSError *)error
{
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Accept"]) {
NCMCAlertView* alertV = (NCMCAlertView*)alertView;
[self.currentCentralService invitePeer:alertV.target];
}
} else if (alertView.tag == 2) {
NCMCAlertView* alertV = (NCMCAlertView*)alertView;
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Cancel"]) {
alertV.handler(NO, self.currentSession, alertV.target);
} else {
alertV.handler(YES, self.currentSession, alertV.target);
}
}
}
@end
//
// NCMCAlertView.h
// NCMultipeerConnectivity
//
// Created by Chengzhao Li on 2016-06-27.
// Copyright © 2016 Apportable. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "NCMCPeerID.h"
#import "NCMCSession.h"
typedef void (^MyInvitationHandler)(BOOL accept, NCMCSession *session, NCMCPeerID *peerID);
@interface NCMCAlertView : UIAlertView
@property(nonatomic, copy) NCMCPeerID* target;
@property(nonatomic, copy) MyInvitationHandler handler;
@end
//
// NCMCAlertView.m
// NCMultipeerConnectivity
//
// Created by Chengzhao Li on 2016-06-27.
// Copyright © 2016 Apportable. All rights reserved.
//
#import "NCMCAlertView.h"
@implementation NCMCAlertView
@synthesize target, handler;
@end
......@@ -13,13 +13,33 @@
#import "NCMCPeripheralService+Core.h"
@interface NCMCBluetoothLEManager : NSObject
@interface NCMCBluetoothLEManager : NSObject<CBCentralManagerDelegate, CBPeripheralDelegate, CBPeripheralManagerDelegate>
@property (assign, nonatomic)Boolean isCentral;
@property (strong, nonatomic)NCMCSession* session;
@property (strong, nonatomic)NCMCCentralService* centralService;
@property (strong, nonatomic)NCMCPeripheralService* peripheralService;
@property (assign, nonatomic) Boolean isDeviceReady;
-(void)clear;
+(NCMCBluetoothLEManager *)instance;
-(void)disconnect;
// central
-(void)setupCentralEnv:(NCMCCentralService*)service;
-(Boolean)startBrowsing;
-(void)stopBrowsing;
-(void)invitePeer:(NCMCPeerID*)peerID;
-(void)sendCentralData : (NSData*)data toPerihperal:(NSString*)identifier;