应用程序在蓝牙关闭时有效,但在应用程序打开之前打开时无效

时间:2013-09-17 21:05:28

标签: android eclipse bluetooth

我正在尝试按照orroids蓝牙指南创建一个只显示整数到屏幕的简单应用程序,但是我无法从指南中获取代码才能正常工作。当蓝牙关闭时,应用程序请求允许蓝牙打开的权限,然后打开它,我得到“Hello world!”。但是,如果蓝牙已经打开,程序就会崩溃。

package com.example.bluetooth;

import java.util.Set;

import android.app.Activity;
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.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;


public class MainActivity extends Activity {
private ArrayAdapter<String> mArrayAdapter; 


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

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(), "Cannot Find Bluetooth Adapter", Toast.LENGTH_SHORT).show();
    }

    if (!((BluetoothAdapter) mBluetoothAdapter).isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        int REQUEST_ENABLE_BT = 1;
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    Set<BluetoothDevice> pairedDevices = ((BluetoothAdapter) mBluetoothAdapter).getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }

// Create a BroadcastReceiver for ACTION_FOUND
final BroadcastReceiver mReceiver = new BroadcastReceiver() {           
    @Override
    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 address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());                                   
        }       
    }
};

// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
}//end of onCreate

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

非常感谢,我收到的任何和所有帮助。这就是我试图编写代码http://developer.android.com/guide/topics/connectivity/bluetooth.html

的原因

我在提问时为任何不良礼节道歉。

1 个答案:

答案 0 :(得分:1)

覆盖OnDestroy()函数。在函数内部写:

unregisterReceiver(mReceiver);