• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS 蓝牙 BLE 连接到设备但显示服务为空

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:38:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在连接蓝牙系统 http://redbearlab.com/blemini/

从服务中读取一些数据,

如何查看可用于此设备的服务, 我已经使用 lightblue ios app 进行了检查我的设备有服务。

我可以连接到我的设备并获取名称,

但我的服务数组显示为 null [使用上述应用检查的不是 null]

这是我的代码

#import "MainViewController.h"



@interface MainViewController ()

@property (nonatomic, retain)UILabel *deviceName;
@property (nonatomic, retain)UILabel *deviceUUID;
@property (nonatomic, retain)UILabel *deviceConnected;


@end

@implementation MainViewController

- (void)dealloc
{
    [_deviceName release];
    [_deviceUUID release];
    [_deviceConnected release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initUI];

    self.view.backgroundColor = [UIColor lightGrayColor];
    NSLog(@"mgmt");

    self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
}



- (void)centralManagerDidUpdateStateCBCentralManager *)central {

    NSLog(@"centralManagerDidUpdateState");

    switch (central.state) {
        case CBCentralManagerStatePoweredOn:

            // Scans for any peripheral
            [self.manager scanForPeripheralsWithServices:nil options:nil];

            break;
        default:
            NSLog(@"Central Manager did change state");
            break;
    }
}

// device discovered
- (void)centralManagerCBCentralManager *)central didDiscoverPeripheralCBPeripheral *)peripheral advertisementDataNSDictionary *)advertisementData RSSINSNumber *)RSSI {


    NSLog (@"Discovered peripheral: %@", [peripheral name]);
    NSLog (@"UUID peripheral: %@", [peripheral UUID]);

    NSLog (@"peripheral services: %@", [peripheral services]);


    self.deviceName.text = [NSString stringWithFormat"Device Name: %@", [peripheral name]];
    self.deviceUUID.text = [NSString stringWithFormat"Device UUID: %@",[peripheral UUID]];

    [self.manager connectPeripheral:peripheral options:nil];
}




//scan for peripherals
- (int) scanForPeripherals {

    NSLog(@"scanForPeripherals");

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    [self.manager scanForPeripheralsWithServices:nil optionsptions];
    return 0;
}


- (void)centralManagerCBCentralManager *)central didConnectPeripheralCBPeripheral *)peripheral {
    NSLog(@"Central Manager didConnectPeripheral");


    self.deviceConnected.text = @"Device Connected: YES";

    NSLog (@"UUID services: %@", [peripheral services]);


    // Clears the data that we may already have
    [self.data setLength:0];
    // Sets the peripheral delegate
    [self.activePeripheral setDelegate:self];
    // Asks the peripheral to discover the service

//test! direct
    CBUUID  *serviceUUID    = [CBUUID UUIDWithString"D298E274-3CB0-2C5F-A0B0-A90D97304453"];
    NSArray *serviceArray   = [NSArray arrayWithObjects:serviceUUID, nil];

    [self.activePeripheral discoverServices:serviceArray];



}



- (void)peripheralCBPeripheral *)aPeripheral didDiscoverServicesNSError *)error {

    NSLog(@"didDiscoverServices");


    if (error) {
        NSLog(@"Error discovering service: %@", [error localizedDescription]);
        //[self cleanup];
        return;
    }

}

- (void)peripheralCBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    NSLog(@"didDiscoverCharacteristicsForService");

    if (error) {
        NSLog(@"Error discovering characteristic: %@", [error localizedDescription]);
        //[self cleanup];
        return;
    }

}

//
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    NSLog(@"didUpdateNotificationStateForCharacteristic");

    if (error) {
        NSLog(@"Error changing notification state: %@", error.localizedDescription);
    }


}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

所以请就我的服务数组为何为空提供建议? 以及如何访问它以“阅读”谢谢!



Best Answer-推荐答案


您必须使用您在 didDiscoverPeripheral 中获得的外围设备设置您的属性 self.activePeripheral。您的代码正在为 null 的外围设备发现服务,这就是您没有获得服务的原因。

关于iOS 蓝牙 BLE 连接到设备但显示服务为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142017/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap