Bluetooth with Android

The Raspberry Pi will communicate with the smartphone with Bluetooth. To program this communication and create our Android application we use Eclipse and use JAVA language.

Here what you have to know to create a Bluetooth communication :

 

In the manifest.xml file :

<uses-permission android:name="android.permission.BLUETOOTH" />

Turn On yout Bluetooth :

Asking permission :

if (!blueAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

Without asking permission :

// Main
if (!blueAdapter.isEnabled()) {
    blueAdapter.enable();}

Create a list which contain the name and the address of devices :


myListView = (ListView)findViewById(R.id.listView1); // assoiate the variable to the list view

// create the arrayAdapter that contains the BTDevices, and set it to the ListView
BTArrayAdapter = new ArrayAdapter<String>(this,        android.R.layout.simple_list_item_1);
myListView.setAdapter(BTArrayAdapter);

Get paired device :

pairedDevices = myBluetoothAdapter.getBondedDevices();

// put it's one to the adapter
for(BluetoothDevice device : pairedDevices)
BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());