To resolve device item, you can use this:
var deviceItem = Sitecore.Data.Items.DeviceItem.ResolveDevice(pageItem.Database);
Then you can use one of the approaches below:
Sitecore.Context.Device = deviceItem; // doesn't validate deviceItem
Sitecore.Context.Data.Device = deviceItem; // validates deviceItem
using(new DeviceSwitcher(deviceItem)) { // you*your code herehere*/ }
You also can use the switchers below (if needed) to switch Context.Database and Context.Item if they are null:
using (new DatabaseSwitcher(pageItem.Database)) {}
using (new ContextItemSwitcher(pageItem)) {}