iOS ble写入不改变值

时间:2017-06-27 06:03:35

标签: ios objective-c bluetooth bluetooth-lowenergy core-bluetooth

通过iOS发送数据(writeValue:forCharacteristic:type :)方法可以正常工作而不会出错,但值不会改变。你知道为什么吗?

> 2017-06-27 14:53:54.846963+0900 BluetoothSample[12662:5477661] Did
> write  characteristic <CBCharacteristic: 0x1700ad500, UUID =
> 2A588021-4FB2-40F5- 8204-85315DEF11C5, properties = 0xA, value =
> <1357>, notifying = NO>


uint8_t val = 123;
NSData * valData = [NSData dataWithBytes:(void*)&val length:sizeof(val)];


 [self.discoveredPeripheral writeValue:valData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];


- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if (error) {
         NSLog(@"didWriteValueForCharacteristic With error: %@", [error localizedDescription]);

        self.receivePaketDataTextView.text = [[NSString alloc] initWithFormat:@"%@",[error localizedDescription]];
        return ;
    }

    self.receivePaketDataTextView.text = [[NSString alloc] initWithFormat:@"%@",characteristic.value];
}

当我尝试写入时,只打印相同的日志。

2 个答案:

答案 0 :(得分:2)

尝试:

[self.discoveredPeripheral writeValue: valData
           forCharacteristic:self.usedCharacteristic
                        type:CBCharacteristicWriteWithoutResponse];

使用类型:CBCharacteristicWriteWithoutResponse

同时检查您是否设置了通知程序

  [self.discoveredPeripheral setNotifyValue:YES forCharacteristic:self.usedCharacteristic];

在你的代表面前。

答案 1 :(得分:0)

在您的iOS中心,value中的didWriteValueForCharacteristic特征的Exception: System.Web.HttpUnhandledException Message: Exception of type 'System.Web.HttpUnhandledException' was thrown. Source: System.Web at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.sitecore_shell_controls_treeviewex_treeviewex_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\252d77a1\7452ed5e\App_Web_0so42fby.0.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) Nested Exception Exception: System.ArgumentNullException Message: Null ids are not allowed. Parameter name: name Source: Sitecore.Kernel at Sitecore.Diagnostics.Assert.ArgumentNotNullOrEmpty(String argument, String argumentName) at Sitecore.Web.UI.HtmlControls.Data.DataViewFactory.GetDataView(String name, String parameters) at Sitecore.Web.UI.WebControls.TreeviewEx.RenderChildren(HtmlTextWriter output, Item parent) at Sitecore.Web.UI.WebControls.TreeviewEx.Render(HtmlTextWriter output) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Page.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 并未反映您刚才所写的更改。

您需要发出显式读取以获取新值。

这是设计使然,因为无法保证您刚才写的值是特征的当前值;只有外围设备可以为您提供当前值。