/// <summary> // Called when the user picks a devices to connect to. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDeviceSelectedChanged(object sender, SelectionChangedEventArgs e) { theSelectedDevice = _Devices.SelectedItem as SampleDevice; if (theSelectedDevice != null) { // Make a persistent BTLE connection if (theSelectedDevice.Connect()) { ShowFeedback("BTLE connection made"); _ConnectedDevice.Text = theSelectedDevice.Name; _OnConnectServicesBtn.IsEnabled = true; // Connection made so we are done } } }
/// <summary> /// Inputs the device name and ip. /// </summary> /// <param name="socket">Socket.</param> //端末名とIPアドレスのセットを受け取る public void inputDeviceNameAndIp(Socket socket)//*** Host And Guest ***// { try { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(socket.InputStream) ); int infoCounter = 0; String remoteDeviceInfo; //ホスト端末情報(端末名とIPアドレス)を保持するためのクラスオブジェクト //※このクラスは別途作成しているもの SampleDevice hostDevice = new SampleDevice(); while ((remoteDeviceInfo = bufferedReader.ReadLine()) != null && !remoteDeviceInfo.Equals("outputFinish")) { switch (infoCounter) { case 0: //1行目、端末名の格納 hostDevice.setDeviceName(remoteDeviceInfo); infoCounter++; break; case 1: //2行目、IPアドレスの取得 hostDevice.setDeviceIpAddress(remoteDeviceInfo); infoCounter++; return; default: return; } } } catch (IOException e) { e.PrintStackTrace(); } }
/// <summary> /// Called on a periodic timer to look for changes in the set of bluetooth devices detected. /// Two things to check: /// BluetoothLeDevicesAdded, new devices seen since last call /// BluetoothLeDevicesRemoved, the devices no longer seen /// </summary> public async void Update() { if (ble != null) { if (ble.DevicesChanged) { var newDeviceList = ble.BluetoothLeDevicesAdded; foreach (var theNewDevice in newDeviceList) { // First see if we already have it in the cache var item = theCachedDevices.SingleOrDefault(r => r.DeviceInfo.Id == theNewDevice.DeviceInfo.Id); if (item == null) { // new item // Create the wrapper for the BTLE object SampleDevice newSampleDevice = new SampleDevice(theNewDevice); // Add it to our cache of devices theCachedDevices.Add(newSampleDevice); bool addToList = false; ShowFeedback("BTLE Device added: " + theNewDevice.Name); string id = theNewDevice.DeviceInfo.Id; if (_Filter.Text.Length > 0) { // Filter defined so only take things that contain the filter name if (theNewDevice.Name.Contains(_Filter.Text, StringComparison.OrdinalIgnoreCase)) { ShowFeedback("Filtered BTLE Device found"); addToList = true; } } else { // No filter so just list everything found ShowFeedback("BTLE Device found: " + theNewDevice.Name); addToList = true; } if (addToList) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Add it to the list _Devices.Items.Add(newSampleDevice); _Devices.ScrollIntoView(newSampleDevice); }); } } else { ShowFeedback("BTLE Duplicate device seen: " + theNewDevice.Name); } } // For all the removed devices we want to remove them from the display list and cache var removedDeviceList = ble.BluetoothLeDevicesRemoved; foreach (var removed in removedDeviceList) { var itemToRemove = theCachedDevices.SingleOrDefault(r => r.DeviceInfo.Id == removed.DeviceInfo.Id); if (itemToRemove != null) { // Found it so remove it from the listbox and the cache. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Remove it from the list _Devices.Items.Remove(itemToRemove); }); // And the cache theCachedDevices.Remove(itemToRemove); ShowFeedback("removed: " + removed.Name); } //// Look through the cache for this device //foreach (var device in theSeenDevices) //{ // if (device.DeviceInfo.Id == removed.DeviceInfo.Id) // { // // Found it so remove it from the listbox and the cache. // await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => // { // // Remove it from the list // _Devices.Items.Remove(device); // }); // // And the cache // theSeenDevices.Remove(device); // ShowFeedback("removed: " + removed.Name); // // Done now // break; // } //} } } } }
/// A unity defined script method, called every tick to allow the script to perform some actions. public void Update() { // If we don't have a BTLE library connection yet we need to create one if (ble != null) { // If the devices changed flag has been set we need to process any additions / removals if (ble.DevicesChanged) { // Process the list of new devices. var newDeviceList = ble.BluetoothLeDevicesAdded; foreach (var theNewDevice in newDeviceList) { // First see if we already have it in the cache var item = theCachedDevices.SingleOrDefault(r => r.DeviceInfo.Id == theNewDevice.DeviceInfo.Id); if (item == null) { // new item // Create the wrapper for the BTLE object SampleDevice newSampleDevice = new SampleDevice(theNewDevice); // Add it to our cache of devices theCachedDevices.Add(newSampleDevice); ShowFeedback("BTLE Device added: " + theNewDevice.Name); string id = theNewDevice.DeviceInfo.Id; if (filter.Length > 0) { // Filter defined so only take things that contain the filter name if (theNewDevice.Name.Contains(filter)) { ShowFeedback("Filtered BTLE Device found"); } } else { // No filter so just list everything found ShowFeedback("BTLE Device found: " + theNewDevice.Name); } } else { ShowFeedback("BTLE Duplicate device seen: " + theNewDevice.Name); } } // For all the removed devices we want to remove them from the display list and cache var removedDeviceList = ble.BluetoothLeDevicesRemoved; foreach (var removed in removedDeviceList) { var itemToRemove = theCachedDevices.SingleOrDefault(r => r.DeviceInfo.Id == removed.DeviceInfo.Id); if (itemToRemove != null) { // Found it so remove it from the display list and the cache. theCachedDevices.Remove(itemToRemove); ShowFeedback("removed: " + removed.Name); } } } } while (feedbackMsgs.Count > 0) { _ShowFeedback(feedbackMsgs[0]); feedbackMsgs.RemoveAt(0); } }