public void Pair(string name)
        {
            BluetoothDevice unbonded = this.UnBondedDevices.FirstOrDefault(device => device.Name == name);
            // Status is failed by default
            // TODO - check if needed another for BLE
            BTPairOperationStatus status = new BTPairOperationStatus()
            {
                Name = name,
            };

            try {
                if (unbonded != null)
                {
                    if (unbonded.CreateBond())
                    {
                        status.IsSuccessful = true;
                        status.PairStatus   = BT_PairingStatus.Paired;
                    }
                    else
                    {
                        status.PairStatus = BT_PairingStatus.AuthenticationFailure;
                    }
                }
                else
                {
                    status.PairStatus = BT_PairingStatus.NoParingObject;
                }
            }
            catch (Exception e) {
                this.log.Exception(9999, "PairgAsync", "", e);
                status.PairStatus = BT_PairingStatus.Failed;
            }

            this.BT_PairStatus?.Invoke(this, status);
        }
        /**
         * Método para vincular un dispositivo BT al dispositivo móvil
         * **/
        public void bondedDevice(BluetoothDevice device)
        {
            bluetoothAdapter.CancelDiscovery(); //para abrir conexion hay que parar https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect()

            bool res = device.CreateBond();

            Thread.Sleep(10000);
        }
Exemple #3
0
        /// <summary>
        /// Creates the bond with the device specified in address.
        /// </summary>
        /// <param name="address">Address of the device to create bond.</param>
        public void CreateBond(string address)
        {
            BluetoothDevice device = _btAdapter.GetRemoteDevice(address);

            try {
                device.CreateBond();
            } catch {
            }
        }
        public static void PairHC(Activity activity, BluetoothDevice device)
        {
            //Tell the user we found an HC-05
            var status = activity.FindViewById <TextView>(Resource.Id.textView2);

            status.Append("\nFound Possible Skimmer; Connecting...");
            activity.FindViewById <ScrollView>(Resource.Id.scrollView1).FullScroll(Android.Views.FocusSearchDirection.Down);

            //Connect to it
            device.CreateBond();
        }
Exemple #5
0
 /// <summary>
 /// Pairs the device if needed
 /// </summary>
 /// <param name="dev">Bluetooth device</param>
 public static void pairDevice(BluetoothDevice dev)
 {
     if (!(dev.BondState == Bond.Bonded))
     {
         Log.Debug(TAG + "-pairDevice", "Attempting to pair device");
         dev.CreateBond();
     }
     else
     {
         Log.Debug(TAG + "-pairDevice", "Device Already Paired");
     }
 }
Exemple #6
0
        public bool Connect(Android.Content.Context context, Button btConnect, out BluetoothSocket socket, out string message)
        {
            message = string.Empty;
            if (btConnect != null)
            {
                btConnect.Text = "Connecting..";
            }
            socket = null;
            //BluetoothDevice device = null;
            string result = string.Empty;

            try
            {
                Java.Util.UUID id = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");

                BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
                adapter.StartDiscovery();
                //BluetoothManager btm = (BluetoothManager)context.GetSystemService(Context.BluetoothService);

                if (adapter.BondedDevices.Count(x => x.Name == "HC-06") == 0)
                {
                    adapter        = null;
                    message        = "HC-06 device is not paired";
                    btConnect.Text = "Connect";
                    return(false);
                }

                BluetoothDevice k9BTdevice = adapter.BondedDevices.First(x => x.Name == "HC-06");
                socket = k9BTdevice.CreateRfcommSocketToServiceRecord(id);

                try
                {
                    k9BTdevice.SetPairingConfirmation(false);
                    //   k9BTdevice.Dispose();
                    k9BTdevice.SetPairingConfirmation(true);
                    btConnect.Text = "Connect";
                }
                catch (Exception ex) { };
                k9BTdevice.CreateBond();
                //context.ApplicationContext.
                socket.Connect();
                btConnect.Text = "Connected";
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(false);
            }

            return(true);
        }
 public static void With(BluetoothDevice device, BondCreated onSuccess)
 {
     device.BondCreated += (s, e) =>
     {
         if (e.Result == BluetoothError.None)
         {
             var pair = new Pair
             {
                 Device = e.Device,
             };
             onSuccess(pair);
         }
     };
     device.CreateBond();
 }
Exemple #8
0
        private static Intent PairDevice(NFCDevice nfcDevice, Intent intent)
        {
            BluetoothDevice device = _BluetoothAdapter.GetRemoteDevice(nfcDevice.MacAddress);

            if (device != null)
            {
                OnDevicePaired(device, device.CreateBond(), intent, EventArgs.Empty);
            }
            else
            {
                intent.PutExtra("ErrorMessage", "Failed to communicate with device");
            }

            return(intent);
        }
Exemple #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="nfcDevice"></param>
        private void PairDevice(string friendlyName, string macAddress)
        {
            BluetoothDevice device = _activity.Bluetooth.Adapter.GetRemoteDevice(macAddress);

            if (device != null)
            {
                if (device.CreateBond())
                {
                    OnDevicePaired(device, EventArgs.Empty);
                }
            }
            else
            {
                Toast.MakeText(Activity, string.Format("{0} {1}", GetString(Resource.String.MainActivity_PairFailed), friendlyName), ToastLength.Long).Show();
            }
        }
