0

我使用 Apple BLTE Transfer 示例https://developer.apple.com/library/content/samplecode/BTLE_Transfer/Introduction/Intro.html构建了我的应用程序,并对其进行了修改以尝试连接到我正在运行的 HM-11 芯片作为 iBeacon。

在前台,应用程序能够发现 HM-11:

2017-02-26 17:46:26.872 BTLE Transfer[831:436577] Scanning started
2017-02-26 17:46:26.896 BTLE Transfer[831:436577] Discovered MY_DEVICE at -46
2017-02-26 17:46:26.896 BTLE Transfer[831:436577] Connecting to peripheral <CBPeripheral: 0x17d52de0, identifier = 8ADFF3DC-A902-3CE6-CDEB-168E67594AF7, name = MY_DEVICE, state = disconnected>
2017-02-26 17:46:27.202 BTLE Transfer[831:436577] Peripheral Connected
2017-02-26 17:46:27.203 BTLE Transfer[831:436577] Scanning stopped
2017-02-26 17:46:27.501 BTLE Transfer[831:436577] Discovered service: <CBService: 0x17e9d830, isPrimary = YES, UUID = FFE0>
2017-02-26 17:46:27.801 BTLE Transfer[831:436577] Notification began on <CBCharacteristic: 0x17e3a630, UUID = FFE1, properties = 0x16, value = (null), notifying = YES>

但是,在后台没有任何事情发生:

2017-02-26 17:46:31.179 BTLE Transfer[831:436577] Scanning started

以下是如何设置扫描:

/** centralManagerDidUpdateState is a required protocol method.
 *  Usually, you'd check for other states to make sure the current device supports LE, is powered on, etc.
 *  In this instance, we're just using it to wait for CBCentralManagerStatePoweredOn, which indicates
 *  the Central is ready to be used.
 */
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (central.state != CBCentralManagerStatePoweredOn) {
        // In a real app, you'd deal with all the states correctly
        return;
    }

    // The state must be CBCentralManagerStatePoweredOn...

    // ... so start scanning
    [self scan];

}


/** Scan for peripherals - specifically for our service's 128bit CBUUID
 */
- (void)scan
{
//    [self.centralManager scanForPeripheralsWithServices:nil
//                                                options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];
    [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]
                                                options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];

    NSLog(@"Scanning started");
}

我确保只有在我进入该CBCentralManagerStatePoweredOn州后才开始扫描。

在扫描时,我指定要查找的服务 UUID 为FFE0.

这被定义TransferService.h为:

#define TRANSFER_SERVICE_UUID @"FFE0"
#define TRANSFER_CHARACTERISTIC_UUID @"FFE1"

而且我意识到在后台模式下不会扫描重复的键。所以我测试了扫描开始时关闭HM-11,应用程序进入后台,然后打开HM-11的情况。我认为这应该确保该设备尚未被应用程序“发现”,因此它不会显示为重复键。但即使在这种情况下,didDiscoverPeripheral也只有在应用程序返回到前台后才会调用委托。

我正在初始化CBCentralManager这样的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Start up the CBCentralManager
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];

    // And somewhere to store the incoming data
    _data = [[NSMutableData alloc] init];
}

我还在以下代码中包含了我的应用程序的必要权限Supporting Files\BTLE Transfer-Info.plist

<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
    <string>bluetooth-peripheral</string>
</array>

Capabilities应用程序的选项卡中,两者Uses Bluetooth LE accessoriesActs as a Bluetooth LE accessory被选中。我不需要,Acts as a Bluetooth LE accessory但我只是在尝试不同的东西,看看我是否可以让它工作。

关于为什么当应用程序在后台运行时我没有发现这个外围设备的任何想法?

4

0 回答 0