Using BluetoothLEAdvertisementWatcher in background application? - raspberry-pi

I try to scan BLE devices in my Windows 10 IOT background (headless) app running on Raspberry PI 3.
I also tried to use BluetoothLEAdvertisementWatcher in a headed app (with UI) on the same RaspBerry PI machine and it worked.
My headless app is the simplest as it can be:
public sealed class StartupTask : IBackgroundTask
{
private readonly BluetoothLEAdvertisementWatcher _bleWatcher =
new BluetoothLEAdvertisementWatcher();
public void Run(IBackgroundTaskInstance taskInstance)
{
_bleWatcher.Received += _bleWatcher_Received;
_bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
_bleWatcher.Start();
}
private void _bleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
}
}
_bleWatcher_Received is never hit. Capabilities are set (Bluetooth, Internet, Proximity).
What is the problem? What do I miss?

You app shuts down when the run method completes. That's why _bleWatcher_Received is never hit.
To prevent you app from exiting you need call the “GetDeferral” method like this:
public void Run(IBackgroundTaskInstance taskInstance)
{
deferral = taskInstance.GetDeferral();
//YOUR CODE HERE
}
For more information please reference "Developing Background Applications".

Related

Network Client use wrong input devices when using Unity.Netcode with Starter Asset Controller (New input system)

The server is always using Keyboard and Mouse just fine.
The client however always use "xbox controller" instead of Keyboard & Mouse:
The following is the inspector view as a client:
The Start Asset input action are unchanged,
This is what I tried but client is still being assigned to controller:
private void Start()
{
if (!IsOwner)
{
Destroy(GetComponent<PlayerInput>());
}
}
How could I fix this? Other than hard coding (PlayerInput)map.SwitchCurrentControlScheme("KeyboardAndMouse");?
This issue is fixed by disabling Player Input script.
And only enable it on Network Spawn.
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (IsOwner)
{
_playerInput = GetComponent<PlayerInput>();
_playerInput.enabled = true;
}
}

Unity - Mirror Networking : Player client disconnect after scene changed

I'm currently using Mirror Networking to make a multiplayer game. I have a scene when players are all connected, they can choose their characters and set the ready. If all players are ready, I change current scene to arena scene using MyNetworkManager.ServerChangeScene(arenaSceneName). This method sets all player clients as not ready. But After the scene was loaded, my player client is no longer connected to my host and I don't know why.
Can you help me please ?
Thanks a lot for answers.
Clients that connect to this server will automatically switch to this scene. This is called automatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready again to participate in the new scene."
if its not solve problem can you give more information about your project
Are you using MatchInterestManager ? This can be lead some problems like yours
Edit 1 -
Maybe the problem was host
can you run your code on server not host
Edit 2 -
i belive your command function runs on client not server because you miss "Cmd" prefix on your command function change it CmdOnAllPlayersReady()
public class ReadyPlayerChecker : NetworkBehaviour
{
public List<PlayerBehaviour> activePlayers;
public List<PlayerBehaviour> GetActivePlayers()
{
return activePlayers;
}
// Start is called before the first frame update
void Start()
{
activePlayers = new List<PlayerBehaviour>();
}
// Update is called once per frame
void Update()
{
foreach (PlayerBehaviour player in FindObjectsOfType<PlayerBehaviour>
(true))
{
if(!activePlayers.Contains(player))
{
activePlayers.Add(player);
}
}
bool allPlayersReady = true;
foreach (PlayerBehaviour player in activePlayers)
{
if (!player.IsReady())
{
allPlayersReady = false;
}
}
if (allPlayersReady && activePlayers.Count > 0)
{
OnAllPLayersReady();
}
}
[Command]
public void OnAllPLayersReady()
{
GameObject.Find("SceneManager").GetComponent<SceneChanger>
().LoadScene("SimpleArena");
}
}
This is my Network Manager settings :
I succeed to make the playerclient connected when the scene change. Now, the player prefab for my client is in host view but not in the corresponding client view. Do someone can tell me why ?

Why am I not connecting to Photon Chat but rather disconnecting

