向核心蓝牙框架 Xcode 发送数据
Posted
技术标签:
【中文标题】向核心蓝牙框架 Xcode 发送数据【英文标题】:Sending data to Core Bluetooth Framework Xcode 【发布时间】:2013-02-11 13:53:23 【问题描述】:。我正在开发一个使用核心蓝牙框架的应用程序。应用程序与启动、锁定、解锁门有关。在 Apple 提供的示例的帮助下,我几乎完成了该应用程序。那就是心率监测器和温度传感器。
我有 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");
【问题讨论】:
【参考方案1】:您将同时使用两者。您将扫描(如果之前未连接),然后发现服务,然后发现特征。完成后,您将从按钮调用 writeValueForCharacteristic,并使用您希望写入的任何值。
(IBAction) startButtonPressed: (id) 发件人 [外设 writeValue:yourNSData forCharacteristic:yourCBCharacteristic type:yourWriteType];【讨论】:
以上是关于向核心蓝牙框架 Xcode 发送数据的主要内容,如果未能解决你的问题,请参考以下文章
UART蓝牙通信问题 向UART发送数据的正确格式是啥(整数值)