Esempio n. 1
0
        public bool DiscoverServices()
        {
            Services = null;
            bool UEIServiceFound = false;

            int Res = Client.ReadServices(wclGattOperationFlag.goReadFromDevice, out Services);

            if (Res != wclErrors.WCL_E_SUCCESS || Services == null)
            {
                return(false);
            }

            stage = 3;
            foreach (wclGattService Service in Services)
            {
                String s;
                if (Service.Uuid.IsShortUuid)
                {
                    s = Service.Uuid.ShortUuid.ToString("X4");
                }
                else
                {
                    s = Service.Uuid.LongUuid.ToString();
                }
                if (s.Equals("FFE0"))
                {
                    UEIservice      = Service;
                    UEIServiceFound = true;
                }
            }
            return(UEIServiceFound);
        }
Esempio n. 2
0
        private void FClient_OnConnect(object Sender, int Error)
        {
            if (Error != wclErrors.WCL_E_SUCCESS)
            {
                lbLog.Items.Add("Connect failed: 0x" + Error.ToString("X8"));
                FManager.Close();
            }
            else
            {
                wclGattService[] Services;
                Int32            Res = FClient.ReadServices(wclGattOperationFlag.goReadFromCache, out Services);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    lbLog.Items.Add("Read services failed: 0x" + Res.ToString("X8"));
                }
                else
                {
                    foreach (wclGattService s in Services)
                    {
                        lbLog.Items.Add(s.ToString());
                    }

                    FClient.Disconnect();
                }
                FManager.Close();
            }
        }
Esempio n. 3
0
        // GATT client connect event handler.
        private void ClientConnect(Object Sender, Int32 Error)
        {
            // If connection was not established simple fire the event with error code.
            if (Error != wclErrors.WCL_E_SUCCESS)
            {
                DoConnected(Error);
            }
            else
            {
                FHubConnected = true;

                // Read services. We do it only once to save connection time.
                wclGattService[] Services;
                Int32            Res = FClient.ReadServices(wclGattOperationFlag.goNone, out Services);
                if (Res == wclErrors.WCL_E_SUCCESS)
                {
                    if (Services == null)
                    {
                        Res = wclBluetoothErrors.WCL_E_BLUETOOTH_LE_ATTRIBUTE_NOT_FOUND;
                    }
                    else
                    {
                        // Try to connect to WeDo services.
                        Res = FDeviceInformation.Connect(Services);
                        if (Res == wclErrors.WCL_E_SUCCESS)
                        {
                            Res = FBatteryLevel.Connect(Services);
                        }
                        if (Res == wclErrors.WCL_E_SUCCESS)
                        {
                            Res = FIo.Connect(Services);
                        }
                        if (Res == wclErrors.WCL_E_SUCCESS)
                        {
                            Res = FHub.Connect(Services);
                        }
                        if (Res == wclErrors.WCL_E_SUCCESS)
                        {
                            // It does not matter if the function completed with or without success!
                            FHub.ReadBatteryType(out FBatteryType);
                        }

                        Services = null;
                    }
                }

                // If all the operations were success (services found, characteristics are found too)
                // we have to set connection flag.
                if (Res == wclErrors.WCL_E_SUCCESS)
                {
                    FConnected = true;
                }
                else
                {
                    // Otherwise we have to disconnect!
                    FClient.Disconnect();
                }

                // Now we can fire the OnConnected event.
                DoConnected(Res);
            }
        }
Esempio n. 4
0
        void Client_OnConnect(object Sender, int Error)
        {
            CSLibrary.Debug.WriteLine("Connected");


            int Res;

            // Get Services
            FServices = null;
            Res       = Client.ReadServices(wclGattOperationFlag.goNone, out FServices);
            if (Res != wclErrors.WCL_E_SUCCESS)
            {
                CSLibrary.Debug.WriteLine("ReadServices Error: 0x" + Res.ToString("X8"));
                return;
            }

            if (FServices == null)
            {
                return;
            }

            //CS108Service = null;
            foreach (wclGattService Service in FServices)
            {
                if (Service.Uuid.IsShortUuid)
                {
                    if (Service.Uuid.ShortUuid == 0x9800)
                    {
                        CS108Service = Service;
                    }
                }
            }

            //if (CS108Service == null)
            //    return;

            // Get Chatacteristics

            Res = Client.ReadCharacteristics(CS108Service, wclGattOperationFlag.goReadFromDevice, out FCharacteristics);
            if (Res != wclErrors.WCL_E_SUCCESS)
            {
                CSLibrary.Debug.WriteLine("ReadCharacteristics Error: 0x" + Res.ToString("X8"));
                return;
            }

            if (FCharacteristics == null)
            {
                return;
            }

            foreach (wclGattCharacteristic Character in FCharacteristics)
            {
                String s;
                if (Character.Uuid.IsShortUuid)
                {
                    if (Character.Uuid.ShortUuid == 0x9900)
                    {
                        WriteCharacteristic = Character;
                    }
                    else if (Character.Uuid.ShortUuid == 0x9901)
                    {
                        UpdateCharacteristic = Character;
                    }
                }
            }


            Res = Client.Subscribe(UpdateCharacteristic);
            if (Res != wclErrors.WCL_E_SUCCESS)
            {
                CSLibrary.Debug.WriteLine("Subscribe Error: 0x" + Res.ToString("X8"));
            }

            Res = Client.WriteClientConfiguration(UpdateCharacteristic, true, wclGattOperationFlag.goNone); // for library 7.3.9.0 above
            if (Res != wclErrors.WCL_E_SUCCESS)
            {
                CSLibrary.Debug.WriteLine("WriteClientConfiguration Error: 0x" + Res.ToString("X8"));
            }

            _readerState = READERSTATE.IDLE;
            BTTimer      = new Timer(TimerFunc, this, 0, 1000);

            HardwareInit();
        }
