gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:29 25 4
gpt4 key购买 nike

我正在连接到蓝牙系统 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)centralManagerDidUpdateState:(CBCentralManager *)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)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)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 options:options];
return 0;
}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)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)peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error {

NSLog(@"didDiscoverServices");


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

}

- (void)peripheral:(CBPeripheral *)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

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

最佳答案

您必须使用您在 didDiscoverPeripheral 中获得的外设来设置您的属性 self.activePeripheral。您的代码正在为一个空的外围设备发现服务,这就是您得不到任何服务的原因。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com