2013-04-22 193 views
2

我连接到蓝牙系统http://redbearlab.com/blemini/iOS的蓝牙BLE连接到空

从服务中读取一些数据的设备,但节目服务,

我怎么能看到可用的服务此设备, 我已经使用lightblue ios app进行检查,我的设备有服务。

我可以连接到我的设备,并获得名字,

但我的服务阵列将显示为空[检查是不是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 

所以请咨询为什么我的服务数组为空? 以及如何访问它以“阅读”谢谢!

+2

尝试:'[self.activePeripheral discoverServices:nil]'发现所有服务。 然后在'didDiscoverServices'中,NSLog将外围服务。 其他可能性:在iDevice上重新启动/关闭蓝牙iDevice/Sensor Switch。有时我的应用不想发现服务。它只是卡住了。不知道为什么。 – Larme 2013-04-24 09:46:18

回答

1

您必须将您的属性self.activePeripheral设置为您在didDiscoverPeripheral中获得的外围设备。你的代码正在为一个无效的外设发现服务,这就是为什么你没有得到服务。