菜鸟教程小白 发表于 2022-12-12 10:38:05

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&amp;ign-mpt=uo=4" rel="noreferrer noopener nofollow">lightblue ios app</a> 进行了检查我的设备有服务。</p>

<p>我可以连接到我的设备并获取名称,</p>

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

<p>这是我的代码</p>

<pre><code>#import &#34;MainViewController.h&#34;



@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(@&#34;mgmt&#34;);

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



- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

    NSLog(@&#34;centralManagerDidUpdateState&#34;);

    switch (central.state) {
      case CBCentralManagerStatePoweredOn:

            // Scans for any peripheral
            ;

            break;
      default:
            NSLog(@&#34;Central Manager did change state&#34;);
            break;
    }
}

// device discovered
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


    NSLog (@&#34;Discovered peripheral: %@&#34;, );
    NSLog (@&#34;UUID peripheral: %@&#34;, );

    NSLog (@&#34;peripheral services: %@&#34;, );


    self.deviceName.text = ];
    self.deviceUUID.text = ];

    ;
}




//scan for peripherals
- (int) scanForPeripherals {

    NSLog(@&#34;scanForPeripherals&#34;);

    NSDictionary *options = , CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    ;
    return 0;
}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@&#34;Central Manager didConnectPeripheral&#34;);


    self.deviceConnected.text = @&#34;Device Connected: YES&#34;;

    NSLog (@&#34;UUID services: %@&#34;, );


    // 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(@&#34;didDiscoverServices&#34;);


    if (error) {
      NSLog(@&#34;Error discovering service: %@&#34;, );
      //;
      return;
    }

}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    NSLog(@&#34;didDiscoverCharacteristicsForService&#34;);

    if (error) {
      NSLog(@&#34;Error discovering characteristic: %@&#34;, );
      //;
      return;
    }

}

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

    NSLog(@&#34;didUpdateNotificationStateForCharacteristic&#34;);

    if (error) {
      NSLog(@&#34;Error changing notification state: %@&#34;, 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]
查看完整版本: iOS 蓝牙 BLE 连接到设备但显示服务为空