Пример #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
            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}");
            }
Пример #3
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;
                }
            }