. 私は、コア bluetooth フレームワークを使用するアプリに取り組んでいます。アプリは、ドアの開始、ロック、ロック解除に関連しています。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");
}
}
}
}