2010-08-23 24 views

Répondre

1

Je sais que cela est un peu en retard, étant plus de 18 mois après avoir posé la question, mais voici un peu de code que j'utilise en état de la batterie:

mach_port_t  masterPort; 
kern_return_t kr; 
io_iterator_t ite; 
io_object_t  obj = 0; 
CFMutableDictionaryRef properties; 

kr = IOMasterPort(bootstrap_port, &masterPort); 
if (kr != KERN_SUCCESS) 
    printf("IOMasterPort() failed: %x\n", kr); 

kr = IORegistryCreateIterator(masterPort, 
           kIOServicePlane, 
           true, 
           &ite); 

while ((obj = IOIteratorNext(ite))) 
{ 
    kr = IORegistryEntryCreateCFProperties(obj, 
              &properties, 
              kCFAllocatorDefault, 
              kNilOptions); 

    if ((kr != KERN_SUCCESS) || !properties) { 
     printf("IORegistryEntryCreateCFProperties error %x\n", kr); 
    } 

    else 
    { 
     CFNumberRef percent = (CFNumberRef) CFDictionaryGetValue(properties, CFSTR("BatteryPercent")); 
     if (percent) 
     { 
      SInt32 s; 
      if(!CFNumberGetValue(percent, kCFNumberSInt32Type, &s)) 
      { 
       printf("***CFNumber overflow***\n"); 
      } 

      else 
      { 
       NSDictionary *deviceProperties = (__bridge NSDictionary *)properties; 
       //Use the key @"BatteryPercent" in this dictionary to access the battery percent of any bluetooth mouse, keyboard, or trackpad connected. 
      } 
     } 
    } 
} 

espoir qui aide ... si vous utilisez Spotlight pour trouver l'application IORegistryExplorer, qui peut aider à déterminer quelles autres clés peuvent être utiles dans le dictionnaire pour trouver d'autres informations utiles (comme le nom ou le type de l'appareil.)