Beispiel #1
0
        private void FindBTPrinter()
        {
            string printername = apara.PrinterName.Trim().ToUpper();

            try{
                mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;

                if (mBluetoothAdapter == null)
                {
                    msg = callingActivity.Resources.GetString(Resource.String.msg_bluetoothnofound);
                    return;
                }
                string txt = "";
                if (!mBluetoothAdapter.Enable())
                {
                    Intent enableBluetooth = new Intent(
                        BluetoothAdapter.ActionRequestEnable);
                    StartActivityForResult(enableBluetooth, 0);
                }

                var pair = mBluetoothAdapter.BondedDevices;
                if (pair.Count > 0)
                {
                    foreach (BluetoothDevice dev in pair)
                    {
                        Console.WriteLine(dev.Name);
                        txt = txt + "," + dev.Name;
                        if (dev.Name.ToUpper() == printername)
                        {
                            mmDevice = dev;
                            //							File.WriteAllText(addrfile,dev.Address);
                            break;
                        }
                    }
                }
                msg = callingActivity.Resources.GetString(Resource.String.msg_bluetoothfound) + mmDevice.Name;
            }catch {
                //txtv.Text = ex.Message;
                mmDevice = null;
                msg      = callingActivity.Resources.GetString(Resource.String.msg_bluetoothnofound);
                //AlertShow(ex.Message);
            }finally {
                if (mBluetoothAdapter != null)
                {
                    if (mBluetoothAdapter.IsDiscovering)
                    {
                        mBluetoothAdapter.CancelDiscovery();
                        Thread.Sleep(300);
                    }

                    mBluetoothAdapter.Dispose();
                    mBluetoothAdapter = null;
                }
            }
        }
Beispiel #2
0
        public async Task CloseOutputStream()
        {
            Thread.Sleep(1000);

            await socket.OutputStream.FlushAsync();

            //fecha
            socket.OutputStream.Close();

            socket.Close();

            socket.Dispose();

            socket = null;

            btAdapter.Dispose();


            //aguarda 2s
            Thread.Sleep(1000);
        }
Beispiel #3
0
 private void TryToConnect()
 {
     System.Threading.ThreadPool.QueueUserWorkItem(o => {
         BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
         SetStatusText("acquiring adapter...");
         if (bluetoothAdapter == null)
         {
             SetStatusText("No Bluetooth Adapter found.");
             return;
         }
         else if (!bluetoothAdapter.IsEnabled)
         {
             SetStatusText("BlueTooth Adapter disabled.");
             return;
         }
         SetStatusText("finding Shelly's Garage");
         var device = bluetoothAdapter.BondedDevices.FirstOrDefault(x => x.Name == "Shelly'sGarage");
         if (device == null)
         {
             SetStatusText("You ain't paired with Shelly's Garage.");
             return;
         }
         SetStatusText("connecting...");
         socket = device.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
         try {
             socket.Connect();
             bluetoothAdapter.Dispose();
             SetStatusText("Connected to Shelly's Garage");
             RunOnUiThread(() => {
                 connectorButton.Text = "Disconnect";
             });
         } catch {                //Exception ex) {
             SetStatusText("Connection failt.");
         }
     });
 }