簡體   English   中英

如何從此對象中訪問deviceName和deviceHardwareAddress?

[英]How do I access deviceName and deviceHardwareAddress from within this object?

我有以下幾行代碼。 我正在嘗試訪問字符串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();
}

}

如果是“服務”,則可以使用活頁夾或觀察者模式來使數據可用於活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM