【问题标题】:iOS App working with BLE mini与 BLE mini 配合使用的 iOS 应用程序
【发布时间】:2014-06-17 08:53:58
【问题描述】:
我已将我的 iOS 应用程序连接到一个 BLE Mini 并且它到达了didDiscoverCharacteristics 方法。
我对这里应该发生的事情有点困惑。我发现的几个示例使用 for 循环来遍历特征,并在找到正确的特征时执行某些操作。有没有办法找到特征的特定ID?
【问题讨论】:
标签:
ios
methods
bluetooth
bluetooth-lowenergy
core-bluetooth
【解决方案1】:
您应该使用CBCentralManager 并实现CBCentralManagerDelegate。
然后,在didDiscoverPeripheral: 方法中检查CBPeripheral 对象,检查它是否是您正在寻找的对象并执行您的工作。
实施
-
声明一个属性。
@property(nonatomic, strong) CBCentralManager *centralManager;
-
创建一个CBCentralManager。
_centralManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil];
-
实现CBCentralManagerDelegate 协议。
- (void)centralManagerDidUpdateState:(CBCentralManager *)centralManager
{
if (centralManager.state == CBCentralManagerStatePoweredOn)
{
NSLog(@"Start scanning");
// This will scan for all peripherals.
[_centralManager scanForPeripheralsWithServices:nil
options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSLog(@"Peripheral: %@", peripheral);
// Do stuff.
}
祝你好运。