[英]Problems with searching for available device, via bluetooth, in android
我的應用程序應該僅通過藍牙檢查周圍是否有某些Arduino服務器,並吐出正確的消息。
這是用戶按下按鈕搜索服務器時的代碼:
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();
}
以及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();
}
}
}
我有三個問題。
首先,我遇到“找不到服務器”消息的問題。 即使在arduino周圍時也會顯示。 我真的不知道將該行放在我的代碼中。 我試圖將其放入不同的代碼行中,但無法獲得所需的內容。
其次,兩次顯示找到服務器的消息。 我在廣播接收器中添加烤面包,而不是在pairedDevices內部添加烤面包(在此之后,我將1111放入顯示的烤面包中)。 我不明白第二次執行該Toast的代碼部分是什么。
而且進度對話框也有問題。 我無法從屏幕中刪除對話框,即使找到服務器,該對話框仍然存在。 我將dialog.dismiss()放在發現完成的塊和找到的設備中,但仍在屏幕上。
有誰能幫助我嗎?
每次收到意圖時,都要設置變量。
Boolean b = false;
ProgressDialog dialog = new ProgressDialog(ConnectActivity.this);
不應將其放在OnReceived()中,而必須將其設置為BroadCastReceiver的私有變量。 這將不會取消進度並且不會顯示“找不到服務器”的消息,因為當您收到ACTION_DISCOVERY_FINISHED並嘗試取消未顯示的progressDialog時,b始終為false。
關於兩次“服務器找到”調用的多次調用,請檢查是否在不再需要時注銷了BroadcastReceiver。
希望對您有所幫助。
編輯:像這樣設置您的broadcastReceiver,應該在ACTION_DISCOVERY_STARTED中創建progressDialog:
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
private boolean b = false;
private ProgressDialog dialog ;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
dialog = new ProgressDialog(ConnectActivity.this);
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();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.