Hi I am trying to connect to Photon Chat but I am not able to connect to it. Rather I am getting disconnected. If I don't put the
if(chatclient!=null)
chatclient.Service();
in the Update loop, it is not getting disconnected, but rather not getting connected as well. Well this is my code
public class PhotonChatController : MonoBehaviour, IChatClientListener
{
void Start()
{
if (string.IsNullOrEmpty(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat))
Debug.Log("no CHat app ID provided");
ConnectToPhotonChat();
}
private void ConnectToPhotonChat()
{
chatclient = new ChatClient(this);
chatclient.ChatRegion="in";
chatclient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat,PhotonNetwork.AppVersion,
new Photon.Chat.AuthenticationValues(nickname));
Debug.Log("Connect to Photon Chat");
}
void Update()
{
if(chatclient!=null)
chatclient.Service();//in the update so that I constantly is connected and receiving messages
}
public void OnDisconnected()
{
Debug.Log("You have disconnected from the Photon Chat........");
}
public void OnConnected()
{
Debug.Log("You have successfully connected to the Photon Chat.......");
chatclient.SendPrivateMessage("chethan7", "INVITE......");
}
Actually speaking in the console "You have successfully connected to the Photon Chat.......",which is the one which gets executed when you successfully connect to Photon Chat should be executed.But that statement is not getting executed at all.
Here is the look of the console:
I got the answer here, hope it helps someone as well

Loader during Unity IAP Callback

I want to put loader in between dialog boxes come up for the purchase. What is the way for this?
Because when game player press Buy button, he should require to wait for 5 to 10 second depends on internet speed and server response and this process happed 2 to 3 times because multiple dialogs come up within screen.
So in this case, may be player can leave the screen. I want to put the loader so that game player realise that some processing is running in background, he required to wait for some time.
At present I was following completely this code for Unity IAP setup.
Integrating Unity IAP In Your Game
I assume this is for mobile platform but even if its not still the following can be considered:
Simple solution is to create a full screen Image (UI/Panel) object in your UI to block clicks. I would use Animator component (with triggers) to display this panel in front of other UI when there is a background process running.
public class Loader : MonoBehaviour
{
public static Loader Instance;
Animator m_Animator;
public bool Loading {get; private set;}
void Awake()
{
Instance = this; // However make sure there is only one object containing this script in the scene all time.
}
void Start()
{
//This gets the Animator, which should be attached to the GameObject you are intending to animate.
m_Animator = gameObject.GetComponent<Animator>();
Loading = false;
}
public void Show()
{
Loading = true;
m_Animator.SetBool("Loading", Loading); // this will show the panel.
}
public void Hide()
{
Loading = false;
m_Animator.SetBool("Loading", Loading); // this will hide the panel.
}
}
Then in any script which manipulates UI:
public void BuyButtonClicked()
{
Loader.Instance.Show();
// process time taking stuff
Loader.Instance.Hide();
}
You can also create any kind of loading animation as child of panel object using simple images and animation tool inside Unity (for example rotating animation (use fidget spinner, its cool)).
And in case of Android where user have option to leave screen by pressing OS back button you can prevent going back by checking if any loading is in progress by following example:
// code for back button
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
BackButtonPressed();
}
}
void BackButtonPressed()
{
if(Loader.Instance.Loading)
return;
// use back button event. (For example to leave screen)
}
Hope this helps ;)

Click on Camera Shutter using UIAutomator

I am writing an Espresso test for my app, and am trying to automate clicking the shutter button after opening of a camera in my app.
I am using Espresso and UIAutomator in the Android Emulator. I managed to dump this UI in UIAutomatorViewer.
I can't figure out why I am unable to click on the shutter button using UIAutomator using this code:
public void clickCameraShutterButton() throws UiObjectNotFoundException
{
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiSelector shutterSelector = new UiSelector().resourceId("com.android.camera:id/shutter_button");
UiObject shutterButton = device.findObject(shutterSelector);
shutterButton.click();
}
The camera just sits there, and the shutter button is never clicked. This is the stack trace I'm getting in the Android Studio monitor:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
Any advice would be appreciated.
You can try this code:
device.findObject(new UiSelector().resourceId("com.android.camera:id/shutter_button")).click();
or
device.findObject(new UiSelector().description("Shutter button")).click();
or
device.executeShellCommand("input keyevent 27");
this mean KEYCODE_CAMERA value is 27
or
device.click(x,y);
This worked for me
#Before
public void setUp() {
// Initialize UiDevice instance
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
mDevice = UiDevice.getInstance(instrumentation);
}
...
/**
* ##Test comment here##
*
* #throws Exception
*/
#Test
public void culebraGeneratedTest_CameraShutter() throws Exception {
mDevice.findObject(By.res("com.android.camera2:id/shutter_button").desc("Shutter").clazz("android.widget.ImageView").text(Pattern.compile("")).pkg("com.android.camera2")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
}
This test finds the shutter and clicks on it.
If you are interested this test was automatically generated using CulebraTester.
Just need to put the UI Automator Viewer "resource-id" value at the place of *
mdevice.findObject(new UiSelector().resourceId("*")).click();