我有以下几行代码。我正在尝试访问字符串deviceName和deviceHarwareAddress。我是否需要构造一个扩展BroadcastReceiver的类并在该类中创建一个将为我返回代码的方法?

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Discovery has found a device. Get the BluetoothDevice
            // object and its info from the Intent.
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
            String deviceHardwareAddress = device.getAddress(); // MAC address


        }
    }
};

最佳答案

不必要。如果要限制其在单个组件(例如活动/片段/服务)中的使用,则可以将“ mReceiver”保留在该组件内,然后为其注册mReceiver。那会很好的。

如果您在活动中执行此操作,就是这种情况。

public class BluetoothTest extends AppCompatActivity {
private ArrayList<BluetoothDevice> deviceList;
private BluetoothAdapter mBluetoothAdapter;
private ArrayList<DeviceItem> deviceItemList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.abc);
    /**
     * Do necessary coding to enable bluetooth
     */
    registerReceiver();
    startBluetoothDiscovery();
}
private void registerReceiver() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    IntentFilter dintentFilter = new IntentFilter();
    dintentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    dintentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    dintentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mDiscoveryBroadcastReceiver, dintentFilter);
}

public void startBluetoothDiscovery() {
    if (!mBluetoothAdapter.isDiscovering())
        mBluetoothAdapter.startDiscovery();
}

public void setBluetoothDevice(BluetoothDevice device) {
    if (!deviceList.contains(device))
        deviceList.add(device);
}

public ArrayList<BluetoothDevice> getBluetoothDeviceList() {
    return deviceList;

}

private void resetAll() {
    deviceItemList.clear();
    unregisterReceiver(mDiscoveryBroadcastReceiver);
}

private final BroadcastReceiver mDiscoveryBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Toast.makeText(getApplicationContext(), "Started discovery!!!", Toast.LENGTH_SHORT).show();
            deviceList = new ArrayList<>();
            deviceItemList.clear();
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Toast.makeText(getApplicationContext(), "Finished discovery!!!", Toast.LENGTH_SHORT).show();

        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            DeviceItem deviceItem = new DeviceItem(device.getName(),
                    device.getAddress(), device.getBluetoothClass(), device);
            deviceItem.setBluetoothDevice(device);
            /**
             * To check if the device is in paired list or not
             */
            if (mBluetoothAdapter.getBondedDevices().contains(device))
                deviceItem.setPaired(true);
            else
                deviceItem.setPaired(false);

            if (!deviceItemList.contains(deviceItem))
                deviceItemList.add(deviceItem);
            /**
             * Once the device is found,it is added to a list
             */
            setBluetoothDevice(device);
        }
    }
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return id == R.id.action_settings;
}

@Override
protected void onDestroy() {
    super.onDestroy();

}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}


}

如果是“服务”,则可以使用活页夹或观察者模式来使数据可用于活动。

09-11 02:20