My app should just check, via bluetooth, if there is certain Arduino server around, and toast proper message.
This is the code when user presses button to search for server:
public void onClick(View v) {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("ARD_SPP")) {
sendButton.setVisibility(View.VISIBLE);
Toast.makeText(ConnectActivity.this, "Arduino server found, please sign up 1111", Toast.LENGTH_SHORT);
break;
}
}
}
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(discoveryResult, filter);
mBluetoothAdapter.startDiscovery();
}
And code inside BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
Boolean b = false;
String action = intent.getAction();
ProgressDialog dialog = new ProgressDialog(ConnectActivity.this);
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
dialog.setMessage("Searching for Arduino server...");
dialog.show();
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
dialog.dismiss();
if (!b)
Toast.makeText(ConnectActivity.this, "Server not found", Toast.LENGTH_SHORT).show();
}
else if (BluetoothDevice.ACTION_FOUND.equals(action)){
String deviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
if (deviceName.equals("ARD_SPP")) {
Toast.makeText(ConnectActivity.this, "Arduino server found, please sign up 2222", Toast.LENGTH_SHORT).show();
sendButton.setVisibility(View.VISIBLE);
openButton.setVisibility(View.GONE);
b = true;
dialog.dismiss();
}
}
}
I've got three problems with this.
First, I have the problem with "Server not found" message. It is shown even when arduino is around. I really don't have idea where to put that line in my code. I tried to put it in different lilnes of code, but I couldn't get what is required.
Second, message that server is found is shown two times. I meam toast inside broadcast receiver, not toast inside pairedDevices(after this one I put 1111 to recognize which toast is shown). I don't understand what is the part of the code where that toast is executed for the second time.
And I also had problems with progress dialog. I couldn't remove dialog from the screen, it was still there even when the server is found. I put dialog.dismiss() both in discovery finished block and device found, but it is still on the screen.
Does anyone please can help me with this ?
You set your variable every time you received an intent.
This should not be put in the OnReceived() but must be setted as private variable of your BroadCastReceiver. This will this the progress not dismiss and the "Server not found" toast, since b is always false when you received ACTION_DISCOVERY_FINISHED, and you try to dismiss a progressDialog not showed.
Concerning the multiple call of "Server found" call twice, check if you unregister your BroadcastReceiver when you don't need anymore.
Hope that help.
EDIT : Set your broadcastReceiver like that, the progressDialog should be create in the ACTION_DISCOVERY_STARTED :