iOS 蓝牙 BLE 连接到设备但显示服务为空
<p><p>我正在连接蓝牙系统 <a href="http://redbearlab.com/blemini/" rel="noreferrer noopener nofollow">http://redbearlab.com/blemini/</a> </p>
<p>从服务中读取一些数据,</p>
<p>如何查看可用于此设备的服务,
我已经使用 <a href="https://itunes.apple.com/us/app/lightblue/id557428110?mt=8&ign-mpt=uo=4" rel="noreferrer noopener nofollow">lightblue ios app</a> 进行了检查我的设备有服务。</p>
<p>我可以连接到我的设备并获取名称,</p>
<p>但我的服务数组显示为 null [使用上述应用检查的不是 null]</p>
<p>这是我的代码</p>
<pre><code>#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
{
;
;
;
;
}
- (void)viewDidLoad
{
;
;
self.view.backgroundColor = ;
NSLog(@"mgmt");
self.manager = [initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSLog(@"centralManagerDidUpdateState");
switch (central.state) {
case CBCentralManagerStatePoweredOn:
// Scans for any peripheral
;
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: %@", );
NSLog (@"UUID peripheral: %@", );
NSLog (@"peripheral services: %@", );
self.deviceName.text = ];
self.deviceUUID.text = ];
;
}
//scan for peripherals
- (int) scanForPeripherals {
NSLog(@"scanForPeripherals");
NSDictionary *options = , CBCentralManagerScanOptionAllowDuplicatesKey, nil];
;
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Central Manager didConnectPeripheral");
self.deviceConnected.text = @"Device Connected: YES";
NSLog (@"UUID services: %@", );
// Clears the data that we may already have
;
// Sets the peripheral delegate
;
// Asks the peripheral to discover the service
//test! direct
CBUUID*serviceUUID = ;
NSArray *serviceArray = ;
;
}
- (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error {
NSLog(@"didDiscoverServices");
if (error) {
NSLog(@"Error discovering service: %@", );
//;
return;
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
NSLog(@"didDiscoverCharacteristicsForService");
if (error) {
NSLog(@"Error discovering characteristic: %@", );
//;
return;
}
}
//
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"didUpdateNotificationStateForCharacteristic");
if (error) {
NSLog(@"Error changing notification state: %@", error.localizedDescription);
}
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
@end
</code></pre>
<p>所以请就我的服务数组为何为空提供建议?
以及如何访问它以“阅读”谢谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您必须使用您在 didDiscoverPeripheral 中获得的外围设备设置您的属性 self.activePeripheral。您的代码正在为 null 的外围设备发现服务,这就是您没有获得服务的原因。</p></p>
<p style="font-size: 20px;">关于iOS 蓝牙 BLE 连接到设备但显示服务为空,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16142017/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16142017/
</a>
</p>
页:
[1]