蓝牙功能可以破坏广播接收器

时间:2015-02-19 05:25:21

标签: java android bluetooth

我的应用旨在将所有扫描的蓝牙设备附加到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");
    }

2 个答案:

答案 0 :(得分:2)

您选择的通过BluetoothAdapter.enable()方法启用蓝牙无线电的方法是异步调用。从method's documentation可以看出,一旦方法返回,就无法假设无线电已启动并处于活动状态。您必须等待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();
    }
}

此修改将在您每次到达前台时检查蓝牙,并且只有在您准备好时才会触发扫描。

答案 1 :(得分:0)

尝试做:

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

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

OR:

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