Guest User

Untitled

a guest
Dec 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using Android.App;
  2. using Android.OS;
  3. using Android.Support.V7.App;
  4. using Android.Runtime;
  5. using Android.Widget;
  6. using Android.Bluetooth;
  7. using Android.Util;
  8.  
  9. using Android.Content;
  10. using System.Reflection;
  11.  
  12. namespace firstTry3
  13. {
  14. [Activity(Label = "@string/app_name")]
  15. public class bluetoothConnectionActivity : AppCompatActivity
  16. {
  17. BluetoothAdapter mBluetoothAdapter;
  18. const int REQUEST_ENABLE_BT = 1; //necessary to start bluetooth, must be greater than 0
  19. string tag = "blueApp";
  20.  
  21.  
  22. protected override void OnCreate(Bundle bundle)
  23. {
  24. base.OnCreate(bundle);
  25. // Set our view from the "main" layout resource
  26. SetContentView(Resource.Layout.activity_bluetoothConnection);
  27.  
  28. Button buttonBluetoothOn = FindViewById<Button>(Resource.Id.bluetoothOn);
  29. Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
  30. Button buttonDissconnect = FindViewById<Button>(Resource.Id.disconnectButton);
  31.  
  32. mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
  33.  
  34. buttonBluetoothOn.Click += (sender, e) =>
  35. {
  36. if (!mBluetoothAdapter.IsEnabled)
  37. {
  38. Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
  39. }
  40. enableBluetooth();
  41. };
  42. }
  43.  
  44. public void enableBluetooth() {
  45. if(mBluetoothAdapter == null)
  46. {
  47. Log.Warn(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
  48. }
  49. if (!mBluetoothAdapter.IsEnabled)
  50. {
  51. Intent enableBTIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
  52. StartActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
  53. }
  54. }
  55.  
  56. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  57. {
  58. if (resultCode == constants.RESULT_OK)
  59. {
  60. Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
  61. Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
  62. buttonConnect.Enabled = true;
  63.  
  64. }
  65. if (resultCode == constants.RESULT_CANCELLED){
  66. Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has NOT been activated");
  67. }
  68. else
  69. {
  70. Log.Error(tag, MethodBase.GetCurrentMethod().Name + ": invalid resultCode");
  71. }
  72. }
  73. }
Add Comment
Please, Sign In to add comment