2017-12-19 21 views
0

내가 자 마린 양식에서 방송 메시지 수신을 구현하기 위해 노력하고 있지만, 모든 메시지를 수신 할 수 있어요 양식, 다음 내 주요 활동 코드방송 수신기가 내가 잘못 무슨 일이 일어나고 있는지 확실하지 않다,

[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을 사용할 수 있습니다.

네 단계 :

1.Define 당신이 xamarin forms side로 불리는 한 PCL의 인터페이스 :

public interface ISend 
    { 
     void Send(); 
    } 

2.Implementation 안드로이드 플랫폼 :

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

3.Use DependencyService 등록하기 이를 네임 스페이스 위에 추가하십시오.

,210
[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'를 붙이면 어떨까요? –