Esempio n. 5
0
        void FClient_OnConnect(object Sender, int Error)
        {
            if (Error != wclErrors.WCL_E_SUCCESS)
            {
                Trace("Connection failed: 0x" + Error.ToString("X8"));
                CloseBluetoothManager();
            }
            else
            {
                FSubscribed = new List <wclGattCharacteristic>();

                Trace("Connected with success");

                Trace("Read services");
                wclGattService[] Services;
                Int32            Res = FClient.ReadServices(wclGattOperationFlag.goNone, out Services);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    Trace("Read services failed: 0x" + Res.ToString("X8"));
                }
                else
                {
                    foreach (wclGattService Service in Services)
                    {
                        Trace("Service " + ShowUuid(Service.Uuid));
                        wclGattCharacteristic[] Chars;
                        Res = FClient.ReadCharacteristics(Service, wclGattOperationFlag.goNone, out Chars);
                        if (Res != wclErrors.WCL_E_SUCCESS)
                        {
                            Trace("  Unable to read characteristics: 0x" + Res.ToString("X8"));
                        }
                        else
                        {
                            foreach (wclGattCharacteristic Char in Chars)
                            {
                                Trace("  Characteristic " + ShowUuid(Char.Uuid));
                                if (Char.IsReadable)
                                {
                                    Trace("    Readable");
                                }
                                if (Char.IsWritable || Char.IsSignedWritable || Char.IsWritableWithoutResponse)
                                {
                                    Trace("    Writable");
                                }
                                if (Char.IsIndicatable || Char.IsNotifiable)
                                {
                                    Trace("    Notifiable");
                                    Trace("    Subscribe");
                                    Res = FClient.Subscribe(Char);
                                    if (Res != wclErrors.WCL_E_SUCCESS)
                                    {
                                        Trace("      Subscribed failed: 0x" + Res.ToString("X8"));
                                    }
                                    else
                                    {
                                        Res = FClient.WriteClientConfiguration(Char, true, wclGattOperationFlag.goNone, wclGattProtectionLevel.plNone);
                                        if (Res != wclErrors.WCL_E_SUCCESS)
                                        {
                                            Trace("      Write CCD failed: 0x" + Res.ToString("X8"));
                                            FClient.Unsubscribe(Char);
                                        }
                                        else
                                        {
                                            Trace("      Subscribed");
                                            FSubscribed.Add(Char);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                btDisconnect.Enabled = true;
            }
        }
Esempio n. 6
0
        private void GetService()
        {
            _fServices = null;

            var resServices = _client.ReadServices(wclGattOperationFlag.goNone, out _fServices);

            if (resServices != wclErrors.WCL_E_SUCCESS)
            {
                MessageBox.Show("Error: 0x" + resServices.ToString("X8"), "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                TraceEvent(_client.Address, "Services", "FF00", "Failed");
                _client.Disconnect();
                return;
            }

            TraceEvent(_client.Address, "Services", "FF00", "Success");
            if (_fServices == null)
            {
                return;
            }

            foreach (var service in _fServices)
            {
                if (service.Uuid.IsShortUuid)
                {
                    if (_uuid.ShortUuid == service.Uuid.ShortUuid)
                    {
                        var resChar = _client.ReadCharacteristics(service, wclGattOperationFlag.goNone,
                                                                  out _fCharacteristics);
                        if (resChar != wclErrors.WCL_E_SUCCESS)
                        {
                            return;
                        }

                        foreach (var character in _fCharacteristics)
                        {
                            if (character.Uuid.ShortUuid == _rxUuid)
                            {
                                var resSubs = _client.Subscribe(character);
                                if (resSubs != wclErrors.WCL_E_SUCCESS)
                                {
                                    TraceEvent(_client.Address, "Subs", "RX", "Failed");
                                    return;
                                }
                                else
                                {
                                    TraceEvent(_client.Address, "Subs", "RX", "Success");
                                    var resReadDesc = _client.ReadDescriptors(character, wclGattOperationFlag.goNone,
                                                                              out _fDescriptors);
                                    if (resReadDesc != wclErrors.WCL_E_SUCCESS)
                                    {
                                        TraceEvent(_client.Address, "Subs", "RX", "Failed to get description");
                                        return;
                                    }
                                }
                            }
                            else if (character.Uuid.ShortUuid == _txUuid)
                            {
                                _txCharacteristic = character;
                            }
                        }
                    }
                }
            }
        }