【问题标题】:Using CoreBluetooth in iOS app [closed]在 iOS 应用程序中使用 CoreBluetooth [关闭]
【发布时间】:2013-07-30 00:39:46
【问题描述】:

我对 Xcode 还是很陌生,并且正在努力弄清楚如何使用 coreBluetooth 框架。

我正在尝试连接到 BLE 设备并与之交换数据。我已经经历了几个例子,但我很难理解一切。有谁知道一个非常基本的例子,甚至是关于如何实现所有这些的分步教程?

【问题讨论】:

  • 查看 xcode 文档,里面有几个例子
  • Bob,使用蓝牙框架有点奇怪。我看到你投了反对票。可能是因为您没有展示您所做的任何事情,或者没有给我们您尝试过的东西。 Stack Overflow 的真正意义在于帮助开发人员帮助其他开发人员,而不仅仅是为项目粘贴代码 sn-ps。有些人高兴地投了反对票,却从不解释原因。

标签: ios core-bluetooth bluetooth-lowenergy


【解决方案1】:

按照此步骤使用蓝牙传输文件。

  1. 添加 GameKit 框架。
  2. 在.h文件中

    #import <GameKit/GameKit.h>
    
  3. 在.h文件中添加委托

    <GKPeerPickerControllerDelegate,GKSessionDelegate>
    
  4. 在 .h 文件中创建两个对象。

    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. 此代码将发送文件。

    if(filePath)
    {
        NSData *zipFileData = [NSData dataWithContentsOfFile:filePath];
        if(currentSession)
        {
            [currentSession sendDataToAllPeers:zipFileData 
                   withDataMode:GKSendDataReliable error:nil];
        }
    }
    
  9. 此方法将接收数据。字段NSData* data有数据 由发件人发送。你可以用它做任何事情。你可以解析显示 或随意保存。

    - (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);
         }
     }
    

注意: 将 DDLog 语句替换为 NSLog。万事如意。

【讨论】:

  • 抱歉格式错误。我尝试过但无法正确格式化。如果有人能做到,我会很高兴。
  • 我编辑了你的答案。你能过去看看我有没有遗漏什么吗?这是一个很长的帖子。也请学习format code in stackoverflow。你的做法不对
  • @Krishnabhadra 感谢编辑和这个非常有用的链接
  • GameKit 现在支持 BLE 了吗?
  • @CRDave:感谢您提供的信息。
猜你喜欢
  • 2012-07-20
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多