Sending a string via Bluetooth from a PC as client to a mobile as server

假装没事ソ 提交于 2019-11-27 06:34:33

You will need to use the RFComm APIS to make the communication work I have managed to define a class which is a Thread and will be acting as a server and listening for client connections. I have also placed some comments for you to understand.

    private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
    }

    public void run() {         
        BluetoothSocket socket = null;

                    BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

        // Listen to the server socket if we're not connected
        while (true) {

            try {
                // Create a new listening server socket
                Log.d(TAG, ".....Initializing RFCOMM SERVER....");

                // MY_UUID is the UUID you want to use for communication
                mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);                    
                //mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);  you can also try using In Secure connection...

                // This is a blocking call and will only return on a
                // successful connection or an exception                    
                socket = mmServerSocket.accept();                   

            } catch (Exception e) {

            }

            try {
                Log.d(TAG, "Closing Server Socket.....";                    
                mmServerSocket.close();



                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the BluetoothSocket input and output streams

                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();


                mmInStream = new DataInputStream(tmpIn);
                mmOutStream = new DataOutputStream(tmpOut); 

                // here you can use the Input Stream to take the string from the client whoever is connecting
                //similarly use the output stream to send the data to the client
            } catch (Exception e) {
                //catch your exception here
            }

        }
    }

}

I hope this helps

For your another question:

Declaring javax.bluetooth.UUID on Client side (PC) UUID class should be from javax.bluetooth.UUID

   uuidSet2[0] = new UUID("446118f08b1e11e29e960800200c9a66", false);

Declaring java.util.UUID at Server Side (Android)

    UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
TimothyP

I'm not a Java developer but I've had a similar issue with Mono for Android (c#)

The UUID for SPP should be "00001101-0000-1000-8000-00805F9B34FB"
This is a well known UID to identify a Bluetooth SPP adapter.

In my c# code that looks like

private static UUID MY_UUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");

I'm guessing you can update your Java code to something like:

new UUID("00001101-0000-1000-8000-00805F9B34FB", true);

Although I'm not sure what parameters that function accepts, so you may have to check that.

I was using the Android device as a client, but the information may be of use to you,
so I'll include my c# code here which I originally translated from Java samples,
so you should be able to translate it back:

btAdapter = BluetoothAdapter.DefaultAdapter;

btAdapter.CancelDiscovery(); //Always call CancelDiscovery before doing anything
remoteDevice = btAdapter.GetRemoteDevice(Settings["deviceaddress"].ToString());

socket = remoteDevice.CreateRfcommSocketToServiceRecord(MY_UUID);
socket.Connect();

Basically I get the default adapter, cancel any running discovery operations and then create a socket to the other device. In your case you'll want to listen instead of connecting, but just for your information.

I hope it helps, sorry I could not give you more Java specific information.

'Update:' Just found a little sample in Java that more or less follows the same method as what I'm using: Problems with connecting bluetooth SPP in android?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!