我是 iOS 开发新手,正在研究适用于 IOS 的蓝牙低功耗(BLE、蓝牙 4.0)。
我想知道如何在 IOS 7
上使用即时警报服务
。
我可以从BLE 设备
扫描、连接和发现服务
。
接下来是连接到立即警报服务
并将警报级别特征
写入BLE设备
.
我定义了即时警报服务
的UUID和警报级别
,如以下代码所示。
#define IMMEDIATE_ALERT_UUID @"00001802-0000-1000-8000-00805f9b34fb"
#define ALERT_LEVEL_UUID @"00002a06-0000-1000-8000-00805f9b34fb"
以下代码用于连接到即时警报服务
。
[peripheral discoverServices:@[[CBUUID UUIDWithString:IMMEDIATE_ALERT_UUID]]];
连接到IMMEDIATE_ALERT_UUID
后连接到警报级别特征
的代码类似于以下。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:ALERT_LEVEL_UUID]] forService:service];
}
}
订阅警报级别的特征
。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:ALERT_LEVEL_UUID]]) {
// If it is, subscribe to it
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
但是如何将警报级别写入警报级别的特征
??
提前致谢。
最佳答案
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:ALERT_LEVEL_UUID]]){
uint8_t val = 0 //enter the value which you want to write.
NSData* valData = [NSData dataWithBytes:(void*)&val length:sizeof(val)];
[peripheral writeValue:valData forCharacteristic:characteristictype:CBCharacteristicWriteWithResponse];
}
传入参数CBCharacteristicWriteWithResponse
将会调用CBPeripheralDelegate
方法
- (void)外设:(CBPeripheral *)外设 didWriteValueForCharacteristic:(CBCharacteristic *)特征错误:(NSError *)error{
}
关于ios - IOS 7中如何将警报级别写入警报级别的特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148548/