예제 #1
0
            public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
            {
                base.OnConnectionStateChange(gatt, status, newState);

                try
                {
                    if (status == GattStatus.Success && newState == ProfileState.Connected)
                    {
                        GattConnected = true;
                        MainActivity.RunOnUiThread(() => { Log.Info(TAG + DexService.Operation, "Connected"); });
                        DexService.LogMessage2("Gatt Connected", false);
                    }

                    if (newState == ProfileState.Disconnected)
                    {
                        GattConnected = false;
                        DexService.LogMessage2("Gatt Disconnected", false);
                        return;
                    }

                    gatt.DiscoverServices();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
예제 #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);


            //connectedHandleAddress = "00:10:20:8E:2D:7E";
            //connectedHandleAddress = "00:10:20:8E:2B:27";
            //connectedHandleAddress = "00:10:20:8E:28:9D";
            connectedHandleAddress = "00:10:20:8E:2B:ED";
            base.OnCreate(savedInstanceState);

            Xamarin.Forms.Forms.Init(this, savedInstanceState);
            try
            {
                DexService = new DexService(this);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


            DexService.Init();

            LoadApplication(new App());

            App.DexService = DexService;

            currentActivity = this;



            //if (DexService.BluetoothAdapter == null)
            //{
            //    Log.Info(DexService.TAG + DexService.Operation, "Initialize bluetooth adapter failed!");
            //}
            //else
            //{
            //    IntentFilter mfilter = new IntentFilter(BluetoothDevice.ActionAclConnected);
            //    mfilter.AddAction(BluetoothDevice.ActionPairingRequest);
            //    mfilter.AddAction(BluetoothDevice.ActionBondStateChanged);
            //    mfilter.AddAction(BluetoothDevice.ActionFound);
            //    mfilter.AddAction(BluetoothAdapter.ActionDiscoveryFinished);
            //    mfilter.Priority = (int)IntentFilterPriority.HighPriority;
            //    RegisterReceiver(Receiver, mfilter);
            //}

            //PendingIntent = PendingIntent.GetActivity(this,
            //    0,
            //    new Intent(this,
            //        GetType()).AddFlags(ActivityFlags.SingleTop),
            //    0);
        }
예제 #3
0
            public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
            {
                Log.Info(TAG + DexService.Operation,
                         $"Characteristic {characteristic.Uuid} Changed => {characteristic.GetValue()?.ToListOfByte()}");

                base.OnCharacteristicChanged(gatt, characteristic);

                try
                {
                    if (!characteristic.Uuid.Equals(DEX_CHARACTERISTIC_DATAWRITE))
                    {
                        return;
                    }

                    if (DEX_CHARACTERISTIC_DATAWRITE.Equals(characteristic.Uuid))
                    {
                        byte[] receive = characteristic.GetValue();
                        lock (DataReadOperationSync)
                        {
                            var characteristicValue = new string(Encoding.Default.GetChars(receive));

                            mReceiveBuffer.Append(characteristicValue);

                            Log.Info(TAG + DexService.Operation,
                                     $"Data Received => {receive.ToListOfByte()}");

                            DexService.LogMessage2($"Data Received => {receive.ToListOfByte()}", false);

                            if (DexService.Stacking)
                            {
                                DexService.StackList.Add(receive);
                            }

                            ReceiveBytes = ByteHelper.Combine(ReceiveBytes, receive);

                            Log.Info(TAG + DexService.Operation,
                                     $"ReceivedBytes is now  => {ReceiveBytes.ToListOfByte()}");
                        }
                    }
                }
                catch (Java.IO.IOException e)
                {
                    e.PrintStackTrace();
                }
                catch (InterruptedException e)
                {
                    e.PrintStackTrace();
                }
                catch (Exception exception)
                {
                    Log.Error(TAG, $"Error dans OnCharacteristicChanged {exception.Message}");
                }

                //Log.Info(DexService.TAG + DexService.Operation, $"Characteristic UUID IS => {characteristic.Uuid}");
                //Log.Info(DexService.TAG + DexService.Operation, $"DEX UUID IS            => {DexService.DEX_CHARACTERISTIC_DATAWRITE}");
            }
예제 #4
0
 public BLEBroadcastReceiver(MainActivity mainActivity, DexService dexService)
 {
     MainActivity = mainActivity;
     DexService   = dexService;
 }
예제 #5
0
 public BluetoothScanCallback(MainActivity mainActivity, DexService dexService)
 {
     MainActivity = mainActivity;
     DexService   = dexService;
 }
예제 #6
0
 public SendDexFile(MainActivity mainActivity, DexService dexService)
 {
     DexService   = dexService;
     MainActivity = mainActivity;
 }
예제 #7
0
            public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
            {
                base.OnServicesDiscovered(gatt, status);

                try
                {
                    foreach (var bluetoothGattService in gatt.Services)
                    {
                        Log.Info(TAG, $"Service Discovered => {bluetoothGattService.Uuid}");
                        foreach (var bluetoothGattCharacteristic in bluetoothGattService.Characteristics)
                        {
                            Log.Info(TAG, $"Characteristic Discovered => {bluetoothGattCharacteristic.Uuid}");
                        }
                    }



                    WriteCharacteristic = gatt.GetService(DEX_SERVICE_SPP)
                                          .GetCharacteristic(DEX_CHARACTERISTIC_DATAWRITE);


                    Log.Info(TAG, $"WriteCharacteristic Properties => {WriteCharacteristic.Properties.ToString()}");


                    DeviceCharacteristic = gatt.GetService(DEX_SERVICE).GetCharacteristic(DEVICE_SETTINGS);


                    Log.Info(TAG, $"DeviceCharacteristic Properties => {DeviceCharacteristic.Properties.ToString()}");


                    StatusCharacteristic = gatt.GetService(DEX_SERVICE_SPP)
                                           .GetCharacteristic(DEX_CHARACTERISTIC_STATUS);


                    Log.Info(TAG, $"StatusCharacteristic Properties => {StatusCharacteristic.Properties.ToString()}");


                    gatt.SetCharacteristicNotification(DeviceCharacteristic, true);

                    var descDevice = DeviceCharacteristic.GetDescriptor(CLIENT_CHARACTERISTIC_CONFIG);

                    Log.Info(TAG, $"descDevice Permissions => {descDevice.Permissions.ToString()}");


                    descDevice.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
                    gatt.WriteDescriptor(descDevice);
                    descDevice.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray());
                    gatt.WriteDescriptor(descDevice);


                    if (gatt.SetCharacteristicNotification(WriteCharacteristic, true))
                    {
                        MainActivity.RunOnUiThread(() =>
                        {
                            Log.Info(TAG + DexService.Operation, "Set Write Characteristic succeeded");
                        });
                        DexService.LogMessage2("Set Write Characteristic succeeded", false);
                    }
                    else
                    {
                        MainActivity.RunOnUiThread(() => { Log.Info(TAG + DexService.Operation, "Set Write Characteristic failed"); });
                    }

                    BluetoothGattDescriptor descWrite = WriteCharacteristic.GetDescriptor(CLIENT_CHARACTERISTIC_CONFIG);

                    Log.Info(TAG, $"descWrite Permissions => {descWrite.Permissions.ToString()}");



                    descWrite.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
                    gatt.WriteDescriptor(descWrite);
                    descWrite.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray());
                    gatt.WriteDescriptor(descWrite);
                    DexService.Connected = true;
                    DexService.LogMessage2("Device Connected", false);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
예제 #8
0
 public LEGattCallback(MainActivity mainActivity, DexService dexService)
 {
     MainActivity = mainActivity;
     NextStep     = NextStep.TxBase;
     DexService   = dexService;
 }