2015-06-10 74 views
0

我在我的活動中有幾個選項卡,其中一個是搜索藍牙設備並在列表視圖中顯示。但我有一個奇怪的問題,當我第一次點擊藍牙標籤時,它不會啓動discorvery,但當我點擊任何其他標籤後回來。搜索開始並顯示藍牙設備列表。我無法弄清楚,爲什麼它正在發生...任何幫助藍牙發現不從第一次點擊開始

這是從來沒有進入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

我只能猜測,但檢查你的代碼, BluetoothAdapter JavaDoc很有可能您的bluetooth.startDiscovery()調用返回false。來自JavaDoc:

如果藍牙狀態不是STATE_ON,則此API將返回false。打開藍牙後,等待ACTION_STATE_CHANGED與STATE_ON以獲取更新的值。

檢查返回值,並且只有在確定已啓用藍牙時纔開始發現。如果bluetooth.isEnabled()返回false,則必須激活藍牙(就像您已經這樣做了),但您應該在接收器之後引用ACTION_STATE_CHANGED-事件(因此請在致電bluetooth.enable()之前註冊接收器)。

另外,不要忘記在onPause或onStop中調用unregisterReceiver(bReceiver)。如果有更多的代碼,你沒有告訴我們,該行

如果(bluetooth.isEnabled()!){ shouldTurnoffBt = TRUE; }

看起來很可疑關於你描述的行爲;-) 注意,藍牙的發現是非常重量級的,並且需要最多12秒。

這個BroadcastReceiver的東西可能會很棘手。前段時間,我爲Blaubot寫了一個包裝類。檢查此代碼:BlaubotBluetoothDeviceDiscoveryReceiver(您還需要this interface)。 或者如果您只是想連接多個Android設備,只需要使用NFC-Beacon use Blaubot即可完全消除對發現的需求。