Reputation: 31
I need to know how I can connect and send data in android with bluetooth between two devices. At the moment the only thing I know is how to turn on and find bluetooth devices.
I am making a little video game that plays between two devices using bluetooth, but I do not understand how to make the connection between them, how to use bluetoothSockets, bluetoothServerSocket, and Threads that androids brings me.
This as the code that I have made:
public class MainActivity extends AppCompatActivity {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
static ListView dispositivos;
ArrayList<String> adapter=new ArrayList();
ArrayAdapter<String> adap;
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
public void con(View view){
adapter.clear();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
//checamos primero los dispositivos enlazados
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
adapter.add(device.getName() + "\n" + device.getAddress());
}
}
//vamos a buscar otros dispositivos
mBluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
adap=new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1, adapter);
dispositivos.setAdapter(adap);
}
//metodo de busqueda que agrega cada dispositivo que se encuentre a la lista
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
adap.add(device.getName() + "\n" + device.getAddress());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dispositivos=(ListView)findViewById(R.id.lista);
}
}
Upvotes: 0
Views: 324
Reputation: 707
In order to establish a Bluetooth connection and exchange data between two devices you need to follow some steps:
BluetoothServerSocket
)-1 You have said that already know how to search for devices but here there's the documentation page. If target device has been paired yet you don't have to perform a discovery, just ask the BluetoohAdapter for paired devices
-2 The Bluetooth connection has to start from somewhere, so a device has to inizialize it and act as a server; to do this you have to create a BluetoothServerSocket
.
You could find a step-by-step explanation here; essentially once you have created it, this server socket keep listening for an incoming connection's request, if the request will be accepted then return a valid BluetoothSocket
.
Keep in mind that the connection's request hast to come from the other device, so that smartphone has this duty, here, as always, the documentation.
-3 With your two new BluetoothSocket
you are able to retrieve a pair of streams for every device, an Input one and an Output one.
Now you have to manage the connection sending and receiving data from the specific stream
IMPORTANT: Bluetooth activities are heavy and asynchronous so must be performed off the main thread.
Create a Thread
and override his run()
method to perform every action:
search
listen for incoming request (server side)
manage connection (both side)
At the end of every documentation's paragraph which I've linked you could find some code example teaching you how to perform that specific task.
Start trying and if you get some error or you get stuck, ask for help.
Upvotes: 1