Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.OS;
- using Android.Support.V7.App;
- using Android.Runtime;
- using Android.Widget;
- using Android.Bluetooth;
- using Android.Util;
- using Android.Content;
- using System.Reflection;
- namespace firstTry3
- {
- [Activity(Label = "@string/app_name")]
- public class bluetoothConnectionActivity : AppCompatActivity
- {
- BluetoothAdapter mBluetoothAdapter;
- const int REQUEST_ENABLE_BT = 1; //necessary to start bluetooth, must be greater than 0
- string tag = "blueApp";
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.activity_bluetoothConnection);
- Button buttonBluetoothOn = FindViewById<Button>(Resource.Id.bluetoothOn);
- Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
- Button buttonDissconnect = FindViewById<Button>(Resource.Id.disconnectButton);
- mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
- buttonBluetoothOn.Click += (sender, e) =>
- {
- if (!mBluetoothAdapter.IsEnabled)
- {
- Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
- }
- enableBluetooth();
- };
- }
- public void enableBluetooth() {
- if(mBluetoothAdapter == null)
- {
- Log.Warn(tag, MethodBase.GetCurrentMethod().Name + ": Device does not have bluetooth capabilities");
- }
- if (!mBluetoothAdapter.IsEnabled)
- {
- Intent enableBTIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
- StartActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
- }
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (resultCode == constants.RESULT_OK)
- {
- Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has been activated");
- Button buttonConnect = FindViewById<Button>(Resource.Id.connectButton);
- buttonConnect.Enabled = true;
- }
- if (resultCode == constants.RESULT_CANCELLED){
- Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": Bluetooth has NOT been activated");
- }
- else
- {
- Log.Error(tag, MethodBase.GetCurrentMethod().Name + ": invalid resultCode");
- }
- }
- }
Add Comment
Please, Sign In to add comment