另一个iOS设备后未调用didDiscoverPeriphera

另一个iOS设备后未调用didDiscoverPeriphera

本文介绍了广告另一个iOS设备后未调用didDiscoverPeripheral的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有两个应用程序,一个用于扫描蓝牙设备,另一个用于广告,而我试图做的是获取所有iOS设备的列表,该列表在我附近的应用程序中扫描iOS设备。

basically i have two apps, one for scanning for bluetooth devices and the other for advertising, and what i am trying to do is to get a list of all iOS devices, within my proximity on the app which scans for the iOS device .

这是我到目前为止的代码:

This is my code so far:

Scanning.m:

// Scan for all available CoreBluetooth LE devices
    NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};
    NSArray *services = @[[CBUUID UUIDWithString:@"1CC024D6-E413-4B56-993C-831CAF033366"]];

    [self.centralManager scanForPeripheralsWithServices:services options:scanOptions];

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"RSSI: %d", [RSSI intValue]);

}

// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    // Determine the state of the peripheral
    if ([central state] == CBCentralManagerStatePoweredOff) {
        NSLog(@"CoreBluetooth BLE hardware is powered off");
    }
    else if ([central state] == CBCentralManagerStatePoweredOn) {
        NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
    }
    else if ([central state] == CBCentralManagerStateUnauthorized) {
        NSLog(@"CoreBluetooth BLE state is unauthorized");
    }
    else if ([central state] == CBCentralManagerStateUnknown) {
        NSLog(@"CoreBluetooth BLE state is unknown");
    }
    else if ([central state] == CBCentralManagerStateUnsupported) {
        NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
    }
}

Advertising.m:

    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    [self listenForRelays];


    /** Required protocol method.  A full app should take care of all the possible states,
 *  but we're just waiting for  to know when the CBPeripheralManager is ready
 */
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{

    if (peripheral.state == CBPeripheralManagerStatePoweredOn)
    {

        // We're in CBPeripheralManagerStatePoweredOn state...
        NSLog(@"self.peripheralManager powered on.");

        // ... so build our service.

    }
}


-(void) listenForRelays {

    if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn)
    {

        NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",
                                          CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"1CC024D6-E413-4B56-993C-831CAF033322"]]};

        // Start advertising over BLE
        [self.peripheralManager startAdvertising:advertisingData];
    }

}

当我在不同的iPhone,什么也没发生!

When i run the different apps on different iphones, nothing happens !

这不会崩溃,但是 didDiscoverPeripheral 永远不会被呼唤:(请帮帮我!是的,我已经阅读了文档,但仍然不行:((

This doesn't crash but didDiscoverPeripheral never gets called :( please help me out ! and yes i have gone through the documentation but still no good :((

欢呼声

推荐答案

只有在进入开机状态后才能启动扫描-在扫描应用中使用类似于广告应用中的代码。

You can't initiate your scan until after you are in the powered-on state - use code in your scanning app similar to that which is in your advertising app.

在打开电源之前在iOS 7中开始扫描的操作会在控制台上发出警告,但仍然可以。在iOS 8中,此操作不起作用。

In iOS 7 starting the scan before you were powered on issued a warning on the console, but it still worked. In iOS 8 it doesn't work.

这篇关于广告另一个iOS设备后未调用didDiscoverPeripheral的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:07