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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13857143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:31:56  来源:igfitidea点击:

Bluetooth connection between 2 iOS devices

iphoneiosbluetoothcore-bluetoothbluetooth-lowenergy

提问by Krishnabhadra

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

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

  1. Core Bluetooth framework can be used to communicate with ANYhardware, 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.
  1. 核心蓝牙框架可用于与任何支持低功耗蓝牙 (4.0) 硬件的硬件进行通信。
  2. 如果您使用的是 Core Bluetooth 技术,我们可以忘记 Made For iPhone/iPod (MFI) 程序。

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.

我有一部 iPhone 5、iPhone 4S、Google Android Nexus 7,我确信至少前 2 部有对 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 didDiscoverPeripheralnever getting called. What might be the reasons? Am I missing something?

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

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

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

ViewController.h

视图控制器.h

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

ViewController.m

视图控制器.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

采纳答案by Filip Radelic

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

正如 spamsink 所评论的那样,一台设备需要充当外围设备,另一台设备需要充当中心以便它们进行通信。

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

Apple有一个很棒的示例应用程序可以做到这一点。此外,请查看 WWDC 2012 会议 703 - CoreBluetooth 101和 705 - Advanced CoreBluetooth以获得有关 CoreBluetooth 框架使用的精彩解释和示例。

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

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

回答by Krishnabhadra

Well, my understanding of Bluetooth Low Energy (BLE) general was poor. As the accepted answer pointed out, one device has to act as Central and other one has to act as peripheral for communication to take place.

好吧,我对低功耗蓝牙 (BLE) 的一般理解很差。正如公认的答案所指出的那样,一台设备必须充当中央设备,而另一台设备必须充当外围设备才能进行通信

A good example source code for iOS to iOS and iOS to Mac OS BLE communication is here.

iOS 到 iOS 和 iOS 到 Mac OS BLE 通信的一个很好的示例源代码在这里

Some important points to consider

需要考虑的一些要点

  1. on iOS 5.0 -> iPhone can only act as Central, so communication between 2 iOS devices is not possible.
  2. on iOS 6.0 -> iPhone can act as peripheral too.. So for communication to take place, atleast one device has to be running on iOS 6.0 (and probably later).
  3. First iPhone device with BLE hardware added is iPhone 4S. So even if iPhone 4 can run iOS 5 ,BLE communication is not possible on it.
  1. 在 iOS 5.0 -> iPhone 只能作为 Central,所以 2 个 iOS 设备之间的通信是不可能的。
  2. 在 iOS 6.0 上 -> iPhone 也可以充当外围设备。因此,要进行通信,至少必须有一台设备在 iOS 6.0 上运行(可能还有更高版本)。
  3. 第一款添加 BLE 硬件的 iPhone 设备是 iPhone 4S。因此,即使 iPhone 4 可以运行 iOS 5,也无法在其上进行 BLE 通信。

Well some information..

那么一些信息..

回答by user4376986

if you call scanForPeripherals function in didUpdateState delegate then it works because delegate functions can't return.

如果您在 didUpdateState 委托中调用 scanForPeripherals 函数,那么它会起作用,因为委托函数无法返回。