1

I am trying to receive data (strings) from a Bluetooth enabled device whose MAC_ID is known. I have searched many examples ,but each article is pointing to Bluetooth Chat example, I think in Bluetooth Chat example, application need to be installed on both the devices for them to be connected and exchange strings.Correct me if I am wrong.
But I need to install application only on Receiver device.I have tried installing the application only on one device and tried connecting to the sender device, without success.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Usha V
  • 71
  • 1
  • 5

2 Answers2

0

Bluetooth is a peer to peer protocol where you need to have application running on both sides. Hence if you want to exchange data very good example would be Bluetooth chat. If you want to download or transfer a file you should either implement obex or FTP profile based applications.

siva
  • 1,850
  • 13
  • 14
  • Can you provide me a sample to implement either OBEX orFTP profile? – Usha V Jun 17 '15 at 11:19
  • Below link is the android bluetooth applcation source code where OPP profile(object push profile) using which files can be pushed to other devices.(Send via Bluetooth option is enabled through this service). This will be a good reference for implementing file transfer via bluetooth. Please accept and upvote my answer if it is helpful to you. https://android.googlesource.com/platform/packages/apps/Bluetooth/+/android-5.1.1_r4/src/com/android/bluetooth/opp/ – siva Jun 17 '15 at 11:35
0

Yes you need to deploy an application on both sides. If you are really restricted in a way that you only can deploy on one side, you have to figure out which standard protocols/bluetooth profiles the other side is capable of. You can figure that out by doing a SDP lookup. For a device, you will then get a list of UUIDs identifying these services. See the bluetooth spec for well known UUIDs. As @7383 pointed out, your are most probably looking for OBEX or FTP.

If you can deploy on both sides, you can write your own app using Blaubot (disclaimer: I wrote it). A simple Blaubot program would do this:

UUID MY_UUID = UUID.fromString("33bb1246-1472-11e5-b60b-1697f925ec7b");

// onCreate() or in a service, we create a blaubot instance
// using Bluetooth to form a network and Bluetooth + NFC to find devices
IBlaubotDevice ownDevice = new BlaubotDevice();
BlaubotUUIDSet uuidSet = new BlaubotUUIDSet(MY_UUID);

BlaubotBluetoothAdapter bluetoothAdapter = new BlaubotBluetoothAdapter(uuidSet, ownDevice);
BlaubotNFCBeacon nfcBeacon = new BlaubotNFCBeacon();
BlaubotBluetoothBeacon bluetoothBeacon = new BlaubotBluetoothBeacon();
this.mBlaubot = BlaubotAndroidFactory.createBlaubot(MY_UUID, ownDevice, adapter, nfcBeacon, bluetoothBeacon);

// start and wait until connected
this.mBlaubot.startBlaubot();

// create a channel and send your file
IBlaubotChannel fileChannel = this.mBlaubot.createChannel(1);

// convert your file to its bytes
File yourFile = // ... however you get it
byte[] fileBytes = ...// ... see http://stackoverflow.com/questions/858980/file-to-byte-in-java

// send it to all connected devices
fileChannel.publish(fileBytes, true);


// to receive it on the other device, do this:
// subscribe to the channel
fileChannel.subscribe(new IBlaubotMessageListener() {
    @Override
    public void onMessage(BlaubotMessage message) {
        // extract your bytes from the message
        byte[] fileBytes = message.getPayload();
        // .. do something useful or write it to a file again
        // to write it to a file
        File file = new File(yourFilePath);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bos.write(fileBytes);
        bos.flush();
        bos.close();
    }
});

This should be all you need. To allow the devices to connect, you have to pair them before or use NFC (just hold them together, when Blaubot is started). If you go with Blaubot let me know if you have problems that can't be solved with the documentation or the android quickstart guide.

I can only guess what your actual scenario looks like. If you have two android phones, this should work. If that is not the case, you should add more informations about the involved devices. Are we really talking about (classic) Bluetooth connections or are you trying to get data from a Bluetooth Low Energy device? In this case the famous chat example will not help you either.

hgross
  • 690
  • 6
  • 15
  • My scenario is I have to receive data from a third party Bluetooth device.So I cannot run the application on that device. As you mentioned I used UUID corresponding to OPP Profile and I am able to connect to the device (with out running the application on the sender device). Now my issue is when I initiate the file transfer at sender's side(share via Bluetooth),My phone displays its standard receive file dialogue, completely missing out my application.Any idea about this?? – Usha V Jun 18 '15 at 06:54
  • Ah ok, you need to check out Android's DownloadManager (http://developer.android.com/reference/android/app/DownloadManager.html) and register your App with the ACTION `ACTION_DOWNLOAD_COMPLETE` Intent in order to get notified, when a file was received. – hgross Jul 04 '15 at 00:15