Ejemplo n.º 1
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}");
            }
Ejemplo n.º 2
0
        public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicRead(gatt, characteristic, status);

            ReadSemaphore.Release();

            if (characteristic.Uuid == BcoreUuid.BatteryVol)
            {
                ReadBatteryResult?.Invoke(this, new ReadBatteryVoltageEventArgs(characteristic.GetValue()));
            }
            else if (characteristic.Uuid == BcoreUuid.GetFunctions)
            {
                ReadFunctionResult?.Invoke(this, new ReadFunctionsEventArgs(characteristic.GetValue()));
            }
        }
Ejemplo n.º 3
0
 public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
 {
     if (characteristic != null && characteristic.Uuid.ToString().ToUpper().Equals(BTValues.rxCharacteristic.ToUpper()))
     {
         client.callback.onUartDataSent(characteristic.GetValue());
     }
 }
Ejemplo n.º 4
0
 public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
 {
     if (characteristic != null && characteristic.Uuid.ToString().ToUpper().Equals(BTValues.txCharacteristic.ToUpper()) && status == GattStatus.Success)
     {
         client.callback.onUartDataReceived(characteristic.GetValue());
     }
 }
Ejemplo n.º 5
0
        // ReSharper disable once UnusedMethodReturnValue.Local
        private bool ReadGattSppData(BluetoothGattCharacteristic characteristic)
        {
            try
            {
                if (characteristic.Uuid != null && _gattCharacteristicUuidSppRead != null &&
                    characteristic.Uuid.Equals(_gattCharacteristicUuidSppRead))
                {
                    byte[] data = characteristic.GetValue();
                    if (data != null)
                    {
#if DEBUG
                        Android.Util.Log.Info(Tag, string.Format("GATT SPP data received: {0} '{1}'",
                                                                 BitConverter.ToString(data).Replace("-", ""), Encoding.UTF8.GetString(data)));
#endif
                        _btGattSppInStream?.Write(data);
                        _btGattReceivedEvent.Set();
                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(false);
        }
Ejemplo n.º 6
0
        public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicWrite(gatt, characteristic, status);

            Trace.Message("OnCharacteristicWrite: value {0} status {1}", characteristic.GetValue().ToHexString(), status);

            var isSuccessful = false;

            switch (status)
            {
            case GattStatus.Failure:
            case GattStatus.InsufficientAuthentication:
            case GattStatus.InsufficientEncryption:
            case GattStatus.InvalidAttributeLength:
            case GattStatus.InvalidOffset:
            case GattStatus.ReadNotPermitted:
            case GattStatus.RequestNotSupported:
            case GattStatus.WriteNotPermitted:
                break;

            case GattStatus.Success:
                isSuccessful = true;
                break;
            }

            CharacteristicValueWritten?.Invoke(this, new CharacteristicWriteCallbackEventArgs(characteristic, isSuccessful));
        }
Ejemplo n.º 7
0
        public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicWrite(gatt, characteristic, status);

            Mvx.Trace("OnCharacteristicWrite: value {0} status {1}", characteristic.GetValue().ToHexString(), status);

            var args = new CharacteristicWriteEventArgs {
                Characteristic = new Characteristic(characteristic, gatt, this)
            };

            switch (status)
            {
            case GattStatus.Failure:
            case GattStatus.InsufficientAuthentication:
            case GattStatus.InsufficientEncryption:
            case GattStatus.InvalidAttributeLength:
            case GattStatus.InvalidOffset:
            case GattStatus.ReadNotPermitted:
            case GattStatus.RequestNotSupported:
            case GattStatus.WriteNotPermitted:
                args.IsSuccessful = false;
                break;

            case GattStatus.Success:
                args.IsSuccessful = true;
                break;
            }

            CharacteristicValueWritten(this, args);
        }
Ejemplo n.º 8
0
        public void OnCharacteristicCallback(BluetoothGattCharacteristic characteristic)
        {
            byte[] values = characteristic.GetValue();
            string uuid   = characteristic.Uuid + "";
            string str    = "changed characteristic's uuid is : " + characteristic.Uuid + "\r\n";

            str += "changed characteristic's WriteType is : " + characteristic.WriteType + "\r\n";
            str += "changed characteristic's GetValue is ";
            string value = "";

            foreach (byte b in values)
            {
                str   += ": " + b.ToString("X2");
                value += b.ToString("X2") + ":";
            }

            Action act = () =>
            {
                Toast.MakeText(this, value, ToastLength.Short).Show();
            };

            RunOnUiThread(act);

            Log.Error("dana.ye->xamarin->bt", str);
        }
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            base.OnCharacteristicChanged(gatt, characteristic);
            var value = characteristic.GetValue();

            OnCharacteristicAlreadyChanged?.Invoke(this, new CharacteristicAlreadyChangedEventArgs(characteristic.Uuid, value));
        }
Ejemplo n.º 10
0
 public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, [GeneratedEnum] GattStatus status)
 {
     lock (_lock)
     {
         _readCompletionSource?.TrySetResult(characteristic.GetValue());
     }
 }
Ejemplo n.º 11
0
 public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
 {
     if (characteristic.Equals(readCharacteristic))
     {
         ReceivePacket(characteristic.GetValue());
     }
 }
Ejemplo n.º 12
0
        private void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            var uuid = characteristic.Uuid.ToString().ToLower();

            if (_readQueue.ContainsKey(uuid))
            {
                var readItem = _readQueue[uuid];
                _readQueue.Remove(uuid);

                var dataBytes = characteristic.GetValue();


                if (OWBoard.SerialWriteUUID.Equals(uuid, StringComparison.InvariantCultureIgnoreCase) == false &&
                    OWBoard.SerialReadUUID.Equals(uuid, StringComparison.InvariantCultureIgnoreCase) == false)
                {
                    // If our system is little endian, reverse the array.
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(dataBytes);
                    }
                }

                readItem.SetResult(dataBytes);
            }

            _gattOperationQueueProcessing = false;
            ProcessQueue();
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Called when a characteristic changes
        /// </summary>
        /// <param name="gatt">Gatt</param>
        /// <param name="characteristic">Characteristic.</param>
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            base.OnCharacteristicChanged(gatt, characteristic);

            // Setting the last update to be now
            lastChange = DateTime.Now;

            // If this is one of chars we are tracking
            if (notifyChars.Contains(characteristic.Uuid.ToString()))
            {
                Log.Debug(TAG, "The Characteristic Changed");

                // Getting and setting the data
                byte[] data = characteristic.GetValue();
                handle.updateData(data);

                // Notifying
                Intent message = new Intent();
                message.SetAction(AppUtil.SENSOR_READING_UPDATE_ACTION);

                Bundle intentBundle = new Bundle();
                intentBundle.PutString(AppUtil.ADDRESS_KEY, gatt.Device.Address);
                intentBundle.PutString(AppUtil.DETAIL_KEY, handle.xmlDetail);
                message.PutExtras(intentBundle);

                SendBroadcast(message);
            }
        }
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            if (characteristic.Uuid.Equals(DT1WatchDogService.DT1WatchdogDataCharacteristicUUID))
            {
                log.Debug("Watchdog data characteristic changed.");

                // we got data - fine we disconnect and wait for the next alarm

                var CLIENT_CHARACTERISTIC_CONFIG        = Java.Util.UUID.FromString("00002902-0000-1000-8000-00805f9b34fb");
                var clientCharacteristiConfigDescriptor = characteristic.GetDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
                clientCharacteristiConfigDescriptor.SetValue(BluetoothGattDescriptor.DisableNotificationValue.ToArray());
                gatt.WriteDescriptor(clientCharacteristiConfigDescriptor);

                gatt.Disconnect();

                var data    = characteristic.GetValue();
                var reading = GlucoseReading.ParseRawCharacteristicData(data);

                // fill in source
                reading.Source = gatt.Device.Name;

                dataService.PersistReading(reading);

                if (reading.ErrorCode == GlucoseReading.ReadingErrorCode.NoError)
                {
                    dataService.LastValidReading = DateTimeOffset.UtcNow;
                    EvaluateReading(reading);
                }

                var dataIntent = new Intent(DT1WatchDogService.IntentIncommingData);
                service.SendBroadcast(dataIntent);
            }
        }
Ejemplo n.º 15
0
        private void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            var uuid = characteristic.Uuid.ToString().ToLower();

            var writeCharacteristicValueRequest = _writeQueue.FirstOrDefault(t => t.CharacteristicId.Equals(uuid));


            if (writeCharacteristicValueRequest != null)
            {
                _writeQueue.Remove(writeCharacteristicValueRequest);

                var dataBytes = characteristic.GetValue();

                if (OWBoard.SerialWriteUUID.Equals(uuid, StringComparison.InvariantCultureIgnoreCase) == false &&
                    OWBoard.SerialReadUUID.Equals(uuid, StringComparison.InvariantCultureIgnoreCase) == false)
                {
                    // If our system is little endian, reverse the array.
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(dataBytes);
                    }
                }

                writeCharacteristicValueRequest.CompletionSource.SetResult(dataBytes);
            }

            _gattOperationQueueProcessing = false;
            ProcessQueue();
        }
Ejemplo n.º 16
0
 // Overridden from BaseLeConnection
 public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
 {
     if (characteristic.Equals(readCharacteristic))
     {
         lastPacket = characteristic.GetValue();
     }
 }
 private void GattObserverOnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
 {
     if (_onRead != null)
     {
         _onRead(characteristic.GetValue(), status);
     }
 }
Ejemplo n.º 18
0
 /// <summary>
 ///
 /// </summary>
 public virtual void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
 {
     m_IsDirty = true; m_Text = characteristic.GetValue().ToHexString();
     if (m_TxtReport == null)
     {
         Log.i("BluetoothScannerGUI", m_Text);
     }
 }
Ejemplo n.º 19
0
        public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicWrite(gatt, characteristic, status);

            Trace.Message("OnCharacteristicWrite: value {0} status {1}", characteristic.GetValue().ToHexString(), status);

            CharacteristicValueWritten?.Invoke(this, new CharacteristicWriteCallbackEventArgs(characteristic, GetExceptionFromGattStatus(status)));
        }
Ejemplo n.º 20
0
        public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicRead(gatt, characteristic, status);

            Trace.Message("OnCharacteristicRead: value {0}; status {1}", characteristic.GetValue().ToHexString(), status);

            CharacteristicValueUpdated?.Invoke(this, new CharacteristicReadCallbackEventArgs(characteristic));
        }
 private void BroadcastUpdate(string action, BluetoothGattCharacteristic characteristic)
 {
     // This is special handling for the Heart Rate Measurement profile.  Data parsing is
     // carried out as per profile specifications:
     // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
     if (TX_CHAR_UUID2.Equals(characteristic.Uuid))
     {
         Debug.WriteLine(TAG, string.Format("Received Text: {0:D}", characteristic.GetValue().Length));
         CallBack.CallbackMethod(action + EXTRA_DATA, characteristic.GetValue());
     }
     else
     {
         if (NAME_CHAR_UUID.Equals(characteristic.Uuid))
         {
             CallBack.CallbackMethod(action + NAME_DATA, characteristic.GetValue());
         }
     }
 }
Ejemplo n.º 22
0
 public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
 {
     lock (_lock)
     {
         var guid = characteristic.Uuid.ToGuid();
         var data = characteristic.GetValue();
         _onCharacteristicChanged?.Invoke(guid, data);
     }
 }
Ejemplo n.º 23
0
        public BLECharacteristic(BluetoothGattCharacteristic characteristic)
        {
            _nativeCharacteristic = characteristic;

            Value       = _nativeCharacteristic.GetValue();
            Guid        = GuidHelper.FromUUID(_nativeCharacteristic.Uuid);
            ServiceGuid = GuidHelper.FromUUID(_nativeCharacteristic.Service.Uuid);
            Properties  = (CharacteristicPropertyType)(int)_nativeCharacteristic.Properties;
        }
Ejemplo n.º 24
0
        public void ReadCharacteristic(BluetoothGattCharacteristic characteristic)
        {
            if (BlutoothService.GetAssignedNumber(characteristic.Uuid) == 0x2A00)   //org.bluetooth.characteristic.gap.device_name
            {
                byte[] value = characteristic.GetValue();
                if (value != null)
                {
                    deviceName = BitConverter.ToString(value);
                }
            }

            if (BlutoothService.GetAssignedNumber(characteristic.Uuid) == 0x2A01)   //org.bluetooth.characteristic.gap.appearance
            {
                byte[] value = characteristic.GetValue();
                if (value != null)
                {
                    appearance = BitConverter.ToInt16(value, 0);
                }
            }
        }
Ejemplo n.º 25
0
        private async Task <GattReadResult> DoReadValueAsync(BluetoothCacheMode cacheMode)
        {
            bool success = true;

            if (cacheMode == BluetoothCacheMode.Uncached)
            {
                success = _service.Device._bluetoothGatt.ReadCharacteristic(_characteristic);
                _readHandle.WaitOne();
            }

            return(new GattReadResult(success ? GattCommunicationStatus.Success : GattCommunicationStatus.Unreachable, _characteristic.GetValue()));
        }
Ejemplo n.º 26
0
        public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
        {
            base.OnCharacteristicRead(gatt, characteristic, status);

            Mvx.Trace("OnCharacteristicRead: value {0}; status {1}", characteristic.GetValue().ToHexString(), status);

            CharacteristicValueUpdated(this, new CharacteristicReadEventArgs
            {
                Characteristic = new Characteristic(characteristic, gatt, this)
            }
                                       );
        }
        private void NotifyOnValueChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            var hexText    = FindViewById <TextView>(Resource.Id.ReceivedNotificationHex);
            var stringText = FindViewById <TextView>(Resource.Id.ReceivedNotificationString);

            var receivedBytes = characteristic.GetValue();

            RunOnUiThread(() =>
            {
                hexText.SetText(BitConverter.ToString(receivedBytes), TextView.BufferType.Normal);
                stringText.SetText(Encoding.ASCII.GetString(receivedBytes), TextView.BufferType.Normal);
            });
        }
Ejemplo n.º 28
0
 public override void OnCharacteristicRead(BluetoothGatt peripheral, BluetoothGattCharacteristic characteristic, GattStatus status)
 {
     try
     {
         byte[] valueBytes  = characteristic.GetValue();
         string valueString = Encoding.UTF8.GetString(valueBytes);
         _readCompletionSource.SetResult(valueString);
     }
     catch (Exception ex)
     {
         SensusServiceHelper.Get().Logger.Log("Exception while getting characteristic string after reading it:  " + ex, LoggingLevel.Normal, GetType());
     }
 }
Ejemplo n.º 29
0
            public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
            {
                if (status == GattStatus.Success)
                {
                    var data = characteristic.GetValue();
                    _scannerService.AddData(data);
                }
                else
                {
                    _scannerService.ReadDataErrorMessage = "Failed to read Data.";
                }

                Android.Util.Log.Warn(ScannerService.TAG, "OnCharacteristicRead received: " + status);
            }
Ejemplo n.º 30
0
 /// <summary>
 ///
 /// </summary>
 public virtual void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
 {
     //Log.i("BleSerialPort","OnCharacteristicRead"+m_ReadCallback);
     m_BufferRead = characteristic.GetValue(); m_SizeRead = m_BufferRead.Length;
     if (m_ReadCallback != null)
     {
         m_ReadCallback.OnStreamRead(this);
     }
     else
     {
         //Log.e("BleSerialPort","m_ReadCallback==null");
     }
     //m_Buffer=null;
 }