2013-02-11 76 views
0

。我正在研究使用核心藍牙框架的應用程序.App與啓動,鎖定,解鎖門有關。藉助Apple提供的示例,我幾乎可以完成應用程序。那就是心率監測器和溫度傳感器。將數據發送至Core Bluetooth Framework Xcode

我有2個問題。 1:心率監測器應用程序從外設讀取數據,而在我的情況下,我只需要發信號通知startButton被按下的外設。所以我的問題是,我是否需要在discoverCharacteristics方法中使用writeValueForCharacteristic

2:如果是這樣好心幫我在這裏按鈕代碼

- (IBAction) startButtonPressed: (id) sender { 

} 
- (IBAction) lockDoors: (id) sender { 

} 
- (IBAction) unLockDoors: (id) sender { 

} 

代碼由下式給出。

- (void) peripheral:(CBPeripheral *) peripheral didDiscoverServices:(NSError *)error 
{ 
    if (error) 
    { 
     NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); 
     return; 
    } 
    for (CBService * service in peripheral.services) 
    { 
     NSLog(@"Service found with UUID: %@", service.UUID); 

    if([service.UUID isEqual:[CBUUID UUIDWithString:@"1809"]]) 
    { 
     /* Car Start Service - discover 

     CAR_START_CHARACTERISTIC_UUID, 
     LOCK_DOOR_CHARACTERISTIC_UUID, 
     UNLOCK_DOOR_CHARACTERISTIC_UUID, 
     TRUNK_CHARACTERISTIC_UUID 
     ALARM_CHARACTERISTIC_UUID 

     */ 
     [peripheralManager discoverCharacteristics:[NSArray arrayWithObjects:[CBUUID UUIDWithString:@"2A1E"], [CBUUID UUIDWithString:@"2A1C"], [CBUUID UUIDWithString:@"2A21"], [CBUUID UUIDWithString:@"2A1F"],[CBUUID UUIDWithString:@"2A1G"],nil] forService:service]; 
    } 
    else if([service.UUID isEqual:[CBUUID UUIDWithString:@"180A"]]) 
    { 
     /* Device Information Service - discover manufacture name characteristic */ 
     [peripheralManager discoverCharacteristics:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"2A29"]] forService:service]; 
    } 
    else if ([service.UUID isEqual:[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]]) 
    { 
     /* GAP (Generic Access Profile) - discover device name characteristic */ 
     [peripheralManager discoverCharacteristics:[NSArray arrayWithObject:[CBUUID UUIDWithString:CBUUIDDeviceNameString]] forService:service]; 
    } 
} 

}

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error 
{ 

    if (error) 
    { 
     NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]); 
     return; 
    } 
    if([service.UUID isEqual:[CBUUID UUIDWithString:@"1809"]]) { 
     for (CBCharacteristic * characteristic in service.characteristics) 
     { 
      /* Set Car Start */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A1C"]]) 
      { 
       self.carStartCharacteristic = characteristic; 
       NSLog(@"Found Car Start Characteristic"); 
      } 
      /* Set Lock Doors */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A1E"]]) 
      { 
       self.lockDoorsCharacteristic = characteristic; 
       NSLog(@"Found Lock Doors Characteristic"); 
      }  
      /* Set Un Lock Doors */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A11"]]) 
      { 
       self.unLockDoorsCharacteristic = characteristic; 
       NSLog(@"Found UnLock Doors Characteristic"); 
      } 
      /* Set Trunk Open */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A13"]]) 
      { 
       self.trunkCharacteristic = characteristic; 
       NSLog(@"Found Trunk Characteristic"); 
      } 
      /* Set Alarm */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A14"]]) 
      { 
       self.alarmCharacteristic = characteristic; 
       NSLog(@"Found Alarm Characteristic"); 
      } 

     } 
    }if([service.UUID isEqual:[CBUUID UUIDWithString:@"180A"]]) 
    { 
     for (CBCharacteristic * characteristic in service.characteristics) 
     { 
      /* Read manufacturer name */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A29"]]) 
      {     
       [peripheralManager readValueForCharacteristic:characteristic]; 
       NSLog(@"Found a Device Manufacturer Name Characteristic - Read manufacturer name"); 
      }   
     } 
    } 

    if ([service.UUID isEqual:[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]]) 
    { 
     for (CBCharacteristic *characteristic in service.characteristics) 
     { 
      /* Read device name */ 
      if([characteristic.UUID isEqual:[CBUUID UUIDWithString:CBUUIDDeviceNameString]]) 
      {     
       [peripheralManager readValueForCharacteristic:characteristic]; 
       NSLog(@"Found a Device Name Characteristic - Read device name"); 
      } 
     } 
    } 

} 

回答

0
  1. 您將同時使用。您將掃描(如果先前未連接),然後發現服務,然後發現特徵。一旦完成,您將從您的按鈕中調用writeValueForCharacteristic以及您希望編寫的任何值。

    • (IBAction爲)startButtonPressed:(ID)發送方{ [外圍writeValue:yourNSData forCharacteristic:yourCBCharacteristic類型:yourWriteType]; }