I am trying to Do similar to the below code in c# using an Activity to update the list of scanned Bluetooth devices from Broadcast receiver.
But I have my broadcast receiver as a separate class So I was not able to add the scanned devices on to the list.
Can you suggest me any better approach using c# xamarin.
I was trying to communicate to activity from a broadcast receiver
public class DeviceListFragment extends Fragment implements AbsListView.OnItemClickListener{
...
private final BroadcastReceiver bReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Create a new device item
DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
// Add it to our adapter
mAdapter.add(newDevice);
}
}
};
}
This my code which i tried.
namespace Launch {
[Activity(Label = "Launch", MainLauncher = true)]
public class MainActivity : Activity
{
string[] items;
BluetoothAdapter bluetoothadapter;
ListView listView;
//BroadcastReceiver broadcast=null;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
items = new string[] { "wec","wce","dddsa","wecwe"};
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
listView = (ListView) FindViewById<ListView>(Resource.Id.devices_lstv);
ArrayAdapter arrayAdapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleExpandableListItem1,items);
listView.Adapter=arrayAdapter;
RegisterReceiver(new BTimeReceiver(this), new IntentFilter(BluetoothDevice.ActionFound));
}
class BTimeReceiver : BroadcastReceiver
{
MainActivity activity;
public BTimeReceiver(MainActivity activity) : base()
{
this.activity = activity;
}
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received bluetooth device!", ToastLength.Short).Show();
BluetoothDevice device = (Android.Bluetooth.BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
Toast.MakeText(context, device.Name, ToastLength.Short).Show();
// access your service via the "service" var...
}
}
}
}
This is the error from the beginning
Java.Lang.ClassNotFoundException: Didn't find class "md5595308ae13c09d60df4f8b95233e7c94.MainActivity_BTimeReceiver" on path: DexPathList[[zip file "/data/app/Launch.Launch-1/base.apk"],nativeLibraryDirectories=[/data/app/Launch.Launch-1/lib/arm, /data/app/Launch.Launch-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]] at Java.Interop.JniEnvironment+Types.FindClass (System.String classname) [0x00114] in <54816278eed9488eb28d3597fecd78f8>:0 at Java.Interop.JniType..ctor (System.String classname) [0x00006] in <54816278eed9488eb28d3597fecd78f8>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods..ctor (System.Type declaringType) [0x00064] in <54816278eed9488eb28d3597fecd78f8>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.GetConstructorsForType (System.Type declaringType) [0x0002c] in <54816278eed9488eb28d3597fecd78f8>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.StartCreateInstance (System.String constructorSignature, System.Type declaringType, Java.Interop.JniArgumentValue* parameters) [0x00032] in <54816278eed9488eb28d3597fecd78f8>:0 at Android.Content.BroadcastReceiver..ctor () [0x00034] in :0 at Launch.MainActivity+BTimeReceiver..ctor (Launch.MainActivity mainActivity) [0x00000] in C:\Users\ACHINTAR\source\repos\Launch\Launch\MainActivity.cs:73 at Launch
[BroadcastReceiver(Enabled = true)]attribute. - Eren Shen