Android Question Check if external keyboard is connected

padvou

Active Member
Licensed User
Longtime User
Hello,
is it possible for an application to check if an external keyboard for example a barcode scanner via USB is connected? I mean already connected. I can detect the USB attach/detach, so I 'm interested in a device that's already connected when the application starts.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Dim p As Phone
If p.SdkVersion > 15 Then
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim inptmgr As JavaObject = ctxt.RunMethod("getSystemService", Array(ctxt.GetField("INPUT_SERVICE")))
   Dim inputs() As Int = inptmgr.RunMethod("getInputDeviceIds", Null)
   For Each i As Int In inputs
       Dim device As String = inptmgr.RunMethodJO("getInputDevice", Array(i)).RunMethod("getName", Null)
       Log(device)
   Next
End If
It should detect any connected input.

Based on: https://stackoverflow.com/a/29505004/971547
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Try this code:
B4X:
Dim p As Phone
If p.SdkVersion > 15 Then
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim inptmgr As JavaObject = ctxt.RunMethod("getSystemService", Array(ctxt.GetField("INPUT_SERVICE")))
   Dim inputs() As Int = inptmgr.RunMethod("getInputDeviceIds", Null)
   For Each i As Int In inputs
       Dim device As String = inptmgr.RunMethodJO("getInputDevice", Array(i)).RunMethod("getName", Null)
       Log(device)
   Next
End If
It should detect any connected input.

Based on: https://stackoverflow.com/a/29505004/971547

Thank you, it works great detecting USB connected input.
Why doesn't it work with bluetooth connected input and USB connected keyboard?
 
Upvote 0
Top