BluetoothLEAdvertisementWatcher. No events on Hololens - c#

I'm trying to send GPS-coordinates from an Android device to the Hololens by BLE-advertisments.
The problem as far as i see, the events from BluetoothLEAdvertisementWatcher do not trigger on the Hololens.
I have tried copy-paste both this implementation
and this on the receiver side, no received event.
This is the current implementation in Unity, i have implementated the same functions in a blank UWP-C# project and deployed directly on the Hololens with the same result.
private void StartWatcher()
{
void OnAdvertisementReceived(object sender, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
Debug.Log("Event triggered"); // switched to Write-line on the C#->UWP implementation
}
void OnAdvertisementStop(object sender, BluetoothLEAdvertisementWatcherStoppedEventArgs eventArgs)
{
Debug.Log("Event triggered"); // switched to Write-line on the C#->UWP implementation
}
try {
BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher();
watcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(GetManufacturerData());
watcher.Received += OnAdvertisementReceived;
watcher.Stopped += OnAdvertisementStop;
watcher.Start();
Debeug.Log("Started"); // switched to Write-line on the C#->UWP implementation
} catch (Exception e){
}
}
private BluetoothLEManufacturerData GetManufacturerData()
{
var manufacturerData = new BluetoothLEManufacturerData();
manufacturerData.CompanyId = 0X06EF;
return manufacturerData;
I know the advertisements are sent, becuase if i debug the native UWP-application directly in Visual Studio on the PC(even as ARM64), everything works. The events are triggered.
The watcher.Status reports "started" on the Hololens
Even if i disable the Bluetooth on the HoloLens while the application are running, the stop-event are not either triggered.
If i check watcher.Status after i disable the Bluetooth, the status reports aborted but the Stoped-event are not recieved.
The capability is activated following this article
Now to where it gets wierd.
During one of my first tries i had my application running on the Hololens as i've tried to remove the pairing between the Android device and HoloLens.
The android devices appears as two devices on the Hololens, device name as one and "LE"+MAC as the other.
Sometime during the time i was in the Bluetooth settings and removed the two adapters from the cellphone, the HoloLens recieved three separate advertisments. The data in the advertisments was also confirmed from my Android device becuase during my inital tries i parsed the data.
But thats it, i have tried to replicate this for 10 hours today, Pair and unpair the different adapters in different stages while listening for recieve events but without luck.
On my PC, it do not matter if the adapters are paired or not.
I have tried both .NET Standard 2.0 and .Net 4.x as API Compatibility Level.
I have set breakpoints in both the receive and Stoped event-handler as i was afraid that the calls from the Event-functions was the problem. As far as i see, we never reach the implemented events.
Any suggestion on what might be the next step to check is appreciated.
The Hololens Build is 19041.1144.

Ok, so i can now recevie advertisments on the HoloLens by running the example provedied by Microsoft.
I could not receive any events at all and no updates was available in the settings, so i've reinstalled all applications in the Microsoft Store and that seems to be the solution for me. After that and a restart of the HoloLens i was able to recieve advertisements from my PC.
Another tip for other tip for people trying to recieve advertisements on the HoloLens 2.
When i was able to receive advertismements from my pc i saw that the advertisments from my Android device was not recieved on the HoloLens, but recieved on the PC.
The advertisments on my PC was of type NonConnectableUndirected and from my Android device ScannableUndirected.
I needed to change to "AdvertisingSet" and in the AdvertisingSetParameters set both isConnectable and isScannable to false to be able to send NonConnectableUndirected.
Now the HoloLens recevies the advertisments from my Android device.

Related

How to scan for another device through Bluetooth- Xamarin

I'm developing Xamarin application using Visual Studio 2019. I have to connect to another device through Bluetooth and send some data and receive acknowledgement back. Tried these two samples out
https://github.com/msthrax/BLEApp
and
https://github.com/didourebai/BLEPluginDemo.
But didn't help me while scanning for nearby Bluetooth devices, since the below codes are not giving expected result respectively..
1.
listView_DeviceList.ItemsSource = CrossBluetooth.Adaptor.GetListOfDiscoveredDevices();
and
2.
adapter.DeviceDiscovered += (s, a) =>
{
deviceList.Add(a.Device);
};
Both Bluetooth and location turned on in my devices. Can anyone let me know the possibilities of problem here. I don't have any build errors in both of the above samples. Whereas, I have one surprise also. Below line is giving proper result, which is of no use for me currently.
listView_PairedDeviceList.ItemsSource = CrossBluetooth.Adaptor.GetPairedDevices();
After enabling the Location Permission for the app(earlier, I turned on Device Location, but we should allow the app to get device location), all my above problems are solved out.

Detect if game running in android emulator

Is there any way to know the game is running in an emulator application or a real device ?
Some Applications like VirtualXposed are virtual emulator for android devices. Such Applications allow users to install a game / application multiple times on their device with different identifies.
I want to know is there any way to recognize my game / application is running in emulator or a physical device in unity3d ?
something like :
if(isemulator)
{
dosomething();
}
It's not easy to do since there are many emulators out there. The answer from this post says that you can use Build.FINGERPRINT.contains("generic") to do that.
You can port the Java code to C# in Unity with the AndroidJavaClass class and without building Java plugin. Below is the ported version in C#:
public bool IsEmulator()
{
AndroidJavaClass osBuild;
osBuild = new AndroidJavaClass("android.os.Build");
string fingerPrint = osBuild.GetStatic<string>("FINGERPRINT");
return fingerPrint.Contains("generic");
}
To make sure that this covers most emulator, use the android-emulator-detector plugin. This plugin seems to be rigorously tested and detects most emulators. You can use AndroidJavaClass to communicate with it and UnityPlayer.UnitySendMessage to make callback into your Unity code in C#.
Maybe you can look for some emulator specific SystemInfos (Unity Docs SystemInfo) eg. SystemInfo.deviceModel

Windows phone 8 push notification null channel uri

I know this has been brought up many times but couldn't find an answer for it.
I am trying to set up push notifications in my app using the below code however I am running into the channel uri null problem.
I have tried the app on 4 different devices + emulator all under the same network conditions (Work WiFi - Home WiFi - 3G) 2 of the devices are Lumia 920 both could not obtain a channel uri, while the other 2 devices an HTC 8X and a Lumia 820 can successfully obtain channel uri and register for push.
The emulator also can obtain a channel uri successfully.
On one of the Lumia 920's it managed to get a channel uri, but I uninstalled and installed the app again and since then couldn't get any channel uri.
Below are my scenarios:
1- Lumia 920 Black installed on 3G worked fine, uninstalled/reinstalled stopped working on any connection (3G - Work WiFi - Home WiFi)
2- Lumia 920 Yellow installed on 3G - Work WiFi - Home WIfi never managed to get a channel uri
3- HTC 8X on 3G - Work WiFi - Home WiFi worked great on all 3 networks
4- Lumia 820 same as HTC 8X worked great
Please note that push notifications on other apps are working fine on all 4 devices.
I'd greatly appreciate any feedback/advice for the channel null uri
Below is the code I used, it is the same code as provided by MSDN
public MainPage()
{
/// Holds the push channel that is created or found.
HttpNotificationChannel pushChannel;
// The name of our push channel.
string channelName = "ToastSampleChannel";
InitializeComponent();
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
// Register for all the events before attempting to open the channel.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.Open();
// Bind this new channel for toast events.
pushChannel.BindToShellToast();
}
else
{
// The channel was already open, so just register for all the events.
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
// Register for this notification only if you need to receive the notifications while your application is running.
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
pushChannel.ChannelUri.ToString()));
}
}
I have also experienced the same issue. And it got resolved by setting my HTC windows phone with correct date and time.Once I set the date and time automatically, My hybrid application wp8 successfully registered pushplugin and received MPNS Channeluri.
The ChannelUri was null because the new phone I was developing on would not set it's date and time correctly. Once I switched the time to be set manually, then fixed it's time, date and region and then switched it back to auto, everything worked fine such as ssl certificates, windows phone update and most importantly a ChannelUri ;)
Try going into settings > "phone + SIM" and turning off the "Data connection" option this sometimes does the trick?
Have you tried changing the options in settings > data sense? There are options there that restrict background data?
Is battery saver enabled? This limits usage when battery is low etc.
Also as mentioned above, Have you checked the date and time on the device? Maybe it's caused by the auto setting of date and time. You can fix this manually then switch back to auto. It works.
Try using a local wifi connection rather than the cell phone data connection. Disabling the phones "Data connection" as per one of the comments here will achieve this result. Although I tried this and failed it lead me to noticed that my phone was not connected to our wifi router.
NOTE: In my case we have very bad cell coverage so we use a special vodafone router that redirects all our cell phone data, calls, etc thru our fixed line Internet connection. Once I bypassed the that router I received a ChannelUri. I also tried the phone/app at work which uses a different cell tower and that was fine.
I tried setting the date manually but to no effect. What ended up working for me was to actually start the phone without the SIM card, shut down, re-insert SIM and start again. This allowed me to get a ChannelUri again.
Suspect this could be related to the suggestion of TripVoltage who suggests toggling the SIM's data connection.

