Slow Bluetooth data transfer between Raspberry Pi and Java application

Using some examples found on the Internet, I can communicate between my raspberry Pi and a laptop with Bluetooth using the code below. However, I notice that immediately after connecting, it takes a long time (> 30 seconds) to transfer data. In the end, it seems to accelerate to a reasonable amount of time, but the answer seems unreliable. If this is normal, I was wondering if anyone could explain why this is happening. If this is not normal, any suggestions on a better way to continue will be appreciated. Thanks.

Java code

import java.io.*; import javax.microedition.io.*; import javax.bluetooth.*; public class RFCOMMServer { public static void main( String args[] ) { //display local device address and name LocalDevice localDevice; try { localDevice = LocalDevice.getLocalDevice(); System.out.println("Address: "+localDevice.getBluetoothAddress()); System.out.println("Name: "+localDevice.getFriendlyName()); } catch (BluetoothStateException e1) { e1.printStackTrace(); } try { startServer(); } catch (IOException e) { e.printStackTrace(); } } private static void startServer() throws IOException{ //Create a UUID UUID uuid = new UUID("1101", true); //Create the servicve url String connectionString = "btspp://localhost:" + uuid +";name=SampleServer"; //open server url StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString ); //Wait for client connection System.out.println("\nServer Started. Waiting for clients to connect..."); StreamConnection conn=streamConnNotifier.acceptAndOpen(); RemoteDevice dev = RemoteDevice.getRemoteDevice(conn); System.out.println("Remote device address: "+dev.getBluetoothAddress()); System.out.println("Remote device name: "+dev.getFriendlyName(true)); InputStream is = conn.openInputStream(); byte buffer[] = new byte[80]; Boolean closeConnection = false; while(!closeConnection){ try{ int bytes_read = is.read( buffer ); String received = new String(buffer, 0, bytes_read); if(received.equalsIgnoreCase("test")){ OutputStream outStream=conn.openOutputStream(); PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream)); pWriter.write("received test"); pWriter.flush(); pWriter.close(); }else System.out.println(received); }catch(StringIndexOutOfBoundsException exc){ System.out.println("Exception"); conn.close(); System.exit(0); } } conn.close(); } } 

Python code

 #!/usr/bin/python2.7 import sys import bluetooth from bluetooth import * name = "SampleServer" service_matches = bluetooth.find_service(name) if len(service_matchces) == 0 print "couldn't find the server" sys.exit(0) first_match = service_matches[0] port = first_match["port"] print "port: "+str(port) name = first_match["name"] print "name: "+str(name) host = first_match["host"] print "host: "+host print "connecting to \"%s\" in "%s" % (name, host) sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) sock.connect((host, port)) sock.send("test") data = sock.recv(80) print "received: ", data print "connected. type stuff" while True: data = raw_input() if len(data) == 0: break sock.send(data) sock.close() 
+4
source share

Source: https://habr.com/ru/post/1498881/


All Articles