Android蓝牙问题 - 无法解析符号' addAction' &安培;无法解析符号' registerReceiver'

时间:2015-04-10 16:12:00

标签: android android-bluetooth

我正试着为什么我得到了

  

未知类:BluetoothDevice.ACTION_FOUND,未知类:BluetoothDevice.ACTION_ACL_CONNECTED,&未知类:BluetoothDevice.ACTION_ACL_DISCONNECTED错误和未知类:mReceiver&未知类:bluetoothIntentFilter导致我无法解析符号' addAction' &安培;无法解析符号' registerReceiver'

我无法解决的错误。会导致这个问题的原因是什么?

这是我目前的代码:

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.Activity;

public class BluetoothConnectionRepair extends Activity {
    //Assuming that the device address is registered and is connected
    private String partnerDevAdd = "11:22:33:AA:CC:FF";
    private boolean isConnected = true;

    Context context;
    IntentFilter bluetoothIntentFilter = new IntentFilter();
    bluetoothIntentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    Context.registerReceiver(mReceiver, bluetoothIntentFilter);
    bluetoothIntentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    Context.registerReceiver(mReceiver, bluetoothIntentFilter);
    bluetoothIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    Context.registerReceiver(mReceiver, bluetoothIntentFilter);

    //BroadcastReceiver for Bluetooth related checks
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (isConnected == false) {
                //Bluetooth Device discovered
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    //Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                    //Check if the discovered device is one we had communication with
                    if (device.getAddress().equals(partnerDevAdd) == true) {
                        connectToExisting(device);
                    }
                }

                if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                    //Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                    //Check if the connected device is one we had communication with
                    if (device.getAddress().equals(partnerDevAdd) == true) {
                        isConnected = true;
                    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
                        BluetoothDevice bt_device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        if (bt_device.getAddress().equals(partnerDevAdd) == true) {
                            isConnected = false;
                        }
                    }
                }
            }
        }
    };
private void connectToExisting(BluetoothDevice bluToothDevice) {
    new ConnectThread(bluToothDevice);
}
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

从registerReceiver()方法中删除Context,并将代码移到onCreate()方法中。

以下是带有更改的新版代码:

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.Activity;
import android.os.Bundle;

public class BluetoothConnectionRepair extends Activity {
    //Assuming that the device address is registered and is connected
    private String partnerDevAdd = "11:22:33:AA:CC:FF";
    private boolean isConnected = true;

    IntentFilter bluetoothIntentFilter = new IntentFilter();
    @Override
    public void onCreate(Bundle bundle) {
        bluetoothIntentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, bluetoothIntentFilter);
        bluetoothIntentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        registerReceiver(mReceiver, bluetoothIntentFilter);
        bluetoothIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(mReceiver, bluetoothIntentFilter);
    }

    //BroadcastReceiver for Bluetooth related checks
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (isConnected == false) {
                //Bluetooth Device discovered
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    //Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                    //Check if the discovered device is one we had communication with
                    if (device.getAddress().equals(partnerDevAdd) == true) {
                        connectToExisting(device);
                    }
                }

                if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                    //Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                    //Check if the connected device is one we had communication with
                    if (device.getAddress().equals(partnerDevAdd) == true) {
                        isConnected = true;
                    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
                        BluetoothDevice bt_device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        if (bt_device.getAddress().equals(partnerDevAdd) == true) {
                            isConnected = false;
                        }
                    }
                }
            }
        }
    };
private void connectToExisting(BluetoothDevice bluToothDevice) {
    new ConnectThread(bluToothDevice);
}
}