【发布时间】:2017-07-20 01:35:26
【问题描述】:
编辑: 删除了 iBeacon 代码。因为 iBeacon 代码没有问题。
-> 一旦我们识别出我们的信标,我们就必须使用 BLE 与第一个信标进行通信。我们在 Respberry PI 上使用了 Bleno 框架。
//传递我的服务UUID
[bluetoothManager scanForPeripheralsWithServices:@[@"87fa7df2-748c-4093-8e73-b93ee73543b4"] options:scanOptions];
问题是,如果我们通过我的服务 UUID(使用 Light Blue App 验证),我不会得到任何结果。相反,如果我们传递“nil”,我至少能够识别我的设备。
那么,是否有任何通用方法可以从 iOS 代码中识别我们的蓝牙设备。由于 CBPeripheral 返回有限的属性。
我尝试使用以下命令使用 BLENO 框架更改我的蓝牙设备名称。
sudo BLENO_DEVICE_NAME="SOME_NAME_86" 节点 iBeacon.js
有时,我能读懂这个名字。但并非所有时候。所以我正在寻找替代解决方案。
这是我的代码:
@implementation 视图控制器
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
_data = [[NSMutableData alloc] init];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBManagerStatePoweredOn:
[_centralManager scanForPeripheralsWithServices:@[[CBUUID
UUIDWithString:@"87FA7DF2-748C-4093-8E73-B93EE73543B4"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES}]; break;
default: break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
if (_discoveredPeripheral != peripheral) {
_discoveredPeripheral = peripheral;
[_centralManager connectPeripheral:peripheral options:nil];
[_centralManager stopScan];
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Failed to connect");
[self cleanup];
}
- (void)cleanup {
// See if we are subscribed to a characteristic on the peripheral
if (_discoveredPeripheral.services != nil) {
for (CBService *service in _discoveredPeripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
if (characteristic.isNotifying) {
[_discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[_centralManager cancelPeripheralConnection:_discoveredPeripheral];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected");
[_centralManager stopScan];
NSLog(@"Scanning stopped");
[_data setLength:0];
peripheral.delegate = self;
[peripheral discoverServices:@[[CBUUID UUIDWithString:@"87FA7DF2-748C-4093-8E73-B93EE73543B4"]]];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
[self cleanup];
return;
}
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];
}
// Discover other characteristics
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"Error");
return;
}
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
if ([stringFromData isEqualToString:@"EOM"]) {
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
[_centralManager cancelPeripheralConnection:peripheral];
}
[_data appendData:characteristic.value];
}
@结束
问题是,我有两个蓝牙设备并排,广告相同的服务 ID。根据场景,我应该只连接一台设备并读取它们的特征。
【问题讨论】:
-
您不能将信标 UUID 与 BLE 外围设备相关联。您需要使用 CoreBluetooth 来发现您的广告外围设备并以这种方式连接到它们。 CoreBluetooth 向您的应用提供的标识符与设备 MAC 相关,但不是设备 MAC。 Beacon UUID 与 MAC 或 CoreBluetooth 标识符无关
-
感谢保罗的评论。我也尝试了 iBeacnon UUID。无论如何,这是正确的。在同一代码中,我还在 scanPeripheral 方法中添加了我的服务 UUID(对我的设备唯一)。但它没有找到具有该服务 UUID 的我的蓝牙设备。如果不识别我的蓝牙设备,我无法继续。
-
一旦您检测到设备宣传您的服务,您需要连接并读取某些特征的值来识别它。您无法仅从服务广告中识别特定设备
-
我尝试在 scanForPeripherals 方法中使用 Service UUID 连接到我的设备服务。 [bluetoothManager scanForPeripheralsWithServices:@[87FA7DF2-748C-4093-8E73-B93EE7354666] 选项:无];由于我的设备处于活动状态,仍然无法找到蓝牙。
-
您的设备是否在宣传该服务?从应用商店中的 LightBlue 应用开始,确保它可以看到您的设备和该服务。
标签: ios objective-c bluetooth cbperipheral cbcentralmanager