我尝试实现一个必须搜索蓝牙设备的Android应用程序(具有设备名称的条件)。按下搜索按钮时,必须在搜索时显示进度对话框。第一次,没有进度对话框,应用程序工作但现在当我按下按钮时,有时它运行正常,它显示对话框,然后显示找到的设备,但有时,对话框消失,没有显示结果(或结果显示很长时间后)。 这是我的代码:
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class ConnectionScreen extends ActionBarActivity {
//Declaration of components
private static final String TAG = "ConnectionScreen";
public static final String BROADCAST = "PACKAGE_NAME.android.action.broadcast";
private static final int REQUEST_ENABLE_BT = 1;
private Button buttonSearch;
private CheckBox checkBoxAutoConnect;
private ListView listOfDevices;
private TextView statusId;
private TextView statusConnection;
private BluetoothAdapter bluetoothadapt;
private Set<BluetoothDevice> pairedDevices;
private ArrayAdapter<String> deviceAdapter;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connection_screen);
linkViewToResources();
listOfDevices.setAdapter(deviceAdapter);
bluetoothadapt = BluetoothAdapter.getDefaultAdapter();
if (bluetoothadapt == null) {
statusConnection.setText("Not supported");
Toast.makeText(getApplicationContext(), "Your device does not support Bluetooth", Toast.LENGTH_LONG).show();
buttonSearch.setEnabled(false);
} else {
buttonSearch.setEnabled(true);
}
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
private void linkViewToResources() {
buttonSearch = (Button) findViewById(R.id.searchButton);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
find(view);
}
});
checkBoxAutoConnect = (CheckBox) findViewById(R.id.checkBoxAutoconnect);
listOfDevices = (ListView) findViewById(R.id.listOfDevices);
deviceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
}
final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// add the name and the MAC address of the object to the arrayAdapter
// get paired devices
if (device.getName().startsWith("HXM") && device.getName().length() == 9) {
deviceAdapter.add(device.getName() + "\n" + device.getAddress());
//deviceAdapter.notifyDataSetChanged();
}
}
if(dialog != null && dialog.isShowing()){
dialog.dismiss();
}
}
};
public void find(View view) {
if (bluetoothadapt.isDiscovering()) {
// the button is pressed when it discovers, so cancel the discovery
bluetoothadapt.cancelDiscovery();
}
else {
dialog = new ProgressDialog(ConnectionScreen.this);
this.dialog.setMessage("Searching");
this.dialog.show();
deviceAdapter.clear();
bluetoothadapt.startDiscovery();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
sendBroadcast(enableBtIntent);
//new ProgressTask().execute(null, null, null);
//////////////////////////
}
}
@Override
protected void onPause() {
super.onPause();
try {
if (bReceiver != null) {
unregisterReceiver(bReceiver);
}
} catch (IllegalArgumentException e) {
Log.e(TAG, e.getMessage(), e);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.connection_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
有人怎么解决这个问题? 感谢。
更新:
我在测试期间遇到的另一个问题是使用搜索按钮:当我第一次按下它时,它会运行,但是当我第二次按下它时,它不会运行,依此类推。当它没有运行时,logcat中没有显示任何内容。当它运行时,这是在logcat中打印的内容:
07-14 10:34:58.466 22020-22020/com.yast D/ProgressBar﹕ setProgress = 0
07-14 10:34:58.466 22020-22020/com.yast D/ProgressBar﹕ setProgress = 0, fromUser = false
07-14 10:34:58.466 22020-22020/com.yast D/ProgressBar﹕ mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
07-14 10:34:58.496 22020-22020/com.yast W/ResourceType﹕ Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
07-14 10:34:59.527 22020-22020/com.yast E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-14 10:34:59.527 22020-22020/com.yast E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-14 10:34:59.777 22020-22020/com.yast E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-14 10:34:59.777 22020-22020/com.yast E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
答案 0 :(得分:0)
尝试更改代码(当您开始发现并且正在搜索设备时,您的代码不会重新启动发现例程,只需取消它):
public void find(View view) {
if (bluetoothadapt.isDiscovering()) {
// the button is pressed when it discovers, so cancel the discovery
bluetoothadapt.cancelDiscovery();
}
dialog = new ProgressDialog(ConnectionScreen.this);
this.dialog.setMessage("Searching");
this.dialog.show();
deviceAdapter.clear();
bluetoothadapt.startDiscovery();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
sendBroadcast(enableBtIntent);
//new ProgressTask().execute(null, null, null);
//////////////////////////
}
更新
来自documentation:
发现过程通常涉及大约12秒的查询扫描,然后对每个新设备进行页面扫描以检索其蓝牙名称。
设备发现是一个重量级的过程。
我想你必须等到发现例程完成后才能加速这个过程。