2017-12-19 32 views
0

私はXamarinフォームでのブロードキャストメッセージの受信機を実装しようとしているが、任意のメッセージを受信することができませんフォーム、以下の私の主な活動コード放送受信機は、私が間違って何が起こっているかわからない、

[Activity(Label = "HeloApp.Android", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      TabLayoutResource = Resource.Layout.Tabbar; 
      ToolbarResource = Resource.Layout.Toolbar; 

      base.OnCreate(bundle); 

      global::Xamarin.Forms.Forms.Init(this, bundle); 

      LoadApplication(new App()); 
      Connector.Instance.Initialize(this, "151265380086941117", "E862D6D7D70ED2C3729CB24BC3AB40753A52EAB0"); 
      MeasurementReceiver heartRateRec = new MeasurementReceiver(); 
      RegisterReceiver(heartRateRec, new IntentFilter("com.worldgn.connector.HR_MEASUREMENT")); 
     } 

    } 

    [BroadcastReceiver(Enabled = true)] 
    [IntentFilter(new[] { "com.worldgn.connector.HR_MEASUREMENT" })] 
    public class MeasurementReceiver : BroadcastReceiver 
    { 
     public override void OnReceive(Context context, Intent intent) 
     { 
      if (intent.Action.Equals("com.worldgn.connector.HR_MEASUREMENT")) 
      { 
       var heartRate = intent.GetStringExtra("HR_MEASUREMENT"); 
      } 
     } 
    } 
です

以下は、私がxamarinフォーム側からブロードキャストレシーバーを開始する方法です。私は放送受信機がどのように与えられたシナリオで働くかわからないので、私は初めてそれをやっています。

[XamlCompilation(XamlCompilationOptions.Compile)] 
    public partial class BLEDevices : ContentPage 
    { 
     public BLEDevices() 
     { 
      InitializeComponent(); 
     } 

     private void SearchBLE_Clicked(object sender, EventArgs e) 
     { 
      Connector.Instance.Scan(new DeviceScanCallBack()); 
     } 

     private void Disconnect_Clicked(object sender, EventArgs e) 
     { 
      Connector.Instance.UnbindDevice(); 
     } 
    } 

    public class DeviceScanCallBack : Java.Lang.Object, IScanCallBack 
    { 
     public void OnLedeviceFound(DeviceItem p0) 
     { 
      Connector.Instance.Connect(p0); 
      App.Current.MainPage.DisplayAlert("Information", "Device connected successfully.", "Cancel"); 

      //Here i am trying to initiate the broadcast receiver 
      Connector.Instance.MeasureHR(); 
     } 

     public void OnPairedDeviceNotFound() 
     { 
      App.Current.MainPage.DisplayAlert("Warning", "Please disconnect the device first.", "OK"); 
     } 

     public void OnScanFinished() 
     { 

     } 

     public void OnScanStarted() 
     { 

     } 
    } 

私が達成しようとしているのは、デバッグモードでOnReceiveメソッドを押すだけで、すべての問題を解決できます。

助けてください。ありがとう

答えて

1

私はxamarinフォーム側からブロードキャスト受信者を開始しています。

これは、ネイティブAPI(AndroidプラットフォームAPI SendBroadcast)を使用することを意味します。そうですか?それが正しい場合、xamarin.AndroidDependencyServiceを使用して達成することができます。

つのステップ:あなたはxamarin forms sideと呼ばれているPCL

1.Defineインタフェース:

public interface ISend 
    { 
     void Send(); 
    } 

2.Implementation Androidプラットフォーム:レジスタにDependencyService 3.Use

class SendImpl : ISend 
{ 
    public void Send() 
    { 
     Intent intent = new Intent("com.worldgn.connector.HR_MEASUREMENT"); 
     intent.PutExtra("HR_MEASUREMENT","value"); 
     Forms.Context.SendBroadcast(intent); 
    } 
} 

これを名前空間の上に追加します。

[assembly:Dependency(typeof(SendImpl))] 

4.Call PCLでDependencyServiceに、あなたはOnReceiveを達成します:

public void OnLedeviceFound(DeviceItem p0) 
{ 
    Connector.Instance.Connect(p0); 
    App.Current.MainPage.DisplayAlert("Information", "Device connected successfully.", "Cancel"); 

    //Here i am trying to initiate the broadcast receiver 
    DependencyService.Get<ISend>().Send(); 
    Connector.Instance.MeasureHR(); 
} 
+0

は、私はあなたがここに示唆しているが、まだ何かが欠けているとのインタフェースを実装しています。 – user6352350

+0

'Init(this、bundle)'の上に 'RegisterReceiver'を置くのはどうですか? –

関連する問題