2 iOS设备之间的蓝牙连接 [英] Bluetooth connection between 2 iOS devices

查看:163
本文介绍了2 iOS设备之间的蓝牙连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出来的Core蓝牙框架的iOS 5.0 介绍。据对计算器本身许多线程(许多之一):


  1. 核心蓝牙框架可以用来进行通信的任何
    硬件,它具有低功耗蓝牙(4.0)的硬件支持。

  2. 我们可以忘掉专为iPhone / iPod的(MFI)计划,如果你是
    核心采用蓝牙技术。

我有一个iPhone 5,iPhone 4S,谷歌Android的Nexus 7和我在一起,我相信至少第一2对BLE的硬件支持。

我的问题是

好吧,我试着下面给出我的iPhone 4S / iPhone 5 code,但未能扫描,找到的iPhone5 / iPhone 4S的坐在附近。我可以证实,这两个设备必须开启蓝牙功能。委托方法 didDiscoverPeripheral 从未得到调用。
可能是什么原因?我缺少的东西吗?

这是我的code(剥离下来的一个小测试项目)。

ViewController.h

  @interface的ViewController:UIViewController的< CBCentralManagerDelegate,CBPeripheralDelegate {
}
@property(强,非原子)CBCentralManager * mCentralManager;
@结束

ViewController.m

  @implementation的ViewController
@synthesize mCentralManager; - (无效)viewDidLoad中{
    [超级viewDidLoad中];
    mCentralManager = [[CBCentralManager页头] initWithDelegate:自队列:无];
    [个体经营scanForPeripherals]
} - (无效)centralManager:(CBCentralManager *)中央didDiscoverPeripheral:(CBPeripheral *)周边advertisementData:(NSDictionary的*)advertisementData RSSI(NSNumber的*){RSSI
    的NSLog(@收到periferal:%@,外围设备);
} - (INT){scanForPeripherals
    如果(self.mCentralManager.state!= CBCentralManagerStatePoweredOn)
    {
        的NSLog(@self.mCentralManagerState数:%d,self.mCentralManager.state);
        返回-1;
    }
    //获取这里好吗..蓝牙已打开。
    *的NSDictionary选项= [NSDictionary的dictionaryWithObjectsAndKeys:[NSNumber的numberWithBool:NO],CBCentralManagerScanOptionAllowDuplicatesKey,零]
    //文件说passind零为设备UUID,扫描并寻找每一个外设
    [self.mCentralManager scanForPeripheralsWithServices:无选项:选择];
    返回0;
}
@结束


解决方案

由于spamsink评论说,一台设备需要作为外设和一个中央服务器,以便他们进行沟通。

有是一个伟大的<一个href=\"https://developer.apple.com/library/ios/#sample$c$c/BTLE_Transfer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012927\">sample从苹果应用的做到这一点。此外,检查出WWDC 2012届703 - CoreBluetooth 101 和705 - 的Advanced CoreBluetooth 伟大的解释和CoreBluetooth框架使用的例子。

另外请注意,对于设备要在外设模式,它需要更新到iOS 6.0或更高。

I am trying out Core Bluetooth framework introduced in iOS 5.0. According to many threads (one of many) on StackOverflow itself:

  1. Core Bluetooth framework can be used to communicate with ANY hardware, which has Bluetooth Low Energy (4.0) hardware support.
  2. We can forget about Made For iPhone/iPod (MFI) program, if you are using Core Bluetooth technology.

I have an iPhone 5, iPhone 4S, Google Android Nexus 7 with me, and I am sure at least first 2 has hardware support for BLE.

My Question is

Well, I tried below given code on my iPhone 4S/iPhone 5, but it failed to scan and find the iPhone5/iPhone 4S sitting near by. I can confirm, both devices had bluetooth turned ON. The delegate method didDiscoverPeripheral never getting called. What might be the reasons? Am I missing something?

This is my code (stripped down to a small test project).

ViewController.h

@interface ViewController:UIViewController<CBCentralManagerDelegate, CBPeripheralDelegate{
}
@property (strong, nonatomic) CBCentralManager *mCentralManager;
@end

ViewController.m

@implementation ViewController
@synthesize mCentralManager;

- (void)viewDidLoad{
    [super viewDidLoad];
    mCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
    [self scanForPeripherals];
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    NSLog(@"Received periferal :%@",peripheral);
}

- (int) scanForPeripherals {
    if (self.mCentralManager.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"self.mCentralManagerState : %d",self.mCentralManager.state);
        return -1;
    }
    //Getting here alright.. bluetooth is powered on.
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    //Documentation says passind nil as device UUID, scans and finds every peripherals
    [self.mCentralManager scanForPeripheralsWithServices:nil options:options];
    return 0;
}
@end

解决方案

As spamsink commented, one device needs to act as peripheral, and one as central in order for them to communicate.

There is a great sample app from Apple that does that. Also, check out WWDC 2012 sessions 703 - CoreBluetooth 101 and 705 - Advanced CoreBluetooth for great explanation and examples of CoreBluetooth framework usage.

Also note, for device to be in peripheral mode, it needs to be updated to iOS 6.0 or later.

这篇关于2 iOS设备之间的蓝牙连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