Issue
I'm trying to write app with bluetooth connection. When I got request about turning on bluetooth, I have to choose yes or no, and when I do it then it should show little message like toast, but it doesn't show. I tried to do toast inside function without if still doesn't work, please help
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
if (bluetoothAdapter == null)
{
Toast.MakeText(Application.Context, "xxx ", ToastLength.Short).Show();
}
if (!bluetoothAdapter.IsEnabled)
{
Intent wlaczbt = new Intent(BluetoothAdapter.ActionRequestEnable);
StartActivityForResult(wlaczbt, REQUEST_ENABLE_BT);
//OnActivityResult(REQUEST_ENABLE_BT, Result.Ok, wlaczbt);
}
else
{
Toast.MakeText(Application.Context, "xxx ", ToastLength.Short).Show();
}
OnActivity:
public void OnActivityResult(int requestCode,int resultCode,Intent data)
{
base.OnActivityResult(requestCode, Result.Ok , data);
Toast.MakeText(Application.Context, "xx ", ToastLength.Short).Show();
if (requestCode == REQUEST_ENABLE_BT)
{
if(resultCode.Equals(Result.Ok))
{
Toast.MakeText(Application.Context, "xx ",ToastLength.Short).Show();
}
else
{
Toast.MakeText(Application.Context, "xx ", ToastLength.Short).Show();
}
}
}
Solution
I write a demo about it work as normal. Here is running GIF.
Here is my code.
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
Button button1;
int DISCOVERABLE_DURATION = 300;
int DISCOVERABLE_BT_REQUEST_CODE = 1001;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
button1 = FindViewById<Button>(Resource.Id.button1);
button1.Click += Button1_Click;
}
private void Button1_Click(object sender, System.EventArgs e)
{
Intent discoverableIntent = new Intent(BluetoothAdapter.ActionRequestDiscoverable);
discoverableIntent.PutExtra(BluetoothAdapter.ExtraDiscoverableDuration, DISCOVERABLE_DURATION);
StartActivityForResult(discoverableIntent, DISCOVERABLE_BT_REQUEST_CODE);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
Toast.MakeText(Application.Context, "xx ", ToastLength.Short).Show();
if (requestCode == DISCOVERABLE_BT_REQUEST_CODE)
{
if (resultCode.Equals(Result.Ok))
{
Toast.MakeText(Application.Context, "xx ", ToastLength.Short).Show();
}
else
{
Toast.MakeText(Application.Context, "xx ", ToastLength.Short).Show();
}
}
//base.OnActivityResult(requestCode, resultCode, data);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
I add two permission in AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Here is my demo, you can download it.
https://github.com/851265601/XAndroidBlueTooth
This is a helpful article about it, you can refer to it. https://www.codeproject.com/Articles/814814/Android-Connectivity
Answered By - Leon Lu - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.