httpnotificationchannel same ChannelUri from two different apps in emulator

I'm new to windows phone development and have started working with push notifications within the emulator in visual studio 2012. We are having a strange problem where we have two apps in visual studio and when we start each one and call the following:
CurrentChannel = HttpNotificationChannel.Find("ChannelName");
if (CurrentChannel == null)
{
CurrentChannel = new HttpNotificationChannel("ChannelName");
CurrentChannel.Open();
CurrentChannel.BindToShellTile();
CurrentChannel.BindToShellToast();
}
Even though the ChannelName is different in each app the CurrentChannel.ChannelUri is the same so when we send push notifications they only go to one app.
I am sure we are missing something very obvious or misunderstanding the way this is supposed to work but any insight would be appreciated.
I supposed you didn't get the right Uri according to your piece of codes. Try to Listen on ChannelUriUpdated event to get a fresh Uri.
If you did, maybe try it on a real device, emulator is not always to be trusted enough.
And Push notification channel URi is unique for device & app combination, to be sure you made something different in these two apps. Anyway seems this couldn't be happened.

Suppress BTExplorer when another application needs to access the bluetooth connection (Bluetopia)

I am developing a C# device application for Motorola MC55 devices (Bluetopia is used). This application sets the bluetooth inquiry and pairing procedure in motion (by means of the Enterprise Mobility Developer Kit) and writes down the virtual serial port #:
public void DoConnection(IRemoteBTDevice rd)
{
RemoteDevice remoteDevice = new RemoteDevice(rd.DeviceName, rd.Id.Replace(":", ""), "");
this.bluetooth.RemoteDevices.Add(remoteDevice);
this.bluetooth.RemoteDevices.Refresh();
if (!remoteDevice.IsPaired)
{
remoteDevice.Pair(rd.Pin);
}
//TODO Note serial port # (get it by means of remoteDevice.LocalComPort)
//Works smoothly, but a second process has to do this (requirement).
remoteDevice.OpenPort();
}
But as soon as a second process (e.g. another c# device application) opens that serial port, BTExplorer is launched. How do I suppress this phenomenon?
this.comX = new SerialPort(this.BluetoothPortName);
this.comX.Open();
I'm just using another Assembly: http://32feet.codeplex.com/. It works smoothly for Motorola ES400 (Microsoft Stack). After seeing this: http://32feet.codeplex.com/wikipage?title=Stonestreet%20One%20Bluetopia, I used the same code, but it doesn't work trouble-free for MC55 yet.
Thank you for your support,
Roger Huber
As I understand it, it is BTExplorer.exe that actually provides/controls the virtual COM port service, so it needs to be running when a virtual COM port is in use...
Do be sure to let know what the issues you see on your MC55 when using my 32feet.NET Bluetopia support. As I noted at 32feet.NET: Stonestreet One Bluetopia I tested on a M3 Mobile device which has a quite recent version of the Bluetopia stack. Maybe StoneStreetOne changed something between your versions... (If Bluetopia start-up fails completely then I've more logging in version 3.3 -- which I'm not too far away from releasing).
Of course if that second C# program needs to use a virtual COM port then 32feet.NET won't help -- BTExplorer is still required as discussed above. However if you could change that program to use BluetoothClient etc instead of COM ports... (Remembering of course the "Bluetopia one at a time" restriction of course).
Alan

Categories