启用蓝牙会破坏广播接收器

Bluetooth enabling disrupts broadcastreceiver

我的应用旨在将所有扫描的蓝牙设备附加到 textview。如果手机蓝牙打开,这会很好用。但是,如果我的应用程序检查手机蓝牙是否关闭并在关闭时打开它,然后开始我的发现过程,我的 broadcastreciever 没有接收到事件,所以我的 textview 接收到事件不填充。感谢您的帮助!

这是我的代码:

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

    txtResults = (TextView) this.findViewById(R.id.txtResults);
    mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (!(mBlueToothAdapter.isEnabled())) {
        mBlueToothAdapter.enable(); 

    }           

    mBlueToothAdapter.startDiscovery();

我的收件人:

public static class BlueToothBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        blueToothName = device.getName();
        blueToothAddress = device.getAddress();
        blueToothClass = device.getBluetoothClass();
        blueToothBondState = device.getBondState();
        GetBondStateStr(blueToothBondState);
        blueToothUUIDS = device.getUuids();

        paramsBlueTooth
                .add(new BasicNameValuePair("Name: ", blueToothName));
        paramsBlueTooth.add(new BasicNameValuePair("Address: ",
                blueToothAddress));
        paramsBlueTooth.add(new BasicNameValuePair("Class: ", String
                .valueOf(blueToothClass)));
        paramsBlueTooth.add(new BasicNameValuePair("Bond State: ",
                blueToothBondStateStr));
        paramsBlueTooth.add(new BasicNameValuePair("UUIDS: ", String
                .valueOf(blueToothUUIDS)));

        showBlueToothData();

    }

ShowBlueToothData():

    private void showBlueToothData() {
        StringBuilder results = new StringBuilder();

        results.append("-----BLUETOOTH DEVICE INFORMATION-----\n");
        results.append("Name: " + blueToothName + "\n");
        results.append("Address: " + blueToothAddress + "\n");
        results.append("Class: " + blueToothClass + "\n");
        results.append("Bond State: " + blueToothBondStateStr + "\n");
        results.append("UUIDS: " + blueToothUUIDS + "\n");

        txtResults.append(new String(results));
        txtResults.append("\n");
    }

尝试做:

Intent enable_intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable_intent, REQUEST_ENABLE_BT);

里面if (!(mBlueToothAdapter.isEnabled())){...}

或:

for(int i=0;i<5;i++){
   if (!mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.enable(); 
   }
   Thread.sleep(100);
}else{
//display error, unsuccessfull , try manually
}

您选择的通过 BluetoothAdapter.enable() 方法启用蓝牙无线电的方法是异步调用。正如您从 method's documentation 中看到的那样,您不能假设无线电在方法 returns 后立即启动并处于活动状态。您必须等待 ACTION_STATE_CHANGED 广播才能知道收音机已准备好让您尝试扫描。

如果您阅读文档,还要注意这样做是一种糟糕的体验,因为没有通知用户。更好的选择是将用户发送到“设置”以自行启用蓝牙的方法。您可以将启动逻辑修改为更像这样的内容:

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

    txtResults = (TextView) this.findViewById(R.id.txtResults);
    mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();

    …
}

@Override
protected void onResume() {
    super.onResume();
    if (!(mBlueToothAdapter.isEnabled())) { 
        //Take the user to settings first!
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(intent);
    } else {
        mBlueToothAdapter.startDiscovery();
    }
}

此修改会在您每次进入前台时检查蓝牙,只有在您准备好时才触发扫描。