void FManager_OnDiscoveringCompleted(object Sender, wclBluetoothRadio Radio, int Error) { if (Error != wclErrors.WCL_E_SUCCESS) { Trace("Discovering completed with error: 0x" + Error.ToString("X8")); CloseBluetoothManager(); } else { if (FDevices.Count == 0) { Trace("No one BLE device was found"); CloseBluetoothManager(); } else { Trace("Discovering completed with success"); Trace("Use firs found device to connect: " + FDevices[0].ToString("X12")); FClient.Address = FDevices[0]; Int32 Res = FClient.Connect(Radio); if (Res != wclErrors.WCL_E_SUCCESS) { Trace("Connect attemp failed; 0x" + Res.ToString("X8")); CloseBluetoothManager(); } } } FDevices = null; }
/// <summary> Starts connection to the WeDo Hubs. </summary> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> /// <remarks> The method starts searching for WeDo Hubs and to connect to each found. Once the Hub found /// the <c>OnHubFound</c> event fires. An application may accept connection to this Hub by setting /// the <c>Connect</c> parameter to <c>true</c>. </remarks> public Int32 Start() { if (FRadio != null) { return(wclConnectionErrors.WCL_E_CONNECTION_ACTIVE); } Int32 Res = FManager.Open(); if (Res == wclErrors.WCL_E_SUCCESS) { Res = FManager.GetLeRadio(out FRadio); if (Res == wclErrors.WCL_E_SUCCESS) { // Try to start watching for HUBs. Res = FWatcher.Start(FRadio); // If something went wrong we must clear the working radio objecy. if (Res != wclErrors.WCL_E_SUCCESS) { FRadio = null; } } // If something went wrong we must close Bluetooth Manager if (Res != wclErrors.WCL_E_SUCCESS) { FManager.Close(); } } return(Res); }
static public void SearchDevice() { if (Manager == null) { Manager = new wclBluetoothManager(); //Manager.OnNumericComparison += new wclBluetoothNumericComparisonEvent(Manager_OnNumericComparison); //Manager.OnPasskeyNotification += new wclBluetoothPasskeyNotificationEvent(Manager_OnPasskeyNotification); //Manager.OnPasskeyRequest += new wclBluetoothPasskeyRequestEvent(Manager_OnPasskeyRequest); //Manager.OnPinRequest += new wclBluetoothPinRequestEvent(Manager_OnPinRequest); Manager.OnDeviceFound += new wclBluetoothDeviceEvent(Manager_OnDeviceFound); Manager.OnDiscoveringCompleted += new wclBluetoothResultEvent(Manager_OnDiscoveringCompleted); //Manager.OnDiscoveringStarted += new wclBluetoothEvent(Manager_OnDiscoveringStarted); Manager.Open(); Radio = GetRadio(); } if (Radio != null) { Int32 Res = Radio.Discover(3, wclBluetoothDiscoverKind.dkBle); if (Res != wclErrors.WCL_E_SUCCESS) { CSLibrary.Debug.WriteLine("Error starting discovering: 0x" + Res.ToString("X8")); } } }
public fmDevInfo(wclBluetoothRadio Radio, Int64 Address) : base() { FRadio = Radio; FAddress = Address; InitializeComponent(); }
private void Manager_OnDeviceFound(object sender, wclBluetoothRadio radio, long address) { var devType = wclBluetoothDeviceType.dtMixed; var res = radio.GetRemoteDeviceType(address, out devType); _devices.Add(address.ToString("X12"), radio); TraceEvent(address, "Device found", "", ""); }
void Manager_OnDiscoveringCompleted(object Sender, wclBluetoothRadio Radio, int Error) { //Console.WriteLine("Discovering completed"); if (scanning) { // Restart scan as RMIR allows scanning for 15 minutes int Res = Radio.Discover(120, wclBluetoothDiscoverKind.dkBle); scanning = Res == wclErrors.WCL_E_SUCCESS ? true : false; } }
/// <summary> Starts connection to the WeDo Hubs. </summary> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> /// <remarks> The method starts searching for WeDo Hubs and to connect to each found. Once the Hub found /// the <c>OnHubFound</c> event fires. An application may accept connection to this Hub by setting /// the <c>Connect</c> parameter to <c>true</c>. </remarks> public Int32 Start() { if (FRadio != null) { return(wclConnectionErrors.WCL_E_CONNECTION_ACTIVE); } Int32 Res = FManager.Open(); if (Res == wclErrors.WCL_E_SUCCESS) { if (FManager.Count == 0) { Res = wclBluetoothErrors.WCL_E_BLUETOOTH_API_NOT_FOUND; } else { // Look for first available radio. for (int i = 0; i < FManager.Count; i++) { if (FManager[i].Available) { FRadio = FManager[i]; break; } } if (FRadio == null) { Res = wclBluetoothErrors.WCL_E_BLUETOOTH_RADIO_UNAVAILABLE; } else { // Try to start watching for HUBs. Res = FWatcher.Start(FRadio); // If something went wrong we must clear the working radio objecy. if (Res != wclErrors.WCL_E_SUCCESS) { FRadio = null; } } } // If something went wrong we must close Bluetooth Manager if (Res != wclErrors.WCL_E_SUCCESS) { FManager.Close(); } } return(Res); }
public string ConnectBLE(string portName) { Manager = new wclBluetoothManager(); Manager.OnDeviceFound += new wclBluetoothDeviceEvent(Manager_OnDeviceFound); Manager.OnDiscoveringCompleted += new wclBluetoothResultEvent(Manager_OnDiscoveringCompleted); Manager.OnDiscoveringStarted += new wclBluetoothEvent(Manager_OnDiscoveringStarted); Client = new wclGattClient(); Client.OnConnect += Client_OnConnect; Client.OnDisconnect += Client_OnDisconnect; Manager.Open(); Radio = GetRadio(); return(Radio != null ? portName : null); }
private void BtConnect_Click(Object Sender, EventArgs e) { // The very first thing we have to do is to open Bluetooth Manager. // That initializes the underlying drivers and allows us to work with Bluetooth. // Always check result! Int32 Res = FManager.Open(); if (Res != wclErrors.WCL_E_SUCCESS) { // It should never happen but if it does notify user. MessageBox.Show("Unable to open Bluetooth Manager: 0x" + Res.ToString("X8")); } else { // Assume that no one Bluetooth Radio available. wclBluetoothRadio Radio = null; Res = FManager.GetLeRadio(out Radio); if (Res != wclErrors.WCL_E_SUCCESS) { // If not, let user know that he has no Bluetooth. MessageBox.Show("No available Bluetooth Radio found"); } else { // If found, try to start discovering. Res = FWatcher.Start(Radio); if (Res != wclErrors.WCL_E_SUCCESS) { // It is something wrong with discovering starting. Notify user about the error. MessageBox.Show("Unable to start discovering: 0x" + Res.ToString("X8")); } else { btConnect.Enabled = false; btDisconnect.Enabled = true; laStatus.Text = "Searching..."; } } // Again, check the found Radio. if (Res != wclErrors.WCL_E_SUCCESS) { // And if it is null (not found or discovering was not started // close the Bluetooth Manager to release all the allocated resources. FManager.Close(); // Also clean up found Radio variable so we can check it later. Radio = null; } } }
static void Manager_OnDeviceFound(object Sender, wclBluetoothRadio Radio, long Address) { wclBluetoothDeviceType DevType = wclBluetoothDeviceType.dtMixed; Int32 Res = Radio.GetRemoteDeviceType(Address, out DevType); if (Res != wclErrors.WCL_E_SUCCESS) { CSLibrary.Debug.WriteLine("Error: 0x" + Res.ToString("X8")); } else { switch (DevType) { case wclBluetoothDeviceType.dtBle: { CSLibrary.DeviceFinder.DeviceInfomation di = new CSLibrary.DeviceFinder.DeviceInfomation(); string DevName; sbyte RSSI; Res = Radio.GetRemoteRssi(Address, out RSSI); if (Res != wclErrors.WCL_E_SUCCESS) { RSSI = 0; } Res = Radio.GetRemoteName(Address, out DevName); if (Res != wclErrors.WCL_E_SUCCESS) { di.deviceName = DevName = "Error: 0x" + Res.ToString("X8"); } else { di.deviceName = DevName; } di.ID = (uint)_deviceDB.Count; di.macAdd = Address; di.nativeDeviceInformation = null; _deviceDB.Add(Address); RaiseEvent <DeviceFinderArgs>(OnSearchCompleted, new DeviceFinderArgs(di)); } break; } } }
/// <summary> Starts watching (discovering) for WeDo devices. </summary> /// <param name="Radio"> The <see cref="wclBluetoothRadio" /> object that should be used /// for executing Bluetooth LE discovering. </param> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> /// <seealso cref="wclBluetoothRadio" /> public Int32 Start(wclBluetoothRadio Radio) { // First, try to open message receiver. Int32 Res = FReceiver.Open(); if (Res == wclErrors.WCL_E_SUCCESS) { // Now try to start watcher. Res = FWatcher.Start(Radio); // If failed we must close message receiver. if (Res != wclErrors.WCL_E_SUCCESS) { FReceiver.Close(); } } return(Res); }
/// <summary> Stops the WeDo Robot. </summary> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> public Int32 Stop() { if (FRadio == null) { return(wclConnectionErrors.WCL_E_CONNECTION_NOT_ACTIVE); } FWatcher.Stop(); while (FHubs.Count > 0) { FHubs.Values.ElementAt(0).Disconnect(); } FManager.Close(); FRadio = null; return(wclErrors.WCL_E_SUCCESS); }
/// <summary> Connects to a selected WeDo Hub. </summary> /// <param name="Radio"> The <see cref="wclBluetoothRadio" /> object that should be used /// for executing Bluetooth LE connection. </param> /// <param name="Address"> The WeDo Hub MAC address. </param> /// <returns> If the method completed with success the returning value is /// <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is /// one of the Bluetooth Framework error code. </returns> /// <seealso cref="wclBluetoothRadio" /> public Int32 Connect(wclBluetoothRadio Radio, Int64 Address) { if (Radio == null || Address == 0) { return(wclErrors.WCL_E_INVALID_ARGUMENT); } // This check prevent exception raising when you try to change // connection MAC address on already connected client. if (FClient.State != wclClientState.csDisconnected) { return(wclConnectionErrors.WCL_E_CONNECTION_ACTIVE); } FClient.Address = Address; return(FClient.Connect(Radio)); }
private void FWatcher_OnHubFound(Object Sender, Int64 Address, String Name) { wclBluetoothRadio Radio = FWatcher.Radio; FWatcher.Stop(); Int32 Res = FHub.Connect(Radio, Address); if (Res != wclErrors.WCL_E_SUCCESS) { MessageBox.Show("Connect failed: 0x" + Res.ToString("X8")); EnableConnect(false); } else { laStatus.Text = "Connecting"; } }
/// <summary> Creates new instance of the WeDo Robot class. </summary> public wclWeDoRobot() { FHubs = new Dictionary <Int64, wclWeDoHub>(); FManager = new wclBluetoothManager(); FRadio = null; FWatcher = new wclWeDoWatcher(); FWatcher.OnHubFound += WatcherHubFound; FWatcher.OnStarted += WatcherStarted; FWatcher.OnStopped += WatcherStopped; OnHubConnected = null; OnHubDisconnected = null; OnHubFound = null; OnStarted = null; OnStopped = null; }
void FManager_AfterOpen(object sender, EventArgs e) { btStart.Enabled = false; Trace("Bluetooth Manager has been opened"); Trace("Looking for working radio"); if (FManager.Count == 0) { Trace("No Bluetooth hardware installed"); CloseBluetoothManager(); } else { wclBluetoothRadio Radio = null; for (Int32 i = 0; i < FManager.Count; i++) { if (FManager[i].Available) { Radio = FManager[i]; break; } } if (Radio == null) { Trace("No available Bluetooth Radio was found"); CloseBluetoothManager(); } else { Trace("Start discovering for BLE devices on radio " + Radio.ApiName); Int32 Res = Radio.Discover(10, wclBluetoothDiscoverKind.dkBle); if (Res != wclErrors.WCL_E_SUCCESS) { Trace("Start discovering failed: 0x" + Res.ToString("X8")); CloseBluetoothManager(); } } } }
private void Manager_OnDiscoveringCompleted(object sender, wclBluetoothRadio radio, int error) { if (_devices.Count == 0) { MessageBox.Show("No BLE devices were found.", "Discovering for BLE devices", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { // Here we can update found devices names. foreach (var key in _devices.Keys) { var address = Convert.ToInt64(key, 16); var item = lvDevices.Items.Add(address.ToString("X12")); string devName; var res = radio.GetRemoteName(address, out devName); if (devName.Contains("SCH")) { if (res != wclErrors.WCL_E_SUCCESS) { item.SubItems.Add("Error: 0x" + res.ToString("X8")); } else { item.SubItems.Add(devName); } } else { lvDevices.Items.Remove(item); } } } TraceEvent(0, "Discovering completed", "", ""); }
void Manager_OnDeviceFound(object Sender, wclBluetoothRadio Radio, long Address) { int Res = Radio.GetRemoteName(Address, out string NameOut); string Name = Res == wclErrors.WCL_E_SUCCESS ? NameOut : "N/A"; Res = Radio.GetRemoteRssi(Address, out sbyte RssiOut); int Rssi = Res == wclErrors.WCL_E_SUCCESS ? RssiOut : 1; string addrRaw = Address.ToString("x12"); string addrstr = addrRaw.Substring(0, 2); for (int i = 2; i < 12; i += 2) { addrstr += ":" + addrRaw.Substring(i, 2); } if (addrstr.StartsWith("48:d0:cf") && !addressList.Contains(addrstr)) { //Console.WriteLine("Found " + addrstr + " name " + Name); addressList.Add(addrstr); nameList.Add(Name); rssiList.Add(Rssi); } }
/** * <summary>When the Simple_Form loads</summary> * <param name="sender">Object on which the change happened</param> * <param name="e">Additional information about the event</param> */ private void Simple_Form_Load(object sender, EventArgs e) { lineTracker = new LineTracker(this); Manager = new wclBluetoothManager(); Watcher = new wclWeDoWatcher(); listBox1.Items.Add("Please connect one of the hubs."); Watcher.OnHubFound += Watcher_OnHubFound; int res = Manager.Open(); if (res != wclErrors.WCL_E_SUCCESS) { MessageBox.Show("Error opening the Manager."); } else { wclBluetoothRadio radio = null; for (int i = 0; i < Manager.Count; i++) { if (Manager[i].Available) { radio = Manager[i]; break; } } if (radio != null) { res = Watcher.Start(radio); if (res != wclErrors.WCL_E_SUCCESS) { MessageBox.Show("Can't start watching."); } } } }
/// <summary> /// 3,对外接口:连接BLE设备 /// </summary> /// <param name="MacAddress"></param> /// <returns></returns> public string ConnectDevice(string MacAddress) { RecordLog.GetInstance().WriteLog(Level.Info, string.Format("开始配对蓝牙地址,Mac:{0}", MacAddress)); LicenseHelper licenseHelper = new LicenseHelper(); if (!licenseHelper.IsHaveListenersFile()) { MessageBox.Show("授权文件不存在,请联系系统管理员", "系统提示"); return("Error"); } List <string> CheckData = licenseHelper.ReturnCheckData(); if (CheckData.Count == 0) { MessageBox.Show("授权文件信息不存在,请联系系统管理员", "系统提示"); return("Error"); } string name = string.Empty; string mac = string.Empty; string outCheckValue = string.Empty; //连接地址的时候判断授权文件 foreach (var s in EventClass.GetInstance()._dicdeviceAddress) { if (s.Value.ToString().Equals(MacAddress)) { bool res = licenseHelper.EncrptyData(s.Key.ToString(), s.Value.ToString(), out outCheckValue); if (res == true) { bool res2 = licenseHelper.CheckLockDecValue(outCheckValue, CheckData); if (res2 == true) { RecordLog.GetInstance().WriteLog(Level.Info, "开始连接设备: " + MacAddress.ToString()); EventClass.GetInstance()._ConnectBleAddress = MacAddress; //EventClass.GetInstance()._eventDelegate.OnHandlerConnect(); //return "Success"; string msg = string.Empty; List <lockDetail> objlist = RedvelopRecord.GetInstance().GetlockSetting(out msg); if (objlist == null) { MessageBox.Show("系统尚未配置门锁文件", "系统提示"); return("Error"); } foreach (lockDetail lockDetail in objlist) { if (lockDetail._id.Equals(EventClass.GetInstance()._ConnectBleAddress)) { OpenAppDo.GetInstance().ModifyLockTime(lockDetail._modifyName, lockDetail._id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //OpenAppDo.GetInstance().AddLockMap(lockDetail._modifyName, lockDetail._id, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } } EventClass.GetInstance()._client.Address = Convert.ToInt64(MacAddress, 16); wclBluetoothRadio radio = EventClass.GetInstance()._devices[MacAddress]; if (EventClass.GetInstance()._client.Connect(radio) != 0) { RecordLog.GetInstance().WriteLog(Level.Info, "设备连接失败"); MessageBox.Show("设备连接失败", "提示"); return("Error"); } RecordLog.GetInstance().WriteLog(Level.Info, "设备连接成功"); EventClass.GetInstance()._dicdeviceAddress.Clear(); return("Success"); } else { MessageBox.Show(string.Format("{0}门锁未授权,请联系系统管理员", s.Key.ToString()), "系统提示"); return("Error"); } } else { MessageBox.Show(string.Format("处理授权文件失败,请联系系统管理员"), "系统提示"); return("Error"); } } } MessageBox.Show("未找到适配的蓝牙设备", "系统提示"); return("Error"); }
private void FManager_AfterOpen(object sender, EventArgs e) { lbLog.Items.Add("Bluetooth Manager has been opened"); if (FManager.Count == 0) { lbLog.Items.Add("No Bluetooth radio found"); FManager.Close(); } else { wclBluetoothRadio Radio = FManager[0]; lbLog.Items.Add("Found " + Radio.ApiName + " Bluetooth radio"); Int64[] Devices; Int32 Res = Radio.EnumPairedDevices(out Devices); if (Res != wclErrors.WCL_E_SUCCESS) { lbLog.Items.Add("Enumerate paired devices failed: 0x" + Res.ToString("X8")); } else { if (Devices == null || Devices.Length == 0) { lbLog.Items.Add("Not paired devices found"); FManager.Close(); } else { Int64 Address = 0; foreach (Int64 a in Devices) { wclBluetoothDeviceType Type; Res = Radio.GetRemoteDeviceType(a, out Type); if (Res == wclErrors.WCL_E_SUCCESS && Type == wclBluetoothDeviceType.dtBle) { Address = a; break; } } if (Address == 0) { lbLog.Items.Add("No BLE device found"); FManager.Close(); } else { FClient.Address = Address; Res = FClient.Connect(Radio); if (Res != wclErrors.WCL_E_SUCCESS) { lbLog.Items.Add("Connect failed; 0x" + Res.ToString("X8")); } } } } if (Res != wclErrors.WCL_E_SUCCESS) { FManager.Close(); } } }
private void Manager_OnDiscoveringStarted(object sender, wclBluetoothRadio radio) { lvDevices.Items.Clear(); TraceEvent(0, "Discovering started", "", ""); }
void FManager_OnDeviceFound(object Sender, wclBluetoothRadio Radio, long Address) { Trace("Device found: " + Address.ToString("X12")); FDevices.Add(Address); }
void Manager_OnDiscoveringStarted(object Sender, wclBluetoothRadio Radio) { //Console.WriteLine("Discovering started"); }
void FManager_OnDiscoveringStarted(object Sender, wclBluetoothRadio Radio) { Trace("Discovering has been started"); FDevices = new List <Int64>(); }
static void Manager_OnDiscoveringCompleted(object Sender, wclBluetoothRadio Radio, int Error) { System.Console.WriteLine("Discovering completed"); }
private void BtStart_Click(Object Sender, EventArgs e) { // The very first thing we have to do is to open Bluetooth Manager. // That initializes the underlying drivers and allows us to work with Bluetooth. // Always check result! Int32 Res = FManager.Open(); if (Res != wclErrors.WCL_E_SUCCESS) { // It should never happen but if it does notify user. MessageBox.Show("Unable to open Bluetooth Manager: 0x" + Res.ToString("X8")); } else { // Assume that no one Bluetooth Radio available. wclBluetoothRadio Radio = null; // Check that at least one Bluetooth Radio exists (or at least Bluetooth drivers installed). if (FManager.Count == 0) { // No one, even drivers? MessageBox.Show("No Bluetooth Hardware installed"); } else { // Ok, at least one Bluetooth Radio module should be available. for (Int32 i = 0; i < FManager.Count; i++) { // Check if current Radio module is available (plugged in and turned ON). if (FManager[i].Available) { // Looks like we have Bluetooth on this PC! Radio = FManager[i]; // Terminate the loop. break; } } // Check that we found the Bluetooth Radio module. if (Radio == null) { // If not, let user know that he has no Bluetooth. MessageBox.Show("No available Bluetooth Radio found"); } else { // If found, try to start discovering. Res = FWatcher.Start(Radio); if (Res != wclErrors.WCL_E_SUCCESS) { // It is something wrong with discovering starting. Notify user about the error. MessageBox.Show("Unable to start discovering: 0x" + Res.ToString("X8")); // Also clean up found Radio variable so we can check it later. Radio = null; } } } // Again, check the found Radio. if (Radio == null) { // And if it is null (not found or discovering was not started // close the Bluetooth Manager to release all the allocated resources. FManager.Close(); } } }