예제 #1
0
        public async Task <bool> ConnectToBadge()
        {
            //using (var loader = UserDialogs.Instance.Loading("Connecting to badge...", null, "Cancel", true, MaskType.Black))
            //{
            try
            {
                UserDialogs.Instance.Toast("Connecting to Badge.. Please wait", new TimeSpan(1000));
                //loader.Title = "Connecting...";
                Connect();

                //await Task.Delay(2000);
                //loader.Title = "Pairing...";
                //var status = await PairBLE();


                //loader.Title = "MTU Changing...";
                var actualMTU = await Badge.Instance.RequestMTU();

                //await Task.Delay(5000);
                //UserDialogs.Instance.Toast("Waiting for the service...", new TimeSpan(2000));
                var walletSetting   = App.Database.BadgeDAO.GetItemsAsync().Result.Last();
                var walletChr       = walletSetting.Characteristic;
                var chrOffsetString = walletSetting.ServiceUUID.Substring(8);
                var charrr          = Enumerable.Range(0, walletChr.Length / 8).Select(i => new Guid(walletChr.Substring(i * 8, 8) + chrOffsetString)).ToArray();
                BleDevice.WhenAnyCharacteristicDiscovered().Subscribe(async chs => {
                    if (chs.Uuid == charrr[0])
                    {
                        CharacteristicTransaction = chs;
                    }
                    if (chs.Uuid == charrr[1])
                    {
                        CharacteristicTXN = chs; CharacteristicTXN.EnableNotifications();
                    }
                    if (chs.Uuid == charrr[2])
                    {
                        CharacteristicAddERC20 = chs;
                    }
                    if (chs.Uuid == charrr[3])
                    {
                        CharacteristicUpdateBalance = chs;
                    }
                    if (chs.Uuid == charrr[4])
                    {
                        CharacteristicGeneralCmd = chs;
                    }
                    if (chs.Uuid == charrr[5])
                    {
                        CharacteristicGeneralData = chs;
                    }
                    if (CharacteristicTransaction != null)
                    {
                        UserDialogs.Instance.Toast("Badge connected...", new TimeSpan(1000));
                    }
                });

                //GetCharacteristics();
                //var walletSetting = App.Database.BadgeDAO.GetItemsAsync().Result.Last();
                //var walletChr = walletSetting.Characteristic;
                //var chrOffsetString = walletSetting.ServiceUUID.Substring(8);
                //var charrr = Enumerable.Range(0, walletChr.Length / 8).Select(i => new Guid(walletChr.Substring(i * 8, 8) + chrOffsetString)).ToArray();

                //foreach (var item in charrr)
                //{
                //    var abc = await BleDevice.GetKnownService(new Guid(walletSetting.ServiceUUID));
                //    var chs = await abc.GetKnownCharacteristics(item);
                //    //var chs =  await BleDevice.GetKnownCharacteristics(new Guid(walletSetting.ServiceUUID), item);
                //    if (chs.Uuid == charrr[0]) CharacteristicTransaction = chs;
                //    if (chs.Uuid == charrr[1]) { CharacteristicTXN = chs; CharacteristicTXN.EnableNotifications(); }
                //    if (chs.Uuid == charrr[2]) CharacteristicAddERC20 = chs;
                //    if (chs.Uuid == charrr[3]) CharacteristicUpdateBalance = chs;
                //    if (chs.Uuid == charrr[4]) CharacteristicGeneralCmd = chs;
                //    if (chs.Uuid == charrr[5]) CharacteristicGeneralData = chs;
                //}
                //await Task.Delay(10000);
                //loader.Title = "Function Binding...";
                //var charactertiscs = await Badge.Instance.GetGattCharacteristics();
                //CharacteristicTransaction = charactertiscs[Badge.BadgeFunction.Transaction];
                //CharacteristicTXN = charactertiscs[Badge.BadgeFunction.TXN];
                //CharacteristicUpdateBalance = charactertiscs[Badge.BadgeFunction.Balance];
                //loader.Title = "Notifying...";
                //await Badge.Instance.ToggleNotify(CharacteristicTXN);
                //var badgeSetting = App.Database.BadgeDAO.GetItemsAsync().Result.Last();
                //var balance = await EthService.Instance.GetTokenBalance(badgeSetting.Address, EthService.Instance.HitConCoinContract);
                //var discard = 0;
                //await Badge.Instance.DoWrite(CharacteristicUpdateBalance, true, HexEncoding.GetBytes(EthService.Instance.HitConCoinContract + String.Format("0{0:X}", balance), out discard));
            }
            catch (Exception ex)
            {
                UserDialogs.Instance.Alert(ex.ToString());
            }
            //}


            return(true);
        }
