private void GotButton(Bdaddr bdAddr) { var control = new FlicButtonControl(); control.lblBdAddr.Text = bdAddr.ToString(); control.btnListen.Click += (o, args) => { if (!control.Listens) { control.Listens = true; control.btnListen.Text = "Stop"; control.Channel = new ButtonConnectionChannel(bdAddr); control.Channel.CreateConnectionChannelResponse += (sender1, eventArgs) => Invoke((MethodInvoker) delegate { if (eventArgs.Error != CreateConnectionChannelError.NoError) { control.Listens = false; control.btnListen.Text = "Listen"; } else { control.lblStatus.Text = eventArgs.ConnectionStatus.ToString(); } }); control.Channel.Removed += (sender1, eventArgs) => Invoke((MethodInvoker) delegate { control.lblStatus.Text = "Disconnected"; control.Listens = false; control.btnListen.Text = "Listen"; }); control.Channel.ConnectionStatusChanged += (sender1, eventArgs) => Invoke((MethodInvoker) delegate { control.lblStatus.Text = eventArgs.ConnectionStatus.ToString(); }); control.Channel.ButtonUpOrDown += (sender1, eventArgs) => Invoke((MethodInvoker) delegate { control.pictureBox.BackColor = eventArgs.ClickType == ClickType.ButtonDown ? Color.LimeGreen : Color.Red; }); _flicClient.AddConnectionChannel(control.Channel); } else { _flicClient.RemoveConnectionChannel(control.Channel); } }; buttonsList.Controls.Add(control); }
private void GotButton(Bdaddr bdAddr) { DebugEx.TraceLog("Got Button"); var thing = flicThings.TryGetOrDefault(bdAddr); if (thing == null) { thing = ThingTools.FlicThing.CreateThing(bdAddr.ToString().Replace(":", ""), bdAddr.ToString()); thing = AddThing(thing); flicThings.Add(bdAddr, thing); } DebugEx.TraceLog("===========>Add Button Thing Completed"); var channel = ButtonChannels.TryGetOrDefault(bdAddr); if (channel == null) { DebugEx.TraceLog("===========>New Channel is created"); channel = new ButtonConnectionChannel(bdAddr); channel.CreateConnectionChannelResponse += (sender1, eventArgs) => { if (eventArgs.Error == CreateConnectionChannelError.NoError) { _channelConnected((ButtonConnectionChannel)sender1); } else { DebugEx.TraceError(((ButtonConnectionChannel)sender1).BdAddr.ToString() + " could not be connected"); } }; channel.Removed += (sender1, eventArgs) => { _channelDisconnected((ButtonConnectionChannel)sender1); }; channel.ConnectionStatusChanged += (sender1, eventArgs) => { var chan = (ButtonConnectionChannel)sender1; if (eventArgs.ConnectionStatus == ConnectionStatus.Disconnected) { _channelDisconnected(chan); } }; channel.ButtonSingleOrDoubleClickOrHold += (sender1, eventArgs) => { var chan = (ButtonConnectionChannel)sender1; var thisThing = flicThings.TryGetOrDefault(chan.BdAddr); if (thisThing == null) { return; } DebugEx.TraceLog(eventArgs.ClickType + " for " + thisThing.Name + " (key:" + thisThing.ThingKey + ")"); switch (eventArgs.ClickType) { case ClickType.ButtonSingleClick: SetPortState(PortKey.BuildFromArbitraryString(thisThing.ThingKey, ThingTools.FlicThing.SingleClick), "True"); break; case ClickType.ButtonDoubleClick: SetPortState(PortKey.BuildFromArbitraryString(thisThing.ThingKey, ThingTools.FlicThing.DoubleClick), "True"); break; case ClickType.ButtonHold: SetPortState(PortKey.BuildFromArbitraryString(thisThing.ThingKey, ThingTools.FlicThing.LongClick), "True"); break; default: break; } if (!ButtonChannels.ContainsKey(chan.BdAddr)) { _channelConnected(chan); } }; //try to add channel _flicClient.AddConnectionChannel(channel); DebugEx.TraceLog("===========>Add Connection Channel Completed"); foreach (var navctx in NavigationContext.Values) { DebugEx.TraceLog("===========>navctx.CurrentPage.Title: " + navctx.CurrentPage.Title); if (navctx.CurrentPage.Title == "Flic Pairing") { DebugEx.TraceLog("=====>here I am <======"); navctx.GoBack(); navctx.UpdateCurrentPage(createDiscoverPage()); } } } }