ios - 从 iOS 设备上的 BLE 获取制造商数据
<p><p>在我的 Xamarin.Android 应用中,我可以使用以下代码从 BLE 读取制造商数据:</p>
<pre><code>public class CustomScanCallback : ScanCallback
{
public override void OnScanResult( ScanCallbackType callbackType, ScanResult result)
{
base.OnScanResult(callbackType, result);
if (result.ScanRecord.ManufacturerSpecificData != null)
{
var dataByteResult = result.ScanRecord.GetManufacturerSpecificData(0xFFFE);
if (dataByteResult != null)
{
Guid dataResult = new Guid(dataByteResult);
if (dataResult.ToString() == "myUuid")
{
// found the uuid from my UWP app
}
}
}
}
}
</code></pre>
<p>如何在 Xamarin.iOS 上做同样的事情?当外围设备被发现时,我有一个回调:</p>
<pre><code>_cbCentralManager.DiscoveredPeripheral += CBCentralManager_DiscoveredPeripheral;
private void CBCentralManager_DiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
{
// how to find "myUuid" ?
}
</code></pre>
<p>并且尝试了很多方法来找到它,但找不到。 Swift/Obective-C 中的答案也会有所帮助,因为我可以将其翻译成 C#。</p>
<p><strong>编辑</strong></p>
<p>我用 <code>CBAdvertisementDataManufacturerDataKey</code> 键查看了广告数据参数,找到了值:</p>
<p><code><06000109 2002aa4e 7e54b91f 2212c398 74eb0fe9 9fc3ecce 4ce76d8f aa></code></p>
<p>看起来它是用十六进制格式编码的,但不要以为我要找的是我的 uuid...</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>十六进制代码是 <code>NSData</code> 你可以看到它来自 <a href="https://developer.apple.com/documentation/corebluetooth/cbadvertisementdatamanufacturerdatakey?language=objc" rel="noreferrer noopener nofollow">these</a>文档。要将其转换为字符串,我将使用以下内容。</p>
<pre><code> NSData data = NSData.FromData(theValueFromDictionary);
string uuidString = data.ToString();
//Try this if the other line doesn't work. But string encoding should be UTF-16 (the default) or UTF-8
//string uuidString = data.ToString(NSStringEncoding.UTF8);
</code></pre>
<p>如果那不是您想要的字符串,那么您可以尝试使用您正在使用的编码类型,但根据我的经验,苹果通常使用 <code>UTF-8</code> 在数据和字符串之间切换。</code> p>
<p>也许 UUID 是这个字典中的另一个键。查看所有可用的 key<a href="https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/advertisement_data_retrieval_keys?language=objc" rel="noreferrer noopener nofollow">here</a> </p></p>
<p style="font-size: 20px;">关于ios - 从 iOS 设备上的 BLE 获取制造商数据,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/55105741/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/55105741/
</a>
</p>
页:
[1]