this is the first time I'm asking here, and I'm new to programming, so please pardon my ignorance..
I'm trying to make an application that will auto-connect my android to the specified bluetooth devices (arduino).
The parameter is the RSSI level of the specified bluetooth (I'm using 2 bluetooth) so that if :
rssi1 = RSSI level of bluetooth A
rssi2 = RSSI level of bluetooth B
what I want is if rssi1 > rssi2 then my android will be connected to bluetooth A, and vice versa.
I am referencing to the code on the Get bluetooth signal strength by Memochipan and I know that we can read the RSSI of some bluetooth devices while scanning, but I don't know how to make the RSSI as a parameter for my code.
thanks in advance
here's my code
public class Btdisc extends Activity {
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
String ruang1 = "20:14:11:26:03:71";
String ruang2 = "28:BA:B5:E1:B8:C0";
int limit = -58;
int rssi1a;
int rssi2a;
int stat;
private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
private Set<BluetoothDevice> pairedDevices;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_btdisc);
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
Button tombol = (Button) findViewById(R.id.button1);
tombol.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
BTAdapter.startDiscovery();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.btdisc, menu);
return true;
}
private final BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
rssi1a = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
if (device.getAddress().equals(ruang1))
{
TextView rssi1 = (TextView) findViewById(R.id.textView1);
rssi1.setText( "Ruang 1" + " => " + rssi1a + "dBm\n\n");
}
rssi2a = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
if (device.getAddress().equals(ruang2))
{
TextView rssi2 = (TextView) findViewById(R.id.textView2);
rssi2.setText("Ruang 2" + " => " + rssi2a + "dBm\n\n");
}
// try {
// btconnect();
//stat = 1;
//} catch (IOException e) {
//stat = 0;
//e.printStackTrace();
//}
}
}
}
}
void btconnect() throws IOException
{
if (!BTAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 1);
}
Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getAddress().equals(ruang1) && rssi1a >= limit){
mmDevice = device;
break;
}
}
}
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
};
};
};
`