iOS 蓝牙 peripheralManagerDidUpdateState 从未调用过

标签 ios objective-c bluetooth core-bluetooth cbperipheralmanager

我正在尝试将我的 iPhone 设置为充当心率监测器并使用标准心率服务发送信息,以便我在 PC 上运行的应用程序可以检索数据。我是 iOS 的新手,但我之前在 Android 和 Windows 上运行过蓝牙。我正在关注信息 here我在第一个障碍上就倒下了……

按照文档中的说明,我首先调用

CBPeripheralManager *myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:(id<CBPeripheralManagerDelegate>)self queue:nil options:nil];

我还实现了 peripheralManagerDidUpdateState 回调,但问题就出在这里。这个回调永远不会运行!我等了几分钟,什么也没发生。我怀疑我错过了一个非常简单的步骤,因为我搜索了一整天但在网上找不到任何东西!任何人都可以解释在代码中和使用我的 iPhone 实际执行操作时要采取的明确步骤吗?在此回调运行之前是否必须有连接?它必须与任何东西配对吗?

这是完整的代码。一切运行良好,我没有收到任何错误。

#import "ViewController.h"
@import CoreBluetooth;

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"App running...");

    //Create the peripheral manager
    CBPeripheralManager *myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:(id<CBPeripheralManagerDelegate>)self queue:nil options:nil];
}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    int state = peripheral.state;
    NSLog(@"Peripheral manager state =  %d", state);

    //Set the UUIDs for service and characteristic
    CBUUID *heartRateServiceUUID = [CBUUID UUIDWithString: @"180D"];
    CBUUID *heartRateCharacteristicUUID = [CBUUID UUIDWithString:@"2A37"];
    CBUUID *heartRateSensorLocationCharacteristicUUID = [CBUUID UUIDWithString:@"0x2A38"];


    //char heartRateData[2]; heartRateData[0] = 0; heartRateData[1] = 60;

    //Create the characteristics
    CBMutableCharacteristic *heartRateCharacteristic =
    [[CBMutableCharacteristic alloc] initWithType:heartRateCharacteristicUUID
                                       properties: CBCharacteristicPropertyNotify
                                            value:nil
                                      permissions:CBAttributePermissionsReadable];

    CBMutableCharacteristic *heartRateSensorLocationCharacteristic =
    [[CBMutableCharacteristic alloc] initWithType:heartRateSensorLocationCharacteristicUUID
                                       properties:CBCharacteristicPropertyRead
                                            value:nil
                                      permissions:CBAttributePermissionsReadable];
    //Create the service
    CBMutableService *myService = [[CBMutableService alloc] initWithType:heartRateServiceUUID primary:YES];
    myService.characteristics = @[heartRateCharacteristic, heartRateSensorLocationCharacteristic];

    //Publish the service
    NSLog(@"Attempting to publish service...");
    [peripheral addService:myService];

    //Set the data
    NSDictionary *data = @{CBAdvertisementDataLocalNameKey:@"iDeviceName",
                           CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"180D"]]};

    //Advertise the service
    NSLog(@"Attempting to advertise service...");
    [peripheral startAdvertising:data];

}

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

    if (error) {
        NSLog(@"Error publishing service: %@", [error localizedDescription]);
    }
    else NSLog(@"Service successfully published");
}

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

    if (error) {
        NSLog(@"Error advertising: %@", [error localizedDescription]);
    }
    else NSLog(@"Service successfully advertising");
}

@end

最佳答案

一旦 viewDidLoad 方法返回,对象 myPeripheralManager 就会被释放,因为您只有一个指向该对象的引用并且它超出了范围。

解决方案是在 ViewController 中创建一个引用 CBPeripheralManager 实例的属性:

@interface ViewController()
@property (nonatomic, strong) CBPeripheralManager *myPeripheralManager;
@end

然后初始化viewDidLoad中的属性:

self.myPeripheralManager = myPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:(id<CBPeripheralManagerDelegate>)self queue:nil options:nil];

您可能想阅读更多关于 Objective-C 中的内存管理(特别是自动引用计数)的内容。

关于iOS 蓝牙 peripheralManagerDidUpdateState 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248908/

相关文章:

objective-c - 当再次滚动表格 View 时,单元格显示为空白?

ios - EAAccessoryManager 配件选择器错误

html - 互联网不可用时离线 UIWebView

ios - 为什么创建大量 SKNode 需要这么长时间

ios - 在项目中手动导入时,从 swagger-codegen 生成的 Objective c 客户端无法正常工作

objective-c - EXC_BAD_ACCESS 访问结构体变量时出现运行时异常->Objective C

android - 如何在没有用户输入的情况下创建 Android RFCOMM 套接字?

java - 通过蓝牙将超声波传感器的数据从 Arduino 发送到 Android

ios - 着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中无法运行

ios - UICollectionView 放大选择的单元格