Ios 如何从中央设备(iPhone 7)向外围设备(PSoC BLE)写入布尔值
我对BLE实现非常陌生,所以请容忍我。我使用的是Xcode 9.3、iOS 11.3版和PSoC 4200 BLE设备 我有一个自定义电路连接到PSoC模块,该模块输出一个电压,通过uint32数据包发送到我的iOS应用程序。我的下一步是能够创建一个Ios 如何从中央设备(iPhone 7)向外围设备(PSoC BLE)写入布尔值,ios,objective-c,bluetooth-lowenergy,cbperipheral,cbcentralmanager,Ios,Objective C,Bluetooth Lowenergy,Cbperipheral,Cbcentralmanager,我对BLE实现非常陌生,所以请容忍我。我使用的是Xcode 9.3、iOS 11.3版和PSoC 4200 BLE设备 我有一个自定义电路连接到PSoC模块,该模块输出一个电压,通过uint32数据包发送到我的iOS应用程序。我的下一步是能够创建一个UIButton对象(现在只是一个开/关),并将其状态传递回PSoC 忘记了用于设置PSoC的软件(Cypress的PSoC Creator 4.0仅供参考),我想知道在Xcode中使用哪些委托方法来实现此功能…我的最佳猜测是peripheralMa
UIButton
对象(现在只是一个开/关),并将其状态传递回PSoC
忘记了用于设置PSoC的软件(Cypress的PSoC Creator 4.0仅供参考),我想知道在Xcode中使用哪些委托方法来实现此功能…我的最佳猜测是peripheralManager:didReceiveWriteRequests:
在CBPeripheralManagerDelegate
协议中。如果我只能在中央读取特征后回写到外围设备,那么我可以暂时使用uint32电压包的最后8位,如果这是过程需要的话。如果最好创建一个新的uint8或布尔数据包,并将其用作写入特性,而不干预uint32,那么我也可以这样做
到目前为止,我没有传输时间要求或能耗问题……我只需要将布尔值发送到PSoC
任何帮助都将不胜感激,谢谢
更新:
调用外围设备:didUpdateValueForCharacteristic:error:
后,我调用[self-getTransducerData:characteristic withPeripheral:peripheral error:error]代码>
下面是该方法中发生的情况(我添加了我认为是使用writeValue:forCharacteristic:type:
的适当方法)
-(void)getTransducerData:(CBCharacteristic*)特征与外围设备:(CBPeripheral*)外围设备
错误:(n错误*)错误{
NSData*数据=[特征值];
const uint8_t*reportData=[数据字节];
uint32_t v0=(uint32_t)报告数据[0];
uint32_t v1=(uint32_t)reportData[1]你能展示你写数据的尝试吗?你不能这样写bool。你只需要发送一个8位字节,然后决定你对真/假的约定是什么(例如,0=假,其他任何东西都是真的)。你应该为你的bool使用不同的特征。我最终使用了[3]元素来存储0x00或0x01…当我调用writeValue:forCharacteristic:type:
方法,并在我的Cypress-BLE软件中评估它的效果时,我意识到,除非我通过该软件操纵传入的数据,否则它只会运行一个写请求序列,而不会执行任何操作。我决定使用该触发器作为一种手段o模拟开/关开关。完全不是,当然也不是最好的解决方案(当我更熟悉这个过程时,我会尝试按照你的建议来处理2个特征。)
- (void)getTransducerData:(CBCharacteristic*)characteristic withPeripheral:(CBPeripheral*)peripheral
error:(NSError*)error {
NSData* data = [characteristic value];
const uint8_t *reportData = [data bytes];
uint32_t v0 = (uint32_t)reportData[0];
uint32_t v1 = (uint32_t)reportData[1] << 8;
uint32_t v2 = (uint32_t)reportData[2] << 16;
//uint32_t v3 = (uint32_t)reportData[3] << 24;
int32_t voltage = v0 | v1 | v2; //| v3;
if ((characteristic.value) || !error) {
/* _captureOn is the boolean value after my UIButton object receives a touch.
On and Off.
If it's on, notify the peripheral with the last 8 bits of the uint32_t characteristic with 0x01.*/
if (_captureOn) {
uint8_t temp[4];
temp[0] = v0;
temp[1] = v1;
temp[2] = v2;
temp[3] = 0x01;
NSData *data = [NSData dataWithBytesNoCopy:temp length:4 freeWhenDone:YES];
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
self.transducerValue = voltage;
self.realVoltage = voltage/1000000.0;
//self.levelView.value = self.realVoltage;
if (self.realVoltage <= _originalHigh && self.realVoltage >= _originalLow) {
self.testerVoltage = self.realVoltage;
self.middleTachometer.currentLevel = (float)self.realVoltage;
self.transducerPosition.text = [NSString stringWithFormat:@"%0.3lf V", self.realVoltage];
}
}
}