繁体   English   中英

无法在iOS中发现Bluetooth LE服务广告

[英]Failing To Discover Bluetooth LE Service Advertisement in iOS

我创建了两个iOS应用程序; 一个用于发布服务的Bluetooth LE外围设备,以及一个用于扫描发布的服务的Bluetooth LE中心。 外围设备在我的iPhone5s上运行,而中央设备在我的iPad Mini上运行。 我最初将中心设置为扫描特定的广告服务,但后来将其更改为侦听任何服务。 无论哪种情况,充当中心的iPad Mini应用程序都不会检测到任何广告服务。 我不确定这是我设置外围设备管理器来发布广告的方式是否有问题,还是我设置中央管理器进行扫描的方式是否存在问题,还是设备配置问题。 请提供我可以执行的建议或测试。

以下是充当外围设备的iPhone5s应用程序的相关代码:

CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

CBUUID *immediateAlertServiceUUID = [CBUUID UUIDWithString: IMMEDIATE_ALERT_SERVICE_UUID];
CBUUID *alertLevelCharacteristicUUID = [CBUUID UUIDWithString: ALERT_LEVEL_CHARACTERISTIC_UUID];
CBUUID *myCustomCharacteristicUUID = [CBUUID UUIDWithString: MY_CUSTOM_CHARACTERISTIC_UUID];

alertLevelCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead
                                        value: nil permissions:CBAttributePermissionsReadable];
myCustomCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:myCustomCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead
                                        value: nil permissions:CBAttributePermissionsReadable];

NSArray *myCharacteristics = @[alertLevelCharacteristic, myCustomCharacteristic];

// Now setup the service
myService = [[CBMutableService alloc] initWithType:immediateAlertServiceUUID primary:YES];

// Finally, associate the characteristic with the service. This is an array of characteristics
myService.characteristics = myCharacteristics;

[peripheralManager addService:myService];

... wait for user to push button to start advertising ...

// Start Advertising
[peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey : @"My Service",
                                       CBAdvertisementDataServiceUUIDsKey : @[myService.UUID] }];

这是必要的委托方法。 注意:代表方法pheralManagerManagerDidUpdateState会触发并指示“ CoreBluetooth BLE硬件已打开电源并准备就绪”(在中央同样如此)。 委托方法pheralManager:didAddService:error触发时没有错误(请参见下面的输出)。 并委托方法外周管理器DidStartAdvertising:错误触发而没有错误)。 这是从didAddService打印的服务信息:

<CBMutableService: 0x17008efb0 Primary = YES, UUID = 1802, Included Services = (null), Characteristics = (
"<CBMutableCharacteristic: 0x1702c1500 UUID = 2A06, Value = (null), Properties = 0x2, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>",
"<CBMutableCharacteristic: 0x1702c15e0 UUID = 66E613B5-7225-42C6-A9C2-11FADAE62899, Value = (null), Properties = 0x2, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>")>

CBPeripheralManager委托方法(很抱歉,所有代码都只是为了完整而已。):

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

// Determine the state of the peripheral
if ([peripheral state] == CBPeripheralManagerStatePoweredOff) {
    NSLog(@"CoreBluetooth BLE hardware is powered off");
}
else if ([peripheral state] == CBPeripheralManagerStatePoweredOn) {
    NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([peripheral state] == CBPeripheralManagerStateUnauthorized) {
    NSLog(@"CoreBluetooth BLE state is unauthorized");
}
else if ([peripheral state] == CBPeripheralManagerStateUnknown) {
    NSLog(@"CoreBluetooth BLE state is unknown");
}
else if ([peripheral state] == CBPeripheralManagerStateUnsupported) {
    NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
}
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
        didAddService:(CBService *)service
                error:(NSError *)error {

if (error) {
    NSLog(@"Error publishing service: %@", [error localizedDescription]);

    return;
}
else {
    NSLog(@"Hurray! Your Service has been successfully published as: %@", service);
}
}

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
                                   error:(NSError *)error {

if (error == nil) {
    NSLog(@"Your service is now advertising");
}
else {
    NSLog(@"In peripheralManagerDidStartAdvertising: Your service advertising failed with error: %@", error);
}

}

这是在iPad Mini上运行的相关中央代码:

// Scan for all available CoreBluetooth LE devices
NSArray *services = @[[CBUUID UUIDWithString:IMMEDIATE_ALERT_SERVICE_UUID]];

CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

//[centralManager scanForPeripheralsWithServices:services options:nil];
[centralManager scanForPeripheralsWithServices:nil options:nil];
self.centralManager = centralManager;

这是中央委托方法之一。 除了centralManagerDidUpdateState:外,没有任何委托方法可以触发。

// CBPeripheralDelegate - Invoked when you discover the peripheral's available services.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    NSLog(@"Did Discover Services");
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:nil forService:service];
    }
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Did Discover Peripheral");
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if (![localName isEqual:@"My Service"]) {
    // We found the Device
    [self.centralManager stopScan];
    self.myPeripheral = peripheral;
    peripheral.delegate = self;
    [self.centralManager connectPeripheral:peripheral options:nil];
}
}

最后一点,我质疑BLE能否在我的设备上正常工作。 我在iPhone和iPad Mini上加载了几个不同的iBeacon应用程序,以查看是否可以使两台设备识别iBeacon(一个发送,一个接收),但它们也都没有发现iBeacons。 我还尝试了两部iPhone。 我还关闭了蓝牙然后再打开。 我还尝试了关闭/打开设备的电源。 两种设备都在前台运行。 仍然没有运气。 请帮忙。

我将在此处合并所有评论:

使用LightBlueBLE Utility之类的应用程序可以帮助您查找问题是在外围方面还是在中心方面,因为您是自己开发双方的。

在寻找CBServices之前,您必须连接到CBPeripheral 您之前已经展示过的方法,似乎并不明显。

同样,在使用CBCentralManager开始扫描之前,您必须检查其state ,并且必须为CBPeripheralManagerStatePoweredOn

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM