目前正致力于xamarin并尝试通过蓝牙连接扫描仪设备(Zebra扫描仪)。 CoreBluetooth.CBPeripheral用于获取id,name等设备的信息......
扫描使用以下代码的设备:
protected override async Task StartScanningForDevicesNativeAsync(Guid[] serviceUuids, bool allowDuplicatesKey, CancellationToken scanCancellationToken)
{
// Wait for the PoweredOn state
await WaitForState(CBCentralManagerState.PoweredOn, scanCancellationToken).ConfigureAwait(false);
if (scanCancellationToken.IsCancellationRequested)
throw new TaskCanceledException("StartScanningForDevicesNativeAsync cancelled");
Trace.Message("Adapter: Starting a scan for devices.");
CBUUID[] serviceCbuuids = null;
if (serviceUuids != null && serviceUuids.Any())
{
serviceCbuuids = serviceUuids.Select(u => CBUUID.FromString(u.ToString())).ToArray();
Trace.Message("Adapter: Scanning for " + serviceCbuuids.First());
}
DiscoveredDevices.Clear();
_centralManager.ScanForPeripherals(serviceCbuuids, new PeripheralScanningOptions { AllowDuplicatesKey = allowDuplicatesKey });
}
通过以下代码获取事件响应:
_centralManager.DiscoveredPeripheral += (sender, e) =>
{
Trace.Message("DiscoveredPeripheral: {0}, Id: {1}", e.Peripheral.Name, e.Peripheral.Identifier);
System.Diagnostics.Debug.WriteLine("DiscoveredPeripheral: {0}, Id: {1}", e.Peripheral.Name, e.Peripheral.Identifier);;
var name = e.Peripheral.Name;
foreach (var oData in e.AdvertisementData)
{
System.Diagnostics.Debug.WriteLine("AdvertisementData : key : " + oData.Key + " , Value : " + oData.Value);
}
if (e.AdvertisementData.ContainsKey(CBAdvertisement.DataLocalNameKey))
{
// iOS caches the peripheral name, so it can become stale (if changing)
// keep track of the local name key manually
name = ((NSString)e.AdvertisementData.ValueForKey(CBAdvertisement.DataLocalNameKey)).ToString();
}
var device = new Device(this, e.Peripheral, name, e.RSSI.Int32Value,
ParseAdvertismentData(e.AdvertisementData));
HandleDiscoveredDevice(device);
};
现在,在这个AdvertisementData中不包含CBAdvertisement.DataLocalNameKey,因此无法获取设备名称......?