2013-05-13 156 views
1

我的Android應用程序需要一些幫助。我正在嘗試創建一個應用程序來發現藍牙設備,然後將該名稱與字符串進行比較。 但我遇到了2個問題。Android:發現藍牙設備時出錯

1:當我退出程序(返回鍵)退出,然後告訴我崩潰消息(幾秒鐘後)。我認爲這個問題是「mReceiver」(檢查「第二個問題)

2 :(主要問題)在下面的代碼中,$「mReceiver = new BroadcastReceiver()」部分存在問題,我在每一處只是爲了檢查哪個部分不工作而拋出多個Toast,在這條線正常工作之前的所有東西都可以正常工作。

我不知道,但我覺得有沒有在年初宣佈「mReciver」「最後」的問題 - >「私人廣播接收器mReceiver,」不過最終加入會導致問題

守則:

public class MainActivity extends Activity { 

private final static int REQUEST_ENABLE_BT = 1; //It's really just a number that you provide for onActivityResult (>0) 

//Temp objects for testing 
private String StringMeeting = "meeting"; 
ProgressBar bar; 

//Member fields 
private BluetoothAdapter mBluetoothAdapter; 
private ArrayAdapter<String> mPairedDevicesArrayAdapter; 
private ArrayAdapter<String> mNewDevicesArrayAdapter; 

// Create a BroadcastReceiver for ACTION_FOUND 
private BroadcastReceiver mReceiver; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //declare & start progress bar 
    bar = (ProgressBar) findViewById(R.id.progressBar1); 
    bar.setVisibility(View.VISIBLE); 

    //------------Setup a Bluetooth (Get Adapter) ------------------ 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter == null) { 
     // Device does not support Bluetooth 
     Toast.makeText(getApplicationContext(), "This Device does not support Bluetooth", Toast.LENGTH_LONG).show(); 
    }else{Toast.makeText(getApplicationContext(), "Getting the Adabter is done", Toast.LENGTH_SHORT).show();} 


    //------------Setup a Bluetooth (Enable Bluetooth) ------------------ 
    if (!mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
    }else{Toast.makeText(getApplicationContext(), "Enable Bluetooth is done", Toast.LENGTH_SHORT).show();} 

    //------------Finding Devices (Discovering Devices)---------------------- 
    // Create a BroadcastReceiver for ACTION_FOUND 
    Toast.makeText(getApplicationContext(), "creating the mReceiver", Toast.LENGTH_SHORT).show(); //last thing works 
    mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(getApplicationContext(), "accessed onReceive + will create action", Toast.LENGTH_SHORT).show(); 
      String action = intent.getAction(); 

      Toast.makeText(getApplicationContext(), "Waiting to discover a device", Toast.LENGTH_SHORT).show(); 
      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       Toast.makeText(getApplicationContext(), "enterd the if", Toast.LENGTH_SHORT).show(); 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       Toast.makeText(getApplicationContext(), "Getting device names", Toast.LENGTH_SHORT).show(); 
       Toast.makeText(getApplicationContext(), device.getName(), Toast.LENGTH_LONG).show(); 
       Toast.makeText(getApplicationContext(), "displaying the name should be done", Toast.LENGTH_SHORT).show(); 

       // Add the name and address to an array adapter to show in a ListView 
       mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 

      }else{Toast.makeText(getApplicationContext(), "Error: BluetoothDevice.ACTION_FOUND.equals(action) = False", Toast.LENGTH_SHORT).show();} 
     } 
    }; 
    // Register the BroadcastReceiver 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 


} //onCreate end 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 

    // Make sure we're not doing discovery anymore 
    if (mBluetoothAdapter != null) { 
     mBluetoothAdapter.cancelDiscovery(); 
    } 

    // Unregister broadcast listeners 
    this.unregisterReceiver(mReceiver); 
} 
} 

感謝您的時間。

+0

你說當你點擊後退按鈕時,應用程序崩潰了,但你指向的地方是廣播接收器被創建爲錯誤的來源。廣播接收器是在onCreate中創建的。當點擊返回按鈕時,調用onDestroy。所以無論失敗都應該在onDestroy上失敗。 – neo 2013-05-13 14:02:56

+0

另一點是在單擊後退按鈕之前是否旋轉設備。旋轉設備會破壞活動,如果您沒有代碼來處理這種情況,那麼您的狀態會消失。旋轉設備後,您可能會有空引用。 – neo 2013-05-13 14:07:25

+0

我看到的另一件事是以下內容。你檢查mBluetoothAdapter是否爲null,但是你可以在else之外調用isEnabled方法。換句話說,如果mBluetoothAdapter不爲空,啓用藍牙和查找設備纔有意義。您只需顯示吐司消息,但其餘代碼仍在運行。 – neo 2013-05-13 14:19:38

回答

0

您開始一個結果活動,以便提示用戶是否允許啓用藍牙,但您不等待結果並嘗試立即查找設備。

你應該實現onActivityResult回調。如果結果是RESULT_OK,那麼你開始發現設備。啓用藍牙需要一些時間。

+0

問題是即使藍牙已經打開,它不會繼續;所以問題不在於啓用藍牙所需的時間。事實上,在isEnable()之後,除了mReceiver之外,一切正常。我甚至添加了我的應用程序檢查「以前配對的」設備的部分,然後它花費很長時間才轉到mReceiver,並且仍然不起作用。 – neorak 2013-05-14 06:56:14

+0

您能否顯示失敗時顯示的logcat? – neo 2013-05-14 13:20:32

+0

我無法複製日誌,但似乎找不到任何錯誤。但是,我扔了一些日誌消息來檢查代碼..顯然,它到達onRecieve時停止工作,並在退出onReceive後繼續工作。彷彿它忽略了它的存在。 onReceive之前和之後的所有日誌消息都起作用,但其中並沒有。 – neorak 2013-05-14 22:00:00