我的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);
}
}
感謝您的時間。
你說當你點擊後退按鈕時,應用程序崩潰了,但你指向的地方是廣播接收器被創建爲錯誤的來源。廣播接收器是在onCreate中創建的。當點擊返回按鈕時,調用onDestroy。所以無論失敗都應該在onDestroy上失敗。 – neo 2013-05-13 14:02:56
另一點是在單擊後退按鈕之前是否旋轉設備。旋轉設備會破壞活動,如果您沒有代碼來處理這種情況,那麼您的狀態會消失。旋轉設備後,您可能會有空引用。 – neo 2013-05-13 14:07:25
我看到的另一件事是以下內容。你檢查mBluetoothAdapter是否爲null,但是你可以在else之外調用isEnabled方法。換句話說,如果mBluetoothAdapter不爲空,啓用藍牙和查找設備纔有意義。您只需顯示吐司消息,但其餘代碼仍在運行。 – neo 2013-05-13 14:19:38