EliFromToronto
EliFromToronto

Reputation: 81

Bluetooth connection problems

I have a client on a PC and a server on a tablet. I know the MAC addresses for both which means I do not do discoveries.

1. On the client if I use

connectString = "btspp://" + MACaddress + ":4;authenticate=false;encrypt=false;master=false";

It connects fine.

If I change the CN number (4) to anything else, it does not work. How is this number determined?

2. Everything works fine if the tablet is a Samsung with Android 5.0.2 When I use a Qunyico tablet with Android 10, it does not work. I get an error: Failed to connect; [10051] A socket operation was attempted to an unreachable network. What is the problem?

Client on PC – code taken from “Bluetooth-java-client-master”

public class IrcBluetoothClient {

private static void openConnection(String MACaddress) throws IOException {
// Tries to open the connection.
String connectString = "btspp://" + MACaddress + ":4;authenticate=false;encrypt=false;master=false";

StreamConnection connection = (StreamConnection) Connector.open(connectString);
if (connection == null) {
    System.err.println("Could not open connection to address: " + MACaddress);
    System.exit(1);
}

// Initializes the streams.
OutputStream output = connection.openOutputStream();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(isr);

// Starts the listening service for incoming messages.
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(new IncomingMessagesLoggingRunnable(connection));

// Main loop of the program which is not complete yet
LocalDevice localDevice = LocalDevice.getLocalDevice();

while (true) {
    String toSend = reader.readLine();
    byte[] toSendBytes = toSend.getBytes(StandardCharsets.US_ASCII);
    output.write(toSendBytes);
    System.out.println("[" + localDevice.getFriendlyName() + " - " + 
           localDevice.getBluetoothAddress() + "]: " + toSend);

    System.exit(1);
}

Server on tablet – code taken from https://developer.android.com/guide/topics/connectivity/bluetooth

private static final UUID A_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

public BTacceptConnections( BluetoothAdapter mBluetoothAdapter) {
 // Use a temporary object that is later assigned to mmServerSocket 
 // because mmServerSocket is final. 
BluetoothServerSocket tmp = null; 

try { 
    // A_UUID is the app's UUID string, also used by the client code.
    tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, A_UUID);
} catch (IOException e) { 
    Log.e(TAG, "Socket's listen() method failed", e);
} 
mmServerSocket = tmp;
 

// Closes the connect socket and causes the thread to finish.
public void cancel(){
    try {
        mmServerSocket.close();
    }catch (IOException e){
    }
    runFlag = 1;
}

//***********************************************************************************************
//
// This thread runs all the time listening for incoming connections.
//

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned.
    
        while (runFlag == 0) {
            try {
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "Socket's accept() method failed", e);
                break;
            }
    
            if (socket != null) {           // If a connection was accepted
                // A connection was accepted. Perform work associated with
                // the connection in a separate thread.
                // manageMyConnectedSocket(socket);
            }else{
                try {
                   mmServerSocket.close();
                } catch (IOException e) {
                e.printStackTrace();
                } 
           break;
           }
    
         }

Upvotes: 0

Views: 134

Answers (1)

Mr. Panda
Mr. Panda

Reputation: 543

I know the MAC addresses for both which means I do not do discoveries.

Official Linux Bluetooth protocol stack BlueZ uses D-BUS API to establish bluetooth communication. If you check adapter-api, scanning will create device objects that you need to establish a communication which means discovering is not only done to retrieve MAC addresses only.

Your case might be the same, I would suggest doing discovery first.

Upvotes: 1

Related Questions