。我正在开发一个使用核心蓝牙框架的应用程序.App与启动,锁定,解锁门相关。在Apple提供的示例的帮助下,我几乎完成了应用程序。那就是心率监测仪和温度传感器。
我有两个问题。
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");
}
}
}
}
答案 0 :(得分:0)
您将使用两者。您将扫描(如果之前未连接),然后发现服务,然后发现特征。完成后,您将从您的按钮调用writeValueForCharacteristic,其中包含您要写入的任何值。