public async Task <BluetoothSocket> TakeAsyncOrTimeout(BluetoothDevice device) { Channel <BluetoothSocket> channel; using (await synchronization.LockAsync().ConfigureAwait(false)) { var deviceId = MacUtilities.ConvertMacToGuid(device.Address); channel = pendingRequests.GetOrAdd(deviceId, add => ChannelFactory.Nonblocking <BluetoothSocket>()); } bool isTimeout = false; BluetoothSocket result = null; await new Select { Case(ChannelFactory.Timeout(10000), () => { isTimeout = true; }), Case(channel, x => { result = x; }) }.ConfigureAwait(false); if (isTimeout) { throw new TimeoutException(); } BluetoothSocket temporary; while (channel.TryRead(out temporary)) { result = temporary; } return(result); }
public async Task GiveAsync(BluetoothSocket socket) { Channel <BluetoothSocket> channel; using (await synchronization.LockAsync().ConfigureAwait(false)) { var deviceId = MacUtilities.ConvertMacToGuid(socket.RemoteDevice.Address); Console.WriteLine("INBOUND CONNECTION FROM " + deviceId + " aka " + (socket.RemoteDevice.Name ?? "[unknown]")); channel = pendingRequests.GetOrAdd(deviceId, add => ChannelFactory.Nonblocking <BluetoothSocket>()); await ChannelsExtensions.WriteAsync(channel, socket).ConfigureAwait(false); } }
public AndroidBluetoothAdapter( Context applicationContext, BluetoothAdapter bluetoothAdapter, BluetoothDiscoveryFacade bluetoothDiscoveryFacade, InboundBluetoothSocketTable inboundBluetoothSocketTable ) { this.bluetoothAdapter = bluetoothAdapter; this.bluetoothDiscoveryFacade = bluetoothDiscoveryFacade; this.inboundBluetoothSocketTable = inboundBluetoothSocketTable; // bluetoothAdapter.Address lies. See // http://stackoverflow.com/questions/33377982/get-bluetooth-local-mac-address-in-marshmallow var bluetoothAddress = Android.Provider.Settings.Secure.GetString(applicationContext.ContentResolver, "bluetooth_address"); AdapterId = MacUtilities.ConvertMacToGuid(bluetoothAddress); }
public async Task <IReadOnlyList <IBluetoothNeighbor> > DiscoverAsync() { var devices = await bluetoothDiscoveryFacade.DiscoverPeersAsync().ConfigureAwait(false); var neighbors = new List <IBluetoothNeighbor>(); foreach (var device in devices) { if (device.Name == null || (!device.Name.Contains("Spy") && !device.Name.Contains("G920") && !device.Name.Contains("DESKTOP"))) { continue; } var neighborId = MacUtilities.ConvertMacToGuid(device.Address); Neighbor neighbor; if (!neighborsById.TryGetValue(neighborId, out neighbor)) { neighbor = new Neighbor(this, inboundBluetoothSocketTable, device); neighborsById[neighborId] = neighbor; } neighbors.Add(neighbor); } return(neighbors); }