Exemple #10
0
        public bool Connect()
        {
            if (!adapter.IsEnabled)
            {
                BluetoothDisabledEvent.Invoke(this, new EventArgs());
                return(false);
            }

            adapter.StartDiscovery();

            try
            {
                device = adapter.BondedDevices.Where(x => x.Name == "HC-06").FirstOrDefault();
                device.SetPairingConfirmation(false);
                device.Dispose();
                device.SetPairingConfirmation(true);
                device.CreateBond();
            }
            catch (Exception exception)
            {
                Log.Debug(TAG, exception.ToString());
            }

            adapter.CancelDiscovery();

            socket = device.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));

            try
            {
                socket.Connect();
                listenThread = new Thread(Listener);
                if (listenThread.IsAlive == false)
                {
                    listenThread.Start();
                }
            }
            catch (Exception exception)
            {
                Toast.MakeText(Application.Context, "Cannot connect to HC-06.", ToastLength.Short).Show();
                Log.Debug(TAG, exception.ToString());
                return(false);
            }

            return(true);
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.layout_sync_bluetooth, container, false);

            string possible = BluetoothHelper.isPossible();

            if (possible != "")
            {
                view.FindViewById <TextView>(Resource.Id.sb_text).Text = possible;
                return(view);
            }

            view.FindViewById <TextView>(Resource.Id.sb_text).Text = "Dein GeräteName: " + BluetoothHelper.getDeviceName();

            ListView list = view.FindViewById <ListView>(Resource.Id.sb_list);

            list.Adapter           = new ArrayAdapter <string>(Context, Resource.Layout.sb_listitem, BluetoothHelper.getAvaiableDeviceNames());
            list.TextFilterEnabled = true;
            list.ItemClick        += (sender, e) =>
            {
                BluetoothDevice device = BluetoothHelper.GetDeviceByName(((TextView)e.View).Text);


                if (device != null)
                {
                    BluetoothHelper.GetAdapter().CancelDiscovery();
                    Console.WriteLine(device.GetType().ToString());
                    device.CreateBond();
                    Type       t    = device.GetType();
                    MethodInfo info = t.GetMethod("CreateRfcommSocket");
                    object     o    = info.Invoke(device, new object[] { 1 });
                    //		Name	"CreateRfcommSocketToServiceRecord"	string

                    var socket = (BluetoothSocket)o;
                    //var socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
                    socket.Connect();

                    BTDeviceArgs args = new BTDeviceArgs();
                    args.socket = socket;
                    finished(this, args);
                }
            };

            return(view);
        }
        public void Connetti(BluetoothDevice device)
        {
            Android.Bluetooth.Bond b = device.BondState;
            if ((b == Bond.Bonded))// | (b==Bond.None))
            {
                BluetoothGatt g = device.ConnectGatt(this.context, false, this);

                /*
                 * Java.Lang.Reflect.Method m = g.Class.GetMethod("refresh", new Java.Lang.Class[0]);
                 * if (m != null)
                 * {
                 *  m.Invoke(g, new Java.Lang.Object[0]);
                 * }
                 */
            }
            else
            {
                // Richidedere permessi per Android 6.0
                // ActivityCompat.RequestPermissions(activity, new String[] { Manifest.Permission.AccessFineLocation, Manifest.Permission.AccessCoarseLocation}, 1001);


                this.myReceiver = new BondStatusBroadcastReceiver();
                this.myReceiver.Bounded_ConnectDevice_Event       += this.MyReceiver_Bounded_ConnectDevice_Event;
                this.myReceiver.Bounded_ConnectDevice_Event_ERROR += this.MyReceiver_Bounded_ConnectDevice_Event_ERROR;

                IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ActionFound);

/*
 *              intentFilter.AddAction(BluetoothDevice.ActionNameChanged);
 *              intentFilter.AddAction(BluetoothAdapter.ActionDiscoveryStarted);
 *              intentFilter.AddAction(BluetoothAdapter.ActionDiscoveryFinished);
 *              intentFilter.AddAction(BluetoothAdapter.ActionStateChanged);
 *
 */
                intentFilter.AddAction(BluetoothDevice.ActionBondStateChanged);
                context.RegisterReceiver(this.myReceiver, intentFilter);

                //BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
                //adapter.StartDiscovery();

                device.CreateBond();
            }
        }
        private void PairDevice(NfcDevice nfcDevice)
        {
            BluetoothDevice device = bluetoothAdapter.GetRemoteDevice(nfcDevice.MacAddress);

            if (device != null)
            {
                if (device.CreateBond())
                {
                    OnDevicePaired(device, EventArgs.Empty);
                }
                else
                {
                    Toast.MakeText(context, string.Format("{0} {1}", GetString(Resource.String.MainActivity_PairFailed), nfcDevice.FriendlyName), ToastLength.Long).Show();
                }
            }
            else
            {
                Toast.MakeText(context, string.Format("{0} {1}", GetString(Resource.String.MainActivity_PairFailed), nfcDevice.FriendlyName), ToastLength.Long).Show();
            }
        }