2013-07-30 3 views
-1

저는 Xcode를 처음 접했고 coreBluetooth 프레임 워크를 사용하는 방법을 알아 내려고 노력하고 있습니다.iOS 앱에서 CoreBluetooth 사용하기

BLE 장치에 연결하여 데이터를 교환하려고합니다. 몇 가지 예를 들었지만 모든 것을 이해하는 데 어려움을 겪고 있습니다. 누구나 아주 기본적인 예제 나 심지어 모든 것을 구현하는 방법에 대한 단계별 튜토리얼을 알고 있습니까?

+0

xcode 문서, 몇 가지 예가 나와 있습니다. – aaronman

+0

http://stackoverflow.com/questions/13857143/bluetooth-connection-between-2-ios-devices – Krishnabhadra

+0

Bob, 블루투스 프레임 워크는 약간 이상하게 작동합니다. . 네가 다운 표를 얻은 것을 본다. 그 가능성은 당신이 당신이 한 일을 보여주지 않았거나, 당신이 시도한 것을 우리에게 주었기 때문일 것입니다. 스택 오버플로는 개발자가 프로젝트의 코드 스 니펫을 붙여 넣는 것뿐만 아니라 다른 개발자를 도우려고 의미했습니다. 어떤 사람들은 행복하게 downvote 가서 결코 이유를 설명합니다. – logixologist

답변

1

다음 단계에 따라 파일을 블루투스로 전송하십시오.

  1. GameKit 프레임 워크를 추가하십시오. .H 파일에서 .H 파일

    #import <GameKit/GameKit.h> 
    
  2. 에서

  3. 는 두 개의 객체를 생성 .H 파일에서 위임을

    <GKPeerPickerControllerDelegate,GKSessionDelegate> 
    
  4. 를 추가합니다.

    GKSession *currentSession; 
    GKPeerPickerController *picke; 
    
  5. 이 코드를 양쪽 (쌍) 장치에 연결하여 실행하십시오.

    picker = [[GKPeerPickerController alloc] init]; 
    picker.delegate = self; 
    picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby; 
    filePath = fullfilePath; 
    [picker show]; 
    
  6. 다음과 같은 방법으로 연결하면됩니다.

    -(void)peerPickerController:(GKPeerPickerController *)pk 
        didConnectPeer:(NSString *)peerID 
        toSession:(GKSession *)session 
    
  7. 쓰기 다음 코드는이 방법 양쪽에 세션을 유지합니다.

    currentSession = session; 
    session.delegate = self; 
    [session setDataReceiveHandler:self withContext:nil]; 
    picker.delegate = nil; 
    [picker dismiss]; 
    
  8. 이 코드는 파일을 전송합니다. 이 방법은 필드 data.The NSData* data 받아 봐 것이다

    if(filePath) 
    { 
        NSData *zipFileData = [NSData dataWithContentsOfFile:filePath]; 
        if(currentSession) 
        { 
         [currentSession sendDataToAllPeers:zipFileData 
           withDataMode:GKSendDataReliable error:nil]; 
        } 
    } 
    
  9. 데이터가 송신기에 의해 보내 있다. 당신은 그걸로 무엇이든 할 수 있습니다. 디스플레이 을 구문 분석하거나 원하는대로 저장할 수 있습니다.

    - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer 
          inSession:(GKSession *)session context:(void *)context 
    
  10. 세션을 유지하기 위해 두 가지 방법 도움말을 따라.

    -(void)session:(GKSession *)session peer:(NSString *)peerID 
          didChangeState:(GKPeerConnectionState)state 
    { 
        @try 
        { 
         int a = state; 
         switch (a) 
         { 
          case GKPeerStateConnected: 
           DDLogVerbose(@"connected"); 
           break; 
          case GKPeerStateDisconnected: 
           DDLogVerbose(@"disconnected"); 
           currentSession = nil; 
           break; 
         } 
        }@catch (NSException *exception) 
        { 
         DDLogError(@"Exception : %@", exception); 
        } 
    } 
    
    -(void)session:(GKSession *)session didFailWithError:(NSError *)error 
    { 
        @try 
        { 
         DDLogError(@"%@", [error description]); 
        }@catch (NSException *exception) 
        { 
         DDLogError(@"Exception : %@", exception); 
        } 
    } 
    

참고 :는 NSLog와 DDLog 문을 교체합니다. 모두 제일 좋다.

+0

형식이 잘못되었습니다. 시도했지만 제대로 형식을 지정할 수 없습니다. 누군가가 할 수 있으면 행복 할 것입니다. – CRDave

+0

귀하의 답변을 편집했습니다. 내가 빠져 나간 게 있으면 놓치지 마라. 긴 게시물이었습니다. 또한 [stackoverflow의 형식 코드] (http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)를 참조하십시오.당신은 틀린 방법으로 그것을하고 있습니다 – Krishnabhadra

+0

@Krishnabhadra 편집과이 매우 유용한 링크 덕분에 – CRDave

관련 문제