Exemplo n.º 1
0
        /// <summary>
        /// Accepts data from an NFC touch and performs a search for any printers with matching information.
        /// </summary>
        /// <param name="intent"> intent that contains NFC payload data </param>
        private void ProcessNfcScan(Intent intent)
        {
            IParcelable[] scannedTags = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
            if (scannedTags != null && scannedTags.Length > 0)
            {
                NdefMessage msg          = (NdefMessage)scannedTags[0];
                byte[]      payloadBytes = msg.GetRecords()[0].GetPayload();
                String      payload      = String.Empty;
                foreach (byte b in payloadBytes)
                {
                    payload += Convert.ToChar(b);
                }
                NfcDevice nfcDevice = new NfcDevice
                {
                    FriendlyName = GetDeviceFriendlyName(payload),
                    MacAddress   = GetDeviceMacAddress(payloadBytes),
                    PIN          = GetDevicePIN(payload).Value
                };
                if (IsDevicePaired(nfcDevice.FriendlyName))
                {
                    ShowAlreadyPaired(nfcDevice.FriendlyName);
                }
                else
                {
                    StartSearching(nfcDevice.FriendlyName);
                    RunOnUiThread(() =>
                    {
                        try
                        {
                            if (bluetoothAdapter == null)
                            {
                                throw new Exception("No Bluetooth adapter found.");
                            }

                            if (!bluetoothAdapter.IsEnabled)
                            {
                                throw new Exception("Bluetooth adapter is not enabled.");
                            }

                            PairDevice(nfcDevice);
                        }
                        catch (DiscoveryException e)
                        {
                            Console.WriteLine(e.ToString());
                            Console.Write(e.StackTrace);
                        }
                        finally
                        {
                        }
                    });
                }

                intent.RemoveExtra(NfcAdapter.ExtraNdefMessages);
            }
        }
Exemplo n.º 2
0
        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();
            }
        }