Example #1
0
            public void senddexfile()
            {
                try
                {
                    BluetoothGattCharacteristic charac = ConnectedGatt.GetService(DEX_SERVICE_SPP)
                                                         .GetCharacteristic(DEX_CHARACTERISTIC_DATAWRITE);
                    string filepath = "DEX_SAMPLE.txt";
                    byte[] buffer   = new byte[20];

                    lock (ConnectedGatt)
                    {
                        try
                        {
                            Stream inputstream = asm.Open(filepath);
                            while (inputstream.Read(buffer, 0, buffer.Length) > 0)
                            {
                                charac.SetValue(buffer);
                                Java.Lang.Thread.Sleep(20);
                                bool status = ConnectedGatt.WriteCharacteristic(charac);
                                if (!status)
                                {
                                    MainActivity.RunOnUiThread(() =>
                                    {
                                        Log.Info(TAG + DexService.Operation, "send dexFile completed!");
                                    });
                                    break;
                                }

                                Arrays.Fill(buffer, 0);
                            }

                            inputstream.Close();
                        }
                        catch (Java.IO.IOException e)
                        {
                            e.PrintStackTrace();
                        }
                        catch (InterruptedException e)
                        {
                            e.PrintStackTrace();
                        }
                    }
                }
                catch (Java.Lang.Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
Example #2
0
            //public bool Waiting { get; set; }

            public void SendDexBytes(byte[] bytes, CancellationToken token)
            {
                BluetoothGattCharacteristic charac = ConnectedGatt.GetService(DEX_SERVICE_SPP)
                                                     .GetCharacteristic(DEX_CHARACTERISTIC_DATAWRITE);

                lock (ConnectedGatt)
                {
                    try
                    {
                        int    ctr    = 0;
                        byte[] buffer = new byte[20];
                        while (bytes.Length > ctr * 20)
                        {
                            var len   = bytes.Length;
                            var rendu = ctr * 20;

                            buffer = len > rendu + 20
                                ? bytes.Skip(20 * ctr).Take(20).ToArray()
                                : bytes.Skip(20 * ctr).Take(len - rendu).ToArray();
                            charac.SetValue(buffer);
                            ctr++;

                            Java.Lang.Thread.Sleep(20);
                            // charac.WriteType = GattWriteType.NoResponse;
                            bool status = ConnectedGatt.WriteCharacteristic(charac);
                            if (!status || ctr * 20 > len)
                            {
                                var read = ConnectedGatt.ReadCharacteristic(charac);
                                charac.GetValue();
                                if (read)
                                {
                                    MainActivity.RunOnUiThread(() =>
                                    {
                                        Log.Info(TAG + DexService.Operation, "Read completed!");
                                    });
                                }

                                break;
                            }

                            Arrays.Fill(buffer, 0);
                        }
                    }
                    catch (Java.IO.IOException e)
                    {
                        e.PrintStackTrace();
                    }
                    catch (InterruptedException e)
                    {
                        e.PrintStackTrace();
                    }
                    catch (System.OperationCanceledException)
                    {
                        Log.Warn(TAG + DexService.Operation, "Send Dex Bytes Cancelled");
                    }
                    catch (Java.Lang.Exception ex)
                    {
                        Log.Error(TAG, ex.Message);
                    }
                }
            }