Android Bluetooth remote: 1st working (sort of) code
#1
Hi, new user here. Let me know if there's someplace else better suited for this discussion :-)

It has occasionally annoyed me a bit that I've needed to use "mobile hotspot" to control my OSMC. Here's how I've set up a bluetooth connection that can be used to send remote control commands. At the moment it's highly impractical (and doesn't work with Kore), but I hope you'll be able to help me upgrade the solution to something actually useful.

My setup is a phone running Android 8.0 running Kore 2.4.7 and a raspberry 3+ running OSMC 2018.12-1

The main idea is to redirect the JSON-RPC requests from the phone over BT connection, and then on to the interface at the raspberry localhost port 9090. The responses must of course go back in the opposite direction.

At the bottom I have pasted a python script that does the job on the raspberry side. I run it from the commandline over ssh. It uses the SPP profile (the uuid used is the universal uuid for that profile) It works as a proof-of-concept, even if it has lots of issues:
- must run sudo
- must be stopped and restarted if the connection drops
- not super reliable (debugging/testing eventually required)
- the "stop button" consists of running 'sudo killall python' in another terminal
- I couldn't figure out how to deal with stopping threads using the exitFlag variable, see commented-out code at bottom
- Bluetooth scanning must be on before starting (start bluetoothctl, type 'scan on', ctrl-D)
- /etc/systemd/system/dbus-org.bluez.service must be edited for compatibility mode: ExecStart=/usr/libexec/bluetooth/bluetoothd -C
- Requires various packages installed, and I've lost track of what exactly. Maybe try these: python-pip python-dev libbluetooth-dev bluetooth bluez bluez-tools rfkill python-bluez and PyBluez (using pip I think)
- Probably other settings that got changed along the way. I may try to post a verified recipe for installation from scratch at some point.

I hope you've got good tips on how to improve things (or even better, tested opensource code that does the trick).

On the phone side, I've found various apps that can work. Use either of them to set up the connection, and paste JSON-RPC calls to control Kodi. My favorite test is {"jsonrpc": "2.0", "method": "Input.Down", "id": 1} which is down-arrow on the remote. Now, technically, you have a bluetooth remote control, just not very userfriendly ;-)

Serial bluetooth terminal
https://play.google.com/store/apps/detai...l&hl=en_US
https://github.com/kai-morich/SimpleBluetoothTerminal

Bluetooth terminal
https://github.com/Sash0k/bluetooth-spp-terminal

My favourite at the moment is probably Googles own sample code for chat over bluetooth
https://github.com/googlesamples/android-BluetoothChat
(This one requires that you change the UUIDs in BluetoothChatService.java to match the python script running ton the raspberry or vice versa. If you change UUIDs on the raspberry side, other programs that try the standard SPP profile will not work).

My original idea was to use an android program that listens on a tcp port on the phone, and then redirects the calls to the bluetooth link. Then Kore could simply connect to that port and Bob's our uncle. I actually found such a program (unfortunately not open source):

https://play.google.com/store/apps/detai...idge&hl=en

The program works: In termux on the phone I run 'telnet 127.0.0.1 6785' and I can now paste JSON and get responses. Unfortunately I can't connect Kore properly. The 'add media center' testing fails without a http connection. When I insert port 6785 as the http port, I can see a http request arrive at the TCP interface:

POST /jsonrpc HTTP/1.1
Content-Type: application/json; charset=utf-8
Content-Length: 49
Host: 127.0.0.1:6785
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/2.3.0

{"jsonrpc":"2.0","method":"JSONRPC.Ping","id":18}HTTP/1.1 400 Bad Request

Which path would you suggest to make things work on the Android side? (By "working" i mean open source code that can eventually be distributed to ordinary users).

Python script for raspberry:
from __future__ import print_function
import bluetooth
import socket
import time
import threading

exitFlag = 0

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 0 # automatically choose port
server_sock.bind(("",port))
server_sock.listen(1)
uuid = "00001101-0000-1000-8000-00805F9B34FB"

bluetooth.advertise_service( server_sock, "FooBar Service", uuid )

phone_sock,address = server_sock.accept()
print(("Accepted connection from ",address))



kodi_sock = socket.socket(socket.AF_INET)
kodi_sock.connect(("127.0.0.1", 9090))


class sockPlugger (threading.Thread):
    def __init__(self, threadID, sockin, sockout):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.sockin = sockin
        self.sockout = sockout
      
    def run(self):
        print(("starting ", self.threadID))
        while 1:
            if exitFlag: break
            data = self.sockin.recv(1024)
            if not data: continue
            print(data, end="")
            self.sockout.send(data)
        print(("exiting thread", self.threadID))
        
      
threadfromphone = sockPlugger(1, phone_sock, kodi_sock)
threadfromkodi = sockPlugger(2, kodi_sock, phone_sock)

threadfromphone.start()
threadfromkodi.start()

##print("sleeping")
##time.sleep(15)
##print("exiting")
##exitFlag = 1
##print("or what?")
Reply
#2
That's a bit convoluted, but it should work.

On the Android side, do you have any idea why you're getting the 400 Bad request? It's a simple HTTP request that is being sent. Try changing the ports, and in Kore, disable the "Use TCP" and "Use EventServer" options, as those need other 2 ports.
Reply

Logout Mark Read Team Forum Stats Members Help
Android Bluetooth remote: 1st working (sort of) code0