예제 #2
0
        //public async Task<Dictionary<BadgeFunction, IGattCharacteristic>> GetCharacteristics()
        public void GetCharacteristics()
        {
            var walletSetting   = App.Database.BadgeDAO.GetItemsAsync().Result.Last();
            var walletChr       = walletSetting.Characteristic;
            var chrOffsetString = walletSetting.ServiceUUID.Substring(8);
            var charrr          = Enumerable.Range(0, walletChr.Length / 8).Select(i => new Guid(walletChr.Substring(i * 8, 8) + chrOffsetString)).ToArray();

            BleDevice.WhenAnyCharacteristicDiscovered().Subscribe(async chs => {
                if (chs.Uuid == charrr[0])
                {
                    CharacteristicTransaction = chs;
                }
                if (chs.Uuid == charrr[1])
                {
                    CharacteristicTXN = chs; CharacteristicTXN.EnableNotifications();
                }
                if (chs.Uuid == charrr[2])
                {
                    CharacteristicAddERC20 = chs;
                }
                if (chs.Uuid == charrr[3])
                {
                    CharacteristicUpdateBalance = chs;
                }
                if (chs.Uuid == charrr[4])
                {
                    CharacteristicGeneralCmd = chs;
                }
                if (chs.Uuid == charrr[5])
                {
                    CharacteristicGeneralData = chs;
                }
                if (CharacteristicTransaction != null)
                {
                    UserDialogs.Instance.Toast("Badge connected...", new TimeSpan(1000));
                }
            });
            //var allCharactertistics = BleDevice.GetKnownCharacteristics(new Guid(walletSetting.ServiceUUID), charrr)
            //    .ObserveOn(RxApp.MainThreadScheduler)
            //    .Subscribe(
            //   chs =>
            //   {
            //        //if (chs.Uuid == charrr[0] && !BadgeFunctionDicts.ContainsKey(BadgeFunction.Transaction)) BadgeFunctionDicts.Add(BadgeFunction.Transaction, chs);
            //        //if (chs.Uuid == charrr[1] && !BadgeFunctionDicts.ContainsKey(BadgeFunction.TXN)) BadgeFunctionDicts.Add(BadgeFunction.TXN, chs);
            //        //if (chs.Uuid == charrr[2] && !BadgeFunctionDicts.ContainsKey(BadgeFunction.AddERC20)) BadgeFunctionDicts.Add(BadgeFunction.AddERC20, chs);
            //        //if (chs.Uuid == charrr[3] && !BadgeFunctionDicts.ContainsKey(BadgeFunction.Balance)) BadgeFunctionDicts.Add(BadgeFunction.Balance, chs);
            //        //if (chs.Uuid == charrr[4] && !BadgeFunctionDicts.ContainsKey(BadgeFunction.General_Purpose_Cmd)) BadgeFunctionDicts.Add(BadgeFunction.General_Purpose_Cmd, chs);
            //        //if (chs.Uuid == charrr[5] && !BadgeFunctionDicts.ContainsKey(BadgeFunction.General_Purpose_Data)) BadgeFunctionDicts.Add(BadgeFunction.General_Purpose_Data, chs);
            //        if (chs.Uuid == charrr[0]) CharacteristicTransaction = chs;
            //       if (chs.Uuid == charrr[1]) { CharacteristicTXN = chs;  CharacteristicTXN.EnableNotifications(); }
            //       if (chs.Uuid == charrr[2]) CharacteristicAddERC20 = chs;
            //       if (chs.Uuid == charrr[3]) CharacteristicUpdateBalance = chs;
            //       if (chs.Uuid == charrr[4]) CharacteristicGeneralCmd = chs;
            //       if (chs.Uuid == charrr[5]) CharacteristicGeneralData = chs;
            //       if (CharacteristicTransaction != null) UserDialogs.Instance.Toast("Badge connected...", new TimeSpan(1000));
            //   });
            //await Task.Delay(10000);
            //CharacteristicTransaction = BadgeFunctionDicts[BadgeFunction.Transaction];
            //CharacteristicTXN = BadgeFunctionDicts[BadgeFunction.TXN];
            //CharacteristicAddERC20 = BadgeFunctionDicts[BadgeFunction.AddERC20];
            //CharacteristicUpdateBalance = BadgeFunctionDicts[BadgeFunction.Balance];
        }