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

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

问题描述

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

  1. 核心蓝牙框架可用于与ANY通信硬件,具有低功耗蓝牙 (4.0) 硬件支持.
  2. 我们可以忘记 Made For iPhone/iPod (MFI) 计划,如果您是使用核心蓝牙技术.

我有一部 iPhone 5、iPhone 4S、Google Android Nexus 7,我确信至少前 2 部有对 BLE 的硬件支持.

我的问题是

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

这是我的代码(精简为一个小型测试项目).

视图控制器.h

@interface ViewController:UIViewController

ViewController.m

@implementation ViewController@synthesize mCentralManager;- (void)viewDidLoad{[超级viewDidLoad];mCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];[self scanForPeripherals];}- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral adsData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {NSLog(@"收到外设:%@",外设);}- (int) scanForPeripherals {if (self.mCentralManager.state != CBCentralManagerStatePoweredOn){NSLog(@"self.mCentralManagerState : %d",self.mCentralManager.state);返回-1;}//到这里好了..蓝牙已开启.NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];//文档说 passind nil 作为设备 UUID,扫描并找到每个外围设备[self.mCentralManager scanForPeripheralsWithServices:nil options:options];返回0;}@结尾

解决方案

正如 spamsink 所说,一台设备需要充当外围设备,另一台设备需要充当中心以便它们进行通信.

有一个很棒的示例应用 就是这样做的.此外,请查看 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天全站免登陆