我的活动中有几个标签,其中一个是搜索蓝牙设备并在列表视图中显示。但我有一个奇怪的问题,当我第一次点击蓝牙标签时,它不会开始发送,但是当我点击任何其他标签后回来。搜索开始并显示蓝牙设备列表。我无法弄清楚它为什么会发生......任何帮助
第一次永远不会进入OnRecieve
分享代码段
public class BluetoothSettings extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_settings);
listview = (ListView) findViewById(R.id.listbt);
txtgeo = (TextView) findViewById(R.id.txtbt);
progress = (ProgressBar) findViewById(R.id.progressbtBar);
wait_msg = (TextView) findViewById(R.id.txt_wait_msg);
}
@Override protected void onPause()
{
bluetooth.cancelDiscovery();
if(bReceiver != null) {
bReceiver = null;
}
super.onPause();
Log.d(TAG, "onPause");
}
@Override protected void onResume()
{
super.onResume();
if(setBt != null){
setBt.clear();
listview.setAdapter(null);
}
bluetooth.enable();
progress.setVisibility(View.VISIBLE);
wait_msg.setVisibility(View.VISIBLE);
bluetooth = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
bReceiver = new BluetoothReceiver();
registerReceiver(bReceiver, filter);
bluetooth.startDiscovery();
if(!bluetooth.isEnabled()) {
shouldTurnoffBt = true;
}
Log.d(TAG, "onResume");
}
@Override protected void onStart()
{
super.onStart();
Log.d(TAG, "onStart");
}
@Override protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
class BluetoothReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
action = intent.getAction();
Log.d(TAG, "SDSDSSSSSSSSSS ");
if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
{
Log.d(TAG, "dkidkididididididi #1 ");
txtgeo.setVisibility(View.INVISIBLE);
}
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
Log.d(TAG, "dkidkididididididi #2 ");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
setBt.add(new Property(device.getName(), device.getAddress(), rssi));
}
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
BluetoothSettings.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progress.setVisibility(View.INVISIBLE);
wait_msg.setVisibility(View.GONE);
if(shouldTurnoffBt)
bluetooth.disable();
if(setBt != null && setBt.size() > 0){
Log.d(TAG, "dkidkididididididi #3 ");
ArrayList<Property> lt = new ArrayList<Property>(setBt);
esWiFiSettings.settingSort(lt);
btAdapter = new btAdapter(BluetoothSettings.this, R.layout.bt_adapter, lt, "Bluetooth");
listview.setAdapter(btAdapter);
bluetooth.cancelDiscovery();
} else {
txtgeo.setVisibility(View.VISIBLE);
txtgeo.setText("No Bluetooth Device Found");
}
}
});
}
}
}
private BluetoothReceiver bReceiver;
public HashSet<Property> setBt = new HashSet<Property>();
String action;
boolean shouldTurnoffBt = false;
ListView listview;
BluetoothAdapter bluetooth;
private TextView txtgeo, wait_msg;
private ProgressBar progress;
答案 0 :(得分:1)
我只能猜测,但检查您的代码和BluetoothAdapter JavaDoc很可能您的bluetooth.startDiscovery()
调用返回false。来自JavaDoc:
如果蓝牙状态不是STATE_ON,则此API将返回false。打开蓝牙后,请等待带有STATE_ON的ACTION_STATE_CHANGED以获取更新的值。
检查返回值,并在确定启用蓝牙时才启动发现。如果bluetooth.isEnabled()
返回false,则必须激活蓝牙(就像你已经做过的那样)但是你应该在接收器中捕获引用的ACTION_STATE_CHANGED
- 事件(因此在你调用{{1}之前注册接收器})。
另外,请勿忘记在onPause或onStop上拨打unregisterReceiver(bReceiver)
。如果您没有向我们展示更多代码,则行
if(!bluetooth.isEnabled()){ shouldTurnoffBt = true; }
对你所描述的行为看起来很可疑;-) 请注意,蓝牙发现非常重要,最多需要12秒。
这个BroadcastReceiver的东西可能很棘手。我刚才为Blaubot写了一个包装类。请检查此代码:BlaubotBluetoothDeviceDiscoveryReceiver(您还需要this interface)。 或者,如果您只想连接多个Android设备,只需使用NFC-Beacon use Blaubot即可完全消除发现的需要。