6 Replies - 860 Views - Last Post: 09 May 2019 - 03:31 AM

#1 alopsa21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-May 19

Help sending data via Bluetooth

Posted 01 May 2019 - 01:56 AM

Hi,

I'm working in an android app that has to "talk" via Bluetooth with a device. App has to be able to send and receive data.
I started to work with BluetoothAdapter but couldn't make it work properly so I leave it for a few weeks. Now I would like to continue with that and I wonder if is mandatory to use the BluetoothAdapter.
The phone is able to find and pair devices itself so why we need BluetoothAdapter? Why don't we use those connections directly and try to "talk" with the paired devices?
Does it make sense?
Cheers
Alex

Is This A Good Question/Topic? 0
  • +

Replies To: Help sending data via Bluetooth

#2 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Help sending data via Bluetooth

Posted 08 May 2019 - 02:07 AM

are you using bluetooth low energy or bluetooth classic? I have not gotten that far into the bluetooth aspects but I've made connections and stuff for projects. It a bit of work getting everything to work.
Was This Post Helpful? 0
  • +
  • -

#3 alopsa21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-May 19

Re: Help sending data via Bluetooth

Posted 08 May 2019 - 05:52 AM

View Postben255, on 08 May 2019 - 02:07 AM, said:

are you using bluetooth low energy or bluetooth classic? I have not gotten that far into the bluetooth aspects but I've made connections and stuff for projects. It a bit of work getting everything to work.


I didn't check BLE to be honest so I think that I'm going to use Bluetooth classic. Is there any code or example you can report? Main question is if I need to scan and pair devices using Bluetooth adapter or if I can use the phone already paired devices.
Cheers
Was This Post Helpful? 0
  • +
  • -

#4 ben255   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 528
  • Joined: 09-September 13

Re: Help sending data via Bluetooth

Posted 08 May 2019 - 07:50 AM

Well,
https://www.youtube....h?v=Tjx50DyV2xk
here is a vid about classic

https://www.youtube....h?v=qx55Sa8UZAQ
this one is about BLE

Yes you need to scan and pair devices yourself how else will the app know what and who is sending what data
Was This Post Helpful? 0
  • +
  • -

#5 alopsa21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-May 19

Re: Help sending data via Bluetooth

Posted 08 May 2019 - 01:08 PM

The own phone, when you active the Bluetooth, is able to scan, find and pair devices, isn't. I thought my app can use those paired devices...
I'll take a look anyway to those videos
Thank you
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,152
  • Joined: 12-June 08

Re: Help sending data via Bluetooth

Posted 08 May 2019 - 01:46 PM

Well. .sure.. there's device pairing code

https://developer.an...-device-pairing
https://developer.an...ivity/bluetooth
https://developer.an...s/apps/creating


The take away is the adpater is needed to provide a route to talk. It's like having two soup cans with no string between them if you don't have the adapter. There's no other way to piggy back on ti.
Was This Post Helpful? 0
  • +
  • -

#7 alopsa21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-May 19

Re: Help sending data via Bluetooth

Posted 09 May 2019 - 03:31 AM

Thanks for details, I'll take a look at all this info as soon as I have some time. It looks very interesting.
Anyway, I have found this example that could explain better what I meant.

Let's say that I run these steps.
1 - Enable manually the BT in my phone
2 - Phone shows me different devices.
3 - I select one of them
4 - I run my app that contains below code

private void init() throws IOException {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
    if (blueAdapter.isEnabled()) {
        Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

        if(bondedDevices.size() > 0) {
            Object[] devices = (Object []) bondedDevices.toArray();
            BluetoothDevice device = (BluetoothDevice) devices[position];
            ParcelUuid[] uuids = device.getUuids();
            BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
            socket.connect();
            outputStream = socket.getOutputStream();
            inStream = socket.getInputStream();
        }

        Log.e("error", "No appropriate paired devices.");
    } else {
        Log.e("error", "Bluetooth is disabled.");
    }
}



Would bondedDevices object contain the manually discovered and selected device?
Or I need to run the start discovery devices in my own app?https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices

Thanks again for you time
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1