ble.net icon indicating copy to clipboard operation
ble.net copied to clipboard

Example doesn't work for Android - init and obtaindefaultadapter

Open HeadBanging opened this issue 5 years ago • 1 comments

The documentation says to use BluetoothLowEnergyAdapter.InitActivity( this ); in OnCreate but this initActivity doesn't exist. The example code is as follows:

  protected override void OnCreate( Bundle bundle )
  {
     TabLayoutResource = Resource.Layout.Tabbar;
     ToolbarResource = Resource.Layout.Toolbar;

     base.OnCreate( bundle );

     UserDialogs.Init( this );
     Forms.Init( this, bundle );

     // If you want to enable/disable the Bluetooth adapter from code, you must call this.
     BluetoothLowEnergyAdapter.Init( this );
     // Obtain the bluetooth adapter so we can pass it into our (shared-code) Xamarin Forms app. There are
     // additional Obtain() methods on BluetoothLowEnergyAdapter if you have more specific needs (e.g. if you
     // need to support devices with multiple Bluetooth adapters)
     var bluetooth = BluetoothLowEnergyAdapter.ObtainDefaultAdapter( ApplicationContext );

     LoadApplication( new FormsApp( bluetooth, UserDialogs.Instance ) );
  }

This doesn't work for me either. I understand that InitActivity has become Init, but when I try the following it doesn't work:

    protected IBluetoothLowEnergyAdapter bluetooth; // Need to retain this adapter reference for the lifetime of the app

    protected override void OnCreate(Bundle savedInstanceState) {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        BluetoothLowEnergyAdapter.Init(this);
        bluetooth = BluetoothLowEnergyAdapter.ObtainDefaultAdapter(ApplicationContext);

        LoadApplication(new App());
    }

What am I doing wrong? All help gratefully received!

HeadBanging avatar Aug 05 '19 09:08 HeadBanging

Thanks for noticing that error in the readme!

When you say it doesn't work, what specifically is the problem? Did you remember to add the ble.net-android package to your Android project?

nexussays avatar Aug 07 '19 02:08 nexussays