Listen for keyboard input

Hello,

I'm developing a XF app that takes input from a bluetooth barcode scanner (=external keyboard) and validates the barcode. For now I work with an Entry element and validate the barcode in the 'Completed' event. This works fine, but one of the major drawbacks is that the Entry must have focus at all times.

So I was thinking to implement a DependencyService that listens for keyboard input. But I have no idea how to do that on Android, iOS and WP.

Is there anybody who knows how to capture keyboard input on these platforms, but without using an Entry control?

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think It's difficult to have always the focus on an entry. Can you only use keyboard emulation? I think you can open a "bluetooth socket" and receive data in the socket.

    Something like this (it' only an example).

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            global::Xamarin.Forms.Forms.Init (this, bundle);
    
            LoadApplication (new App ());
    
            BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
            if(adapter == null)
                System.Diagnostics.Debug.WriteLine("No Bluetooth adapter found.");
    
            if(!adapter.IsEnabled)
                System.Diagnostics.Debug.WriteLine("Bluetooth adapter is not enabled."); 
    
            BluetoothDevice device = null;
    
            foreach (var bd in adapter.BondedDevices) {
                if (bd.Name.StartsWith ("QuickScan")) {
                    device = bd;
                    break;
                }
            }
    
            if (device == null)
                System.Diagnostics.Debug.WriteLine ("Named device not found.");
            else {
                BthSocket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
                if (BthSocket != null) {
    
                    Task.Run (async() => {
                        await BthSocket.ConnectAsync ();
    
                        if(BthSocket.IsConnected){
                            System.Diagnostics.Debug.WriteLine("Connected!");
                            var mReader = new InputStreamReader(BthSocket.InputStream);
                            var buffer = new BufferedReader(mReader);
                            while (true){
    
                                string barcode = await buffer.ReadLineAsync();
                                if(barcode.Length > 0){
                                    System.Diagnostics.Debug.WriteLine("Letto: " + barcode);
                                    //Xamarin.Forms.MessagingCenter.Send<App, string> ((App)Xamarin.Forms.Application.Current, "Barcode", barcode);
                                }
                            }
    
                        }
    
                    });
                }
            }
        }  
    

    This is for android...

  • CogeniusCogenius BEMember

    Thanks, I will definitely look into that.

  • Hi - that looks quite promising, did you succeed with this? What about using this in a PCL with iOS, UWP,...?

    Is there a sample application somewhere out there showing how to do that for a PCL using dependency service (or other?!) for these Andoid, iOS, and UPW? That would be very appreciated, as I am a bit confused how to get this working in my PCL app....!

Sign In or Register to comment.