How use a Bluetooth keyboard and Mouse with Annex

Place your projects here
Post Reply
User avatar
cicciocb
Site Admin
Posts: 2784
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 592 times
Been thanked: 1990 times
Contact:

How use a Bluetooth keyboard and Mouse with Annex

Post by cicciocb »

Hi all,
starting with the version 1.70.1, the BLE (Bluetooth Low Energy) is also supported into Client mode.
This gives the possibility to connect external devices such as keyboard / Mouse.

I'll just give a little example on how it is easy to use a BLE keyboard .

First of all, you need the MAC address of your external keyboard, in the example below, you can find it into the wlog window as the program begins with a scan.
You must just put is into the command at line 6

Code: [Local Link Removed for Guests]

bluetooth.client "10:a2:04:85:08:51", "1812", "2A4D"
The strings "1812" and "2A4D" are the typical values used by the keyboards / mouses (I should say mice).

Code: [Local Link Removed for Guests]

bluetooth.clear
onbluetooth ble_receive
bluetooth.scan 5
pause 6000
' this is the mac address of the keyboard / mouse
bluetooth.client "10:a2:04:85:08:51", "1812", "2A4D"
wait

ble_receive:
'print bluetooth.len, bluetooth.status
select case bluetooth.status
  case 1
    wlog "------>Device Connected"
  case 2
    wlog "======<Device Disconnected"   
  case 4
    a$ = bluetooth.scanresult$
    wlog a$
  case 5
    bluetooth.READ_IOBUFF(0)
    wlog IOBUFF.toHex$(0)
end select
return
In my particular case, I have a cheap combo keyboard / mouse like this :
image.png
By chance this is also BLE (as usually they are only RF and coupled with a USB dongle) so It works for me.
You'll see an HEX string shown in the console when a key is pressed (or the mouse moved).

As the format of the data received can be different from device to device, I just did a little variant of this code using a library included into an external file; this library simply returns the key pressed from the keycode received.
The code in the library can changed and adapted in function of your keyboard and your layout

Code: [Local Link Removed for Guests]

import "/scancode.inc" ' external library
bluetooth.clear
onbluetooth ble_receive
bluetooth.scan 5
pause 6000
' this is the mac address of the keyboard
bluetooth.client "10:a2:04:85:08:51", "1812", "2A4D"
wait

ble_receive:
'print bluetooth.len, bluetooth.status
select case bluetooth.status
  case 1
    wlog "------>Device Connected"
  case 2
    wlog "======<Device Disconnected"   
  case 4
    a$ = bluetooth.scanresult$
    wlog a$
  case 5
    bluetooth.READ_IOBUFF(0)
    'wlog IOBUFF.toHex$(0)
    ret$ = ""
    scancode 0, ret$
    if ret$<> "" wlog ret$;
end select
return

And this is the "library" scancode.inc

Code: [Local Link Removed for Guests]

sub scancode(iobuff_num, ret$)
  local modifiers, code
  if (iobuff.len(0) <> 8) exit sub
  modifiers = iobuff.read(iobuff_num, 0)
  code = iobuff.read(iobuff_num, 2)
  select case code
    case 4: ret$ = "a"
    case 5: ret$ = "b"
    case 6: ret$ = "c"
    case 7: ret$ = "d"
    case 8: ret$ = "e"
    case 9: ret$ = "f"
    case 10: ret$ = "g"
    case 11: ret$ = "h"
    case 12: ret$ = "i"
    case 13: ret$ = "j"
    case 14: ret$ = "k"
    case 15: ret$ = "l"
    case 16: ret$ = "m"
    case 17: ret$ = "n"
    case 18: ret$ = "o"
    case 19: ret$ = "p"
    case 20: ret$ = "q"
    case 21: ret$ = "r"
    case 22: ret$ = "s"
    case 23: ret$ = "t"
    case 24: ret$ = "u"
    case 25: ret$ = "v"
    case 26: ret$ = "w"
    case 27: ret$ = "x"
    case 28: ret$ = "y"
    case 29: ret$ = "z"
    case 40: ret$ = "OK"
    case 41: ret$ = "ESC"
    case 42: ret$ = "BACK"
    case 43: ret$ = "TAB"
    case 44: ret$ = " "
  end select
  ' if shift pressed
  if (modifiers and 2) ret$ = ucase$(ret$)
  if ret$ <> "" exit sub
  
  if (modifiers and 2) = 0 then
    select case code
      case 30: ret$ = "1"
      case 31: ret$ = "2"
      case 32: ret$ = "3"
      case 33: ret$ = "4"
      case 34: ret$ = "5"
      case 35: ret$ = "6"
      case 36: ret$ = "7"
      case 37: ret$ = "8"
      case 38: ret$ = "9"
      case 39: ret$ = "0"
        
      case 45: ret$ = "-"
      case 46: ret$ = "="
      case 47: ret$ = "["
      case 48: ret$ = "]"
      case 49: ret$ = "\\"

      case 51: ret$ = ";"
      case 52: ret$ = "'"
      case 53: ret$ = "`"
      case 54: ret$ = ","
      case 55: ret$ = "."
      case 56: ret$ = "/"
    end select
  else
    select case code
      case 30: ret$ = "!"
      case 31: ret$ = "@"
      case 32: ret$ = "#"
      case 33: ret$ = "$"
      case 34: ret$ = "%"
      case 35: ret$ = "^"
      case 36: ret$ = "&"
      case 37: ret$ = "*"
      case 38: ret$ = "("
      case 39: ret$ = ")"
        
      case 45: ret$ = "_"
      case 46: ret$ = "+"
      case 47: ret$ = "{"
      case 48: ret$ = "}"
      case 49: ret$ = "|"

      case 51: ret$ = ":"
      case 52: ret$ = """"
      case 53: ret$ = "~"
      case 54: ret$ = "<"
      case 55: ret$ = ">"
      case 56: ret$ = "?"
    end select
  end if
  if ret$ <> "" exit sub
  
  select case code
    case 57: ret$ = "CAPS"
    case 58: ret$ = "F1"
    case 59: ret$ = "F2"
    case 60: ret$ = "F3"
    case 61: ret$ = "F4"
    case 62: ret$ = "F5"
    case 63: ret$ = "F6"
    case 64: ret$ = "F7"
    case 65: ret$ = "F8"
    case 66: ret$ = "F9"
    case 67: ret$ = "F10"
    
    case 74: ret$ = "HOME"
    case 75: ret$ = "PGUP"
    case 76: ret$ = "DEL"
    case 77: ret$ = "END"
    case 78: ret$ = "PGDN"
    case 79: ret$ = "RIGHT"
    case 80: ret$ = "LEFT"
    case 81: ret$ = "DOWN"
    case 82: ret$ = "UP"
    
  end select
  if ret$ = "" then 
    if (code <> 0) then ret$ = str$(code)
  end if
  
end sub
You do not have the required permissions to view the files attached to this post.
Post Reply