Help sending data via Bluetooth

  • Replies:3
alopsa21
  • Forum posts: 3

May 1, 2019, 10:48:29 AM via Website

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

Reply
martin smith
  • Forum posts: 277

May 1, 2019, 12:01:16 PM via Website

Yes, you are right actually you don't need to use the adapter, better go with wireless connection with paired devices only...

Helpful?
alopsa21
Reply
alopsa21
  • Forum posts: 3

May 1, 2019, 1:33:25 PM via Website

Thanks, martin smith.

better go with wireless connection with paired devices only...

you mean Bluetooth wireless connection right?

In this case, could you point me in the right direction, please? Is there any code example or documentation I can follow? Or just give me a clue and I will google it

Helpful?
Reply
alopsa21
  • Forum posts: 3

May 1, 2019, 1:47:15 PM via Website

Reading again you answer I think I got what you meant.

Discover and pair is the difficult part so leave that work to the "phone own Bluetooth app" and work with those already paired devices from my app.
Like in this example that uses getBondedDevices

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.");
    }
}

}

I would expect that bondedDevices contains the paired devices even if they have been paired with the "phone own Bluetooth app" and not with my app. Correct?

Helpful?
Reply