21

I am trying to send a file over bluetooth in an android device. I have done discovery, connection and have made a bluetooth socket. Problem is when i am writing the byte array in the output stream of the bluetooth socket, the recieving side does not receive anything although it accept that something is being sent.

Here's what Iam doing (bad is the bluetooth adaptor)

Please advise.

try
    {
        BluetoothDevice dev = bad.getRemoteDevice(a);
        bad.cancelDiscovery();

        dev.createRfcommSocketToServiceRecord(new UUID(1111, 2222));
        Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
        bs = (BluetoothSocket) m.invoke(dev, Integer.valueOf(1));
        bs.connect();
        tmpOut = bs.getOutputStream();
    }catch(Exception e)
    {

    }

    File f = new File(filename);

    byte b[] = new byte[(int) f.length()];
    try
    {
        FileInputStream fileInputStream = new FileInputStream(f);
        fileInputStream.read(b);
    }catch(IOException e)
    {
        Log.d(TAG, "Error converting file");
        Log.d(TAG, e.getMessage());
    }

    try {
        tmpOut.write(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
exorcist
  • 211
  • 2
  • 4
  • What do you do this for: dev.createRfcommSocketToServiceRecord(new UUID(1111, 2222));? You create a BluetoothSocket using an UUID without using it. Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); bs = (BluetoothSocket) m.invoke(dev, Integer.valueOf(1)); Opens a BluetoothSocket on RfComm Channel 1. So only if the device you are trying to send a file to is listening on this channel, you will be able to recieve the file – jobnz Jul 16 '12 at 10:52
  • OK I removed that line now but still does not work. I launched the debugger mode and it shows me that my tmpOut (outputstream) is null. Soes this mean a problem with my bluetooth socket? Also does a device listens on RFComm channel 1 by default or do I need to have a receiver too in the receiver device? I wanted to just send a file that could be received by the default bluetooth service of another phone. – exorcist Jul 16 '12 at 11:10
  • 1
    Common applications like file transfer are specified in so called Bluetooth Profiles (http://en.wikipedia.org/wiki/Bluetooth_profile) so if you want to transfer a file using the "default bluetooth service" you have to do it according to the OBEX profile which is used to transer files (http://en.wikipedia.org/wiki/OBEX) – jobnz Jul 16 '12 at 11:17
  • I think maybe the way I posted question was a bit bad. I basically want to send file over bluetooth without using the inbuilt Android Intent. How can I achieve this? – exorcist Jul 16 '12 at 11:23
  • http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices - this should answer all your questions then - EDIT: and this: http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection – jobnz Jul 16 '12 at 11:26
  • Thanks a lot. I did what is being told. and I am getting the value of my outputstream as null : BluetoothDevice dev = bad.getRemoteDevice(a); bad.cancelDiscovery(); Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); bs = (BluetoothSocket) m.invoke(dev, Integer.valueOf(1)); bs.connect(); BufferedOutputStream(x)); tmpOut = bs.getOutputStream() Can you tell any possible reason why tmpOut is null? – exorcist Jul 16 '12 at 11:35
  • You should read the links i posted more carefully. There is nothing about opening a BluetoothSocket directly on a channel. There is an example using uuids. Try the code, it should work. – jobnz Jul 16 '12 at 11:58
  • You should try using tmpOut.flush() I find that sometimes it doesn't send until it is flushed out – FabianCook Aug 15 '12 at 00:51

3 Answers3

3

I am using the below code snipped to connect to the serial service in a remote Bluetooth device and it is working fine for me. Just make sure that the other device (can be mobile or PC) has a server socket for serial communication over Bluetooth (see the server side code below)

Client Side:

UUID serialUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothDevice btDevice = btAdapter.getRemoteDevice(BTAddress); // Get the BTAddress after scan
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID);
btSocket.connect();
InputStream iStream = btSocket.getInputStream();
OutputStream oStream = btSocket.getOutputStream();

Server Side:

UUID serialUUID = new UUID("1101", true);
String serviceURL = "btspp://localhost:" + serialUUID
        + ";name=Android BT Server;authorize=false;authenticate=false";
StreamConnectionNotifier connectionNotifier = (StreamConnectionNotifier) Connector
                        .open(serviceURL);
// Blocking method will wait for client to connect
StreamConnection connection = connectionNotifier.acceptAndOpen();

RemoteDevice remoteDevice = RemoteDevice.getRemoteDevice(connection);
InputStream btInput = connection.openInputStream();
OutputStream btOutput = connection.openOutputStream();
iTech
  • 18,192
  • 4
  • 57
  • 80
  • What if I want to send data to Bluetooth Printer Adapter (e.g. HP bt500), where could I write Server Side code? – Farid Feb 16 '17 at 11:26
1

Why not use the standard api call instead of calling through reflection, eg:

BluetoothSocket socket =  destination
                              .createRfcommSocketToServiceRecord(new UUID(...)) ;

Also your catch block is empty. Are you sure the socket was connected without any exception? Connect will throw IOException if the connection failed for some reason. See this link

Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29
vpathak
  • 1,133
  • 12
  • 12
0

It might be because dev and bs go out of scope before tmpout is used because they are declared within your try/catch block.

bill davis
  • 323
  • 2
  • 9