Reputation: 13
I am doing a project Smart cane Companion for Blind people to guide them to walk avoiding the obstacle in their path. It involves pairing Android phone with the HC 05 bluetooth module. The HC 05 bluetooth module is connected to Arudino UNO board. The HC 05 is not connecting with Android. Here is the connect code that I have written on android. Note : This activity lists the paired devices in the form of a listview and the address and name of the devices are gotten from the previous activity.
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
/**
* Created by Stealth on 20-04-2016.
*/
public class PairingList extends Activity
{
ListView lview;
String paires[];
String macs[];
ArrayAdapter<String> adapter;
String btdevices[];
BluetoothSocket btSocket;
BluetoothDevice device;
BluetoothAdapter btadapter;
int BLUETOOTH_REQUEST = 1;
private static final UUID MY_UUID = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pairinglist);
lview = (ListView)findViewById(R.id.listviewid);
btadapter = BluetoothAdapter.getDefaultAdapter();
Bundle bn = getIntent().getExtras();
paires = bn.getStringArray("paires");
macs = bn.getStringArray("macs");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,paires);
lview.setAdapter(adapter);
lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String address = macs[position];
device = btadapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
}
catch (IOException e)
{
Toast.makeText(getBaseContext(),"e" + " "+ address,Toast.LENGTH_LONG).show();
}
btadapter.cancelDiscovery();
try {
btSocket.connect();
Toast.makeText(getBaseContext(),"Connected to Device",Toast.LENGTH_LONG);
}
catch (IOException e1)
{
Toast.makeText(getBaseContext(),"e1"+" " + address,Toast.LENGTH_LONG).show();
}
}
});
}
}
The Problem is exception e1 always triggers. I have the MAC address of the HC 05 module in address MAC address is 98:D3:31:40:9A:D2 THE HC 05 IS STILL NOT CONNECTING WITH ANDROID ANY HELP WILL BE MUCH APPRECIATED THANK YOU!!!
Upvotes: 0
Views: 3992
Reputation: 116
Maybe try changing the UUID to "00001101-0000-1000-8000-00805f9b34fb"? I've connected HC-05 to an Android device in the past using this uuid.
Or you could try using getUuids() to find all the published services and their associated UUID values for each device discovered by Bluetooth and then use the UUID which works.
Upvotes: 2