菜鸟教程小白 发表于 2022-12-11 19:05:22

ios - 如何在 Objective C 中将 CBCharacteristic.value 转换为 NSString


                                            <p><p>我无法将 CBCharacteristic 的 NSData 类型的 value 属性转换为 NSString。我尝试了通常的 initWithData:encoding: NSString 方法,如下所示。但它返回零。 </p>

<pre><code>NSString *str = [ initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(@&#34;%@&#34;, str);
</code></pre>

<p>当我直接对值执行 NSLog 时,我可以在控制台上看到字符串值。</p>

<pre><code>NSLog(@&#34;characteristic.value: %@&#34;, characteristic.value);
</code></pre>

<p>谁能给出关于如何从characteristic.value中获取字符串值的任何想法?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><ul>
<li><p>你需要知道 NSData 是什么意思。可以是字符串、结构、uint32 等。请参见此处:<a href="https://stackoverflow.com/questions/38718841/ios-core-bluetooth-get-all-features-description-from-device/38719661#38719661" rel="noreferrer noopener nofollow">core-bluetooth-get-all-features-description</a> </p></li>
<li><p>BLevel 是否代表电池电量?您需要进行逆向工程或阅读文档(BLE 文档,如果它是“已知”文档,或者设备文档)</p></li>
</ul></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Objective C 中将 CBCharacteristic.value 转换为 NSString,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46923960/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46923960/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Objective C 中将 CBCharacteristic.value 转换为 NSString