0

. 我正在开发一个使用核心蓝牙框架的应用程序。应用程序与启动、锁定、解锁门有关。在 Apple 提供的示例的帮助下,我几乎完成了该应用程序。那就是心率监测器和温度传感器。

我有 2 个问题。1:心率监视器应用程序从外围设备读取数据,而在我的情况下,我只需向startButton按下的外围设备发出信号。所以我的问题是我是否必须使用writeValueForCharacteristic方法 discoverCharacteristics

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");
            }
        }
    }

}
4

1 回答 1

0
  1. 您将同时使用两者。您将扫描(如果之前未连接),然后发现服务,然后发现特征。完成后,您将从按钮调用 writeValueForCharacteristic,并使用您希望写入的任何值。

    • (IBAction) startButtonPressed: (id) sender { [peripheral writeValue:yourNSData forCharacteristic:yourCBCharacteristic type:yourWriteType]; }
于 2014-02-26T00:32:00.147 回答