2个iOS设备之间的蓝牙连接

Kri*_*dra 12 iphone bluetooth ios core-bluetooth bluetooth-lowenergy

我正在尝试在iOS 5.0中引入Core Core框架.根据StackOverflow本身的许多线程(众多线程之一):

  1. 核心蓝牙框架可用于与任何 具有蓝牙低功耗(4.0)硬件支持的硬件进行通信.
  2. 如果您使用的是Core Bluetooth技术,我们可以忘记Made for iPhone/iPod(MFI)程序.

我有一个iPhone 5,iPhone 4S,谷歌Android Nexus 7,我相信至少前两个有BLE的硬件支持.

我的问题是

好吧,我尝试在我的iPhone 4S/iPhone 5下面给出了代码,但它无法扫描并发现附近的iPhone5/iPhone 4S.我可以确认,这两款设备的蓝牙都已打开.委托方法didDiscoverPeripheral永远不会被调用.可能是什么原因?我错过了什么吗?

这是我的代码(剥离到一个小的测试项目).

ViewController.h

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

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
Run Code Online (Sandbox Code Playgroud)

Fil*_*lic 12

正如spamsink所评论的那样,一个设备需要充当外围设备,一个设备需要作为中心才能进行通信.

有一个来自Apple 的优秀示例应用程序可以做到这一点.另外,请查看WWDC 2012会话703 - CoreBluetooth 101和705 - 高级CoreBluetooth,以获得CoreBluetooth框架使用的精彩解释和示例.

另请注意,要使设备处于外设模式,需要将其更新为iOS 6.0或更高版本.