Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从此对象中访问deviceName和deviceHardwareAddress?_Java_Android - Fatal编程技术网

Java 如何从此对象中访问deviceName和deviceHardwareAddress?

Java 如何从此对象中访问deviceName和deviceHardwareAddress?,java,android,Java,Android,我有以下几行代码。我正在尝试访问字符串deviceName和deviceHarwareAddress。我是否需要构造一个扩展BroadcastReceiver的类,并在其中创建一个将为我返回代码的方法 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String

我有以下几行代码。我正在尝试访问字符串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();
}
公共类BluetoothTest扩展了AppCompatActivity{
私人ArrayList恶魔主义者;
私人蓝牙适配器mBluetoothAdapter;
私有ArrayList deviceItemList=新ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
/**
*执行必要的编码以启用蓝牙
*/
registerReceiver();
startBluetoothDiscovery();
}
私有无效注册表接收者(){
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
IntentFilter dintentFilter=新IntentFilter();
dintentFilter.addAction(找到BluetoothDevice.ACTION);
dintentFilter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u已启动);
dintentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
注册接收器(mDiscoveryBroadcastReceiver,dintentFilter);
}
公共无效startBluetoothDiscovery(){
如果(!mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.startDiscovery();
}
公共设备(蓝牙设备){
如果(!deviceList.contains(设备))
添加(设备);
}
公共阵列列表getBluetoothDeviceList(){
回归魔鬼主义;
}
私有void resetAll(){
deviceItemList.clear();
未注册接收人(mDiscoveryBroadcastReceiver);
}
专用最终广播接收器mDiscoveryBroadcastReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
if(BluetoothAdapter.ACTION\u DISCOVERY\u STARTED.equals(ACTION)){
Toast.makeText(getApplicationContext(),“开始发现!!!”,Toast.LENGTH\u SHORT.show();
deviceList=新的ArrayList();
deviceItemList.clear();
}else if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
Toast.makeText(getApplicationContext(),“Finished discovery!!!”,Toast.LENGTH\u SHORT.show();
}else if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
DeviceItem DeviceItem=新的DeviceItem(device.getName(),
device.getAddress(),device.getBluetoothClass(),device);
设备项目设置设备(设备);
/**
*检查设备是否在配对列表中
*/
if(mBluetoothAdapter.getBondedDevices()包含(设备))
deviceItem.setPaired(真);
其他的
deviceItem.setPaired(假);
如果(!deviceItemList.contains(deviceItem))
deviceItemList.add(deviceItem);
/**
*一旦找到设备,就会将其添加到列表中
*/
立根装置(装置);
}
}
};
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
返回id==R.id.action\u设置;
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
}
@凌驾
受保护的void onResume(){
super.onResume();
}
@凌驾
受保护的void onPause(){
super.onPause();
}
}


在“服务”的情况下,您可以使用活页夹或观察者模式为活动提供数据。

无需。如果您限制它在单个组件(比如活动/片段/服务)中的使用,您可以在该组件中保留“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();
}
如果您在活动中执行此操作,则会出现这种情况

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();
}
公共类BluetoothTest扩展了AppCompatActivity{
私人ArrayList恶魔主义者;
私人蓝牙适配器mBluetoothAdapter;
私有ArrayList deviceItemList=新ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
/**
*执行必要的编码以启用蓝牙
*/
registerReceiver();
startBluetoothDiscovery();
}
私有无效注册表接收者(){
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
IntentFilter dintentFilter=新IntentFilter();
dintentFilter.addAction(找到BluetoothDevice.ACTION);
dintentFilter.addAction(BluetoothAdapter.ACTION\u DISCOVERY\u已启动);
dintentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
注册接收器(mDiscoveryBroadcastReceiver,dintentFilter);
}
公共无效startBluetoothDiscovery(){
如果(!mBluetoothAdapter.isDiscovering())
mBluetoothAdapter.startDiscovery();
}
公共设备(蓝牙设备){
如果(!deviceList.contains(设备))
添加(设备);
}
公共阵列列表getBluetoothDeviceList(){
回归魔鬼主义;
}
私有void resetAll(){
deviceItemList.clear();
未注册接收人(mDiscoveryBroadcastReceiver);
}
专用最终广播接收器mDiscoveryBroadcastReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
if(BluetoothAdapter.ACTION\u DISCOVERY\u STARTED.equals(ACTION)){
Toast.makeText(