private static async void HandlePairCommand(EndPoint epFrom, string[] parameters) { if (parameters.Length < 3) { Console.WriteLine($"HandlePairCommand(): Too Short Parameters, length={parameters.Length}"); return; } string name = parameters[1]; string deviceId = parameters[2]; Console.WriteLine($"HandlePairCommand(): Trying to pair with Name={name}, DeviceId={deviceId}"); try { DeviceInformation devInfo = await DeviceInformation.CreateFromIdAsync(deviceId); Device device = new(devInfo); DevicePairer.PairDevice(device, "0000"); sPairingServer.SendTo(epFrom, "OK"); } catch (Exception ex) { sPairingServer.SendTo(epFrom, "FAIL"); Console.WriteLine($"Exception: {ex.StackTrace}"); } return; }
public static void UnpairByName(string name, int discoveryTime, Utils.DeviceType deviceType) { var devices = DeviceFinder.FindDevicesByName(DeviceDiscoverer.DiscoverPairedBluetoothDevices(discoveryTime), name, deviceType); if (devices.Count > 1) { throw new Exception($"{devices.Count} devices found, don't know which one to choose"); } DevicePairer.UnpairDevice(devices[0]); }
public async void UnpairWithMac(string deviceId) { DeviceInformation devInfo = await DeviceInformation.CreateFromIdAsync(deviceId); try { DevicePairer.UnpairDevice(new Device(devInfo)); } catch (Exception ex) { Log.V($"UnpairWithMac(): Exception, message={ex.Message}"); } }
public async void PairWithMac(string deviceId) { Earbuds targetEarbuds = GetNaerByEarbirds(deviceId); try { // FIXME: Need to make the device TRUSTED? // await targetEarbuds.SetTrusted(LocalBtAddress, true); // Log.D($"PairWithMac(): Request to be Trusted, result = {result}"); // FIXME: Need to make the device DISCOVERABLE? // result = await targetEarbuds.SetDiscoverable(true); // Log.D($"PairWithMac(): Request to be Discoverable, result = {result}"); if (targetEarbuds != null) { targetEarbuds.BtConnectionStateChangedObservers += OnBtConnectionStateChangedObservers; targetEarbuds.EnableSppConnection(); } if (PairingManager.PairingSupported) { Log.D($"PairWithMac(): Request to Pair via Pairing Manager with {deviceId}"); PairingManager.Send($"pair/unknown/{deviceId}"); // FIXME: Handle Failure Case } else { Log.D($"PairWithMac(): Request to Pair via Windows API with {deviceId}"); DeviceInformation devInfo = await DeviceInformation.CreateFromIdAsync(deviceId); bool result = await DevicePairer.PairDevice(new Device(devInfo), "0000"); Log.D($"PairWithMac(): Request to Pair, result = {result}"); if (result == false) { targetEarbuds.BtConnectionStateChangedObservers -= OnBtConnectionStateChangedObservers; targetEarbuds.DisableSppConnection(); } } } catch (Exception ex) { Log.E($"PairWithMac(): Exception, message={ex.Message}"); } return; }
/* * private static void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin) * { * var devices = DeviceFinder.FindDevicesByMac(DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTime), mac, deviceType); * * if (devices.Count == 1) * { * DevicePairer.PairDevice(devices[0], pin); * return; * } * * if (devices.Count == 2) * { * throw new Exception( * $"2 devices with the mac '{mac}' found \"{devices[0]}\" and \"{devices[1]}\". Don't know which one to choose.\n"); * } * * throw new Exception( * $"{devices.Count} devices with the mac '{mac}' found. Don't know which one to choose"); * } */ private static async Task <bool> PairWithName(string name, int discoveryTime, Utils.DeviceType deviceType, string pin) { var devices = DeviceFinder.FindDevicesByName(DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTime), name, deviceType); if (devices.Count == 1) { return(await DevicePairer.PairDevice(devices[0], pin)); } if (devices.Count == 2 && devices[0].Type == Bluetooth.DeviceType.BluetoothLE && devices[1].Type == Bluetooth.DeviceType.BluetoothLE) { return(await HandleSituation_2_BluetoothLe_devices_with_the_same_name_found(devices[0], devices[1], pin)); } throw new Exception($"{devices.Count} devices with the name '{name}' found. Don't know which one to choose"); }
/* * public static void Execute(PairDeviceOptions opts) * { * if (!string.IsNullOrEmpty(opts.Mac)) * { * PairWithMac(new MacAddress(opts.Mac), opts.DiscoveryTime, opts.Type, opts.Pin); * } * else if (!string.IsNullOrEmpty(opts.DeviceName)) * { * PairWithName(opts.DeviceName, opts.DiscoveryTime, opts.Type, opts.Pin); * } * else * { * throw new Exception("Mac or device name must be specified"); * } * } */ private static async Task <bool> PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin) { String deviceId = $"Bluetooth#Bluetootha8:6d:aa:5f:35:51-{mac}"; IAsyncOperation <DeviceInformation> devInfoTask = DeviceInformation.CreateFromIdAsync(deviceId); Console.WriteLine($"Paring with Device deviceId-{deviceId} Step 1"); DeviceInformation devInfo = await devInfoTask; Console.WriteLine($"Paring with Device deviceId-{deviceId} Step 2"); Device device = new Device(devInfo); return(await DevicePairer.PairDevice(device, pin)); }
private static void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin) { var devices = DeviceFinder.FindDevicesByMac(DeviceDiscoverer.DiscoverBluetoothDevices(discoveryTime), mac, deviceType); if (devices.Count == 1) { DevicePairer.PairDevice(devices[0], pin); return; } if (devices.Count == 2) { throw new Exception( $"2 devices with the mac '{mac}' found \"{devices[0]}\" and \"{devices[1]}\". Don't know which one to choose.\n"); } throw new Exception( $"{devices.Count} devices with the mac '{mac}' found. Don't know which one to choose"); }
private static async Task <bool> HandleSituation_2_BluetoothLe_devices_with_the_same_name_found(Device device1, Device device2, string pin) { // BLuetooth LE devices use mac randomization, which can lead to the situation when // the user already have the device paired but with different mac address. // In this situation we need to unpair the old device and pair the current one. // If we don't unpair the old device first, the pairing procedure will fail. if (device1.IsPaired || device2.IsPaired) { var oldDevice = device1.IsPaired ? device1 : device2; var newDevice = !device1.IsPaired ? device1 : device2; Console.WriteLine($"2 devices with the same name found: \"{oldDevice}\" (paired) and \"{newDevice}\""); Console.WriteLine("Assume that the device changed its mac address"); DevicePairer.UnpairDevice(oldDevice); return(await DevicePairer.PairDevice(newDevice, pin)); } throw new Exception($"2 unpaired devices with the same name found \"{device1}\" \"{device2}\". Don't know which one to choose."); }
private static async void PairWithMac(MacAddress mac, int discoveryTime, Utils.DeviceType deviceType, string pin) { BluetoothAdapter defaultBtAdapter = await BluetoothAdapter.GetDefaultAsync(); string localBtAddress = string.Join(":", BitConverter.GetBytes(defaultBtAdapter.BluetoothAddress).Reverse().Select(b => b.ToString("X2"))).Substring(6).ToLower(); string deviceId = $"Bluetooth#Bluetooth{localBtAddress}-{mac}"; try { DeviceInformation devInfo = await DeviceInformation.CreateFromIdAsync(deviceId);; Console.WriteLine($"Paring with Device deviceId-{deviceId}"); Device device = new Device(devInfo); DevicePairer.PairDevice(device, "0000"); } catch (Exception ex) { Console.WriteLine($"Exception: {ex.StackTrace}"); } return; }