Android:发现蓝牙设备时出错



我需要一些帮助在我的Android应用程序。我试图创建一个应用程序,发现蓝牙设备,然后比较名称的字符串。但是我遇到了两个问题。

1:当我退出应用程序(后退按钮),它退出,然后显示我崩溃消息(几秒钟后)..我认为问题在于"mReceiver"(检查"第二个问题")。

2:(主要问题)在下面的代码中,$"mReceiver = new BroadcastReceiver()"部分有问题。我在每个地方都扔了多个祝酒词,只是为了检查哪个部分不起作用,这行之前的一切都很好。

我不确定,但我认为在开始时没有"最终"声明"mReceiver"的问题->"private BroadcastReceiver mReceiver;"。但是,添加final会导致问题。

代码:

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);
}
}

谢谢你的时间。

为结果启动一个活动,以便提示用户是否允许启用蓝牙,但您不会等待结果并尝试立即查找设备。

你应该实现onActivityResult回调。如果结果为RESULT_OK,则开始发现设备。启用蓝牙需要一些时间

相关内容

最新更新