I am trying out Core Bluetooth framework introduced in iOS 5.0. According to many threads (one of many) on * itself:
我正在尝试在iOS 5.0中引入Core Core框架。根据*本身的许多线程(众多线程之一):
- Core Bluetooth framework can be used to communicate with ANY hardware, which has Bluetooth Low Energy (4.0) hardware support.
- 核心蓝牙框架可用于与任何具有蓝牙低功耗(4.0)硬件支持的硬件进行通信。
- We can forget about Made For iPhone/iPod (MFI) program, if you are using Core Bluetooth technology.
- 如果您使用的是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,谷歌Android Nexus 7,我相信至少前两个有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?
好吧,我尝试下面的iPhone 4S / iPhone 5上的代码,但它无法扫描,发现附近的iPhone5 / iPhone 4S。我可以确认,这两款设备的蓝牙都已打开。委托方法didDiscoverPeripheral永远不会被调用。可能是什么原因?我错过了什么吗?
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
3 个解决方案
#1
12
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 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.
Apple提供了一个很棒的示例应用程序。另外,请查看WWDC 2012会话703 - CoreBluetooth 101和705 - 高级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或更高版本。
#2
3
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
需要考虑的一些要点
- on iOS 5.0 -> iPhone can only act as Central, so communication between 2 iOS devices is not possible.
- 在iOS 5.0上 - > iPhone只能作为Central,因此无法在2台iOS设备之间进行通信。
- 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).
- 在iOS 6.0上 - > iPhone也可以作为外设。所以要进行通信,至少有一个设备必须在iOS 6.0上运行(可能以后)。
- 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.
- 添加了BLE硬件的首款iPhone设备是iPhone 4S。因此,即使iPhone 4可以运行iOS 5,也无法进行BLE通信。
Well some information..
那么一些信息......
#3
1
if you call scanForPeripherals function in didUpdateState delegate then it works because delegate functions can't return.
如果你在didUpdateState委托中调用scanForPeripherals函数然后它工作,因为委托函数不能返回。
#1
12
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 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.
Apple提供了一个很棒的示例应用程序。另外,请查看WWDC 2012会话703 - CoreBluetooth 101和705 - 高级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或更高版本。
#2
3
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
需要考虑的一些要点
- on iOS 5.0 -> iPhone can only act as Central, so communication between 2 iOS devices is not possible.
- 在iOS 5.0上 - > iPhone只能作为Central,因此无法在2台iOS设备之间进行通信。
- 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).
- 在iOS 6.0上 - > iPhone也可以作为外设。所以要进行通信,至少有一个设备必须在iOS 6.0上运行(可能以后)。
- 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.
- 添加了BLE硬件的首款iPhone设备是iPhone 4S。因此,即使iPhone 4可以运行iOS 5,也无法进行BLE通信。
Well some information..
那么一些信息......
#3
1
if you call scanForPeripherals function in didUpdateState delegate then it works because delegate functions can't return.
如果你在didUpdateState委托中调用scanForPeripherals函数然后它工作,因为委托函数不能返回。