Drakthal
Drakthal

Reputation: 193

How do i send information via a setup bluetooth connection

So im working around with bluetooth and trying to figure out how to send two strings via a bluetooth connection. From one android device to another.

I found this guide http://developer.android.com/guide/topics/connectivity/bluetooth.html but it talks alot about setting up the connection. So i went straight down to the chapter about Managing a Connection. The reason i do this is that in the apps i create i plan to setup the bluetooth connection before opening the apps (via the phones usual bluetooth setup) and then open the apps and send when it is necessary.

So my question is how do i find the bluetooth socket that should be setup? Since that should be what im searching for to create the sending and recieving threads?

Hope this is enough information, else tell what more you need and ill try and answer to the best of my ability.

Best Regards Drakthal

Upvotes: 0

Views: 2192

Answers (1)

Jong
Jong

Reputation: 9125

The usual bluetooth setup only pairs between devices, it doesn't create a data connection between them (And even if it would, you wouldn't be able to access this Socket object because it's not created in your process).

After Bluetooth is turned on, you can call BluetoothAdapter.getBondedDevices() to get a set of the paired devices. You can then iterate over them, and initiate a connection to the one you want. You can't avoid the connection creation :( If you want a simplified example, you can look here (An answer I posted a while ago, regarding the whole pairing/connecting/sending/receiving subject with bluetooth).

Once you acquired an open connection, sending the 2 string is easy.

String s1 = "A", s2 = "B";
byte[] buf1 = s1.getBytes(), buf2 = s2.getBytes();
OutputStream os = connection.getOutputStream();
os.write(buf1);
os.write(buf2);
os.flush();
connection.close();

Upvotes: 1

Related Questions