2016-08-30 91 views
0

我有一個與GATT心率監視器進行通信的應用程序。有時,傳感器失敗,我想重新初始化連接。如果舊連接失敗但是不起作用(請參見How to delete object?),我想將該連接包裝在類中並聲明新對象。然後我嘗試實施IDisposable,但我不知道如何。我也考慮過使用CancelationToken,但也不知道如何。這裏是代碼:如何發佈藍牙GATT連接

public class MainPage : Page 
{ 
    ConnectingObject conn; 
    MainPage() 
    { 
     this.InitializeComponent(); 
     conn = new ConnectingObject(); 
    } 
    public void cancelConnection() 
    { 
     conn.Dispose(); 
    } 
} 

public class ConnectingObject : IDisposable 
{ 
    bool disposed = false; 
    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
    protected virtual void Dispose(bool disposing) 
    { 
     if (disposed) 
      return; 

     if (disposing) 
     { 
      // Free any other managed objects here. 
      // 
     } 

     // Free any unmanaged objects here. 
     // 
     disposed = true; 
    } 

    ~HRGATTConnect() 
    { 
     Dispose(false); 
    } 
    async void Initialize() 
    { 
     try 
     { 
      var heartrateServices = await Windows.Devices.Enumeration 
       .DeviceInformation.FindAllAsync(GattDeviceService 
        .GetDeviceSelectorFromUuid(
         GattServiceUuids.HeartRate), 
       null); 

      GattDeviceService firstHeartRateMonitorService = await 
       GattDeviceService.FromIdAsync(heartrateServices[0].Id); 

      //Debug.WriteLine("serviceName: " + heartrateServices[0].Name); 

      GattCharacteristic hrMonitorCharacteristics = 
       firstHeartRateMonitorService.GetCharacteristics(
        GattCharacteristicUuids.HeartRateMeasurement)[0]; 

      hrMonitorCharacteristics.ValueChanged += hrMeasurementChanged; 

      await hrMonitorCharacteristics 
        .WriteClientCharacteristicConfigurationDescriptorAsync(
         GattClientCharacteristicConfigurationDescriptorValue.Notify);     
     } 
     catch (Exception e) 
     { 

     } 
    } 
    void hrMeasurementChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs) 
    { 
     try 
     { 
      byte[] hrData = new byte[eventArgs.CharacteristicValue.Length]; 
      Windows.Storage.Streams.DataReader.FromBuffer(
       eventArgs.CharacteristicValue).ReadBytes(hrData); 
      data_processing(hrData);     
      } 
     } 
     catch (Exception e) 
     { 

     } 
    } 

謝謝。

回答

1

如何釋放藍牙連接GATT

你需要呼籲GattDeviceService的Dispose(),確保所有GattDeviceService和GattCharacteristic對象爲空。你可以這樣編輯你的代碼:

public sealed partial class MainPage : Page 
{ 
    ConnectingObject conn; 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     conn = new ConnectingObject(); 
    } 
    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Frame.Navigate(typeof(BluetoothConnect)); 
    } 
    public void cancelConnection() 
    { 
     conn.Dispose(); 
    } 

    private void DisconnectButton_Click(object sender, RoutedEventArgs e) 
    { 
     conn.Dispose(); 
    } 

    private void ConnectButton_Click(object sender, RoutedEventArgs e) 
    { 
     conn.Initialize(); 
    } 
} 

public class ConnectingObject : IDisposable 
{ 
    GattCharacteristic hrMonitorCharacteristics; 
    GattDeviceService firstHeartRateMonitorService; 

    bool disposed = false; 
    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
    protected virtual void Dispose(bool disposing) 
    { 
     if (disposed) 
      return; 

     if (disposing) 
     { 
      // Free any other managed objects here. 
      // 
      if (firstHeartRateMonitorService != null) 
      { 
       firstHeartRateMonitorService.Dispose(); 
       firstHeartRateMonitorService = null; 
      } 
      if (hrMonitorCharacteristics != null) 
      { 
       hrMonitorCharacteristics.Service.Dispose(); 
       hrMonitorCharacteristics = null; 
      } 
     } 

     // Free any unmanaged objects here. 
     // 
     disposed = true; 
    } 

    //~HRGATTConnect() 
    //{ 
    // Dispose(false); 
    //} 
    public async void Initialize() 
    { 
     try 
     { 
      var heartrateServices = await Windows.Devices.Enumeration 
       .DeviceInformation.FindAllAsync(GattDeviceService 
        .GetDeviceSelectorFromUuid(
         GattServiceUuids.HeartRate), 
       null); 

      firstHeartRateMonitorService = await 
       GattDeviceService.FromIdAsync(heartrateServices[0].Id); 

      Debug.WriteLine("serviceName: " + heartrateServices[0].Name); 

      hrMonitorCharacteristics = 
       firstHeartRateMonitorService.GetCharacteristics(
        GattCharacteristicUuids.HeartRateMeasurement)[0]; 

      hrMonitorCharacteristics.ValueChanged += hrMeasurementChanged; 

      await hrMonitorCharacteristics 
        .WriteClientCharacteristicConfigurationDescriptorAsync(
         GattClientCharacteristicConfigurationDescriptorValue.Notify); 

      disposed = false; 

     } 
     catch (Exception e) 
     { 

     } 
    } 
    void hrMeasurementChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs) 
    { 
     try 
     { 
      byte[] hrData = new byte[eventArgs.CharacteristicValue.Length]; 
      Windows.Storage.Streams.DataReader.FromBuffer(
       eventArgs.CharacteristicValue).ReadBytes(hrData); 
      //data_processing(hrData); 
     } 
     catch (Exception e) 
     { 

     } 
    } 

}