Xamarin BLE体重秤阅读问题

时间:2018-08-29 14:02:59

标签: c# xamarin bluetooth-lowenergy

我和我的团队不熟悉BLE Android开发,我们正在使用C#Xamarin。

我们正在尝试连接和读取BLE体重秤和血氧饱和度计(Berrymed设备)上的读数,并且我们能够连接和读取设备信息,但无法读取实际的读数,日期和时间。

我们尝试了一切,但没有运气。 C#代码示例的任何帮助或建议都将真正有帮助。我们看到了很多Java示例,但没有看到Xamarin。

该设备是中国HeiTei体重秤

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Bluetooth;
using Android.OS;
using Android.Widget;
using AndroidBluetoothLE.Bluetooth.Client;
using Java.Util;
public class ReadCharacteristicView : BaseCharacteristicView
    {
        private BluetoothGattCharacteristic _characteristic;
        private DeviceReadingHandler _readingHandler;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.ReadCharacteristicView);

            InitializeView();
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            _readingHandler.Dispose();
        }

        private void InitializeView()
        {
            var connectionHandler = BluetoothClient.Instance.ConnectionHandler;
            _readingHandler = new DeviceReadingHandler(connectionHandler.GattValue, GattClientObserver.Instance);
            _characteristic = GetCharacteristic(connectionHandler.GetServiceList());

            var readButton = FindViewById<Button>(Resource.Id.ReadCharacteristicButton);
            readButton.Click += ReadButtonOnClick;
        }

        private BluetoothGattCharacteristic GetCharacteristic(IEnumerable<BluetoothGattService> serviceList)
        {
            var uuid = (UUID)Intent.GetSerializableExtra("Characteristic");

            var service = serviceList.First(s => s.Characteristics.Any(ch => ch.Uuid.Equals(uuid)));
            return service.Characteristics.First(ch => ch.Uuid.Equals(uuid));
        }

        private void ReadButtonOnClick(object sender, EventArgs eventArgs)
        {
            var hexText = FindViewById<TextView>(Resource.Id.HexReadText);
            var stringText = FindViewById<TextView>(Resource.Id.StringReadText);

            ShowDialog("Start reading...");
            _readingHandler.Read(_characteristic, (bytes, status) => RunOnUiThread(() => 
            {
                if (status != GattStatus.Success)
                {
                    ShowDialog("Reading failed with status: " + status);
                    return;
                }

                hexText.SetText(BitConverter.ToString(bytes), TextView.BufferType.Normal);
                stringText.SetText(Encoding.ASCII.GetString(bytes), TextView.BufferType.Normal);
                ShowDialog("Read Success!");
            }));
        }

        private void ShowDialog(string message)
        {
            DialogView.ShowDialog(message, this);
        }
    }

0 个答案:

没有答案