iOS Beacon monitoring - RegionLeft is not fired - ios

I implemented the CLLocationManager.StartMonitoring() method. The CLLocationManager.EnterRegion is fired when the beacon comes into the range of the phone, however the RegionLeft does not get called when the Beacon is outside of the range of the phone again. I use the following code:
public void StartWakeupForBeacon()
{
//NSOperationQueue.MainQueue.AddOperation(() =>
//{
string message = "";
CLProximity previousProximity = CLProximity.Far;
var userId = ServiceLocator.Instance.Get<IAccountStoreService>().GetAccountUserId();
var tupple = UtilHelper.GetMajorMinorFromUserId(userId);
var major = tupple.Item1;
var minor = tupple.Item2;
var beaconUuid = UuidHelper.BeaconUuidString;
BeaconRegion = new CLBeaconRegion(new NSUuid(beaconUuid), (ushort)major, (ushort)minor, "com.example.company");
BeaconRegion.NotifyEntryStateOnDisplay = true;
BeaconRegion.NotifyOnExit = true;
BeaconRegion.NotifyOnEntry = true;
LocationManager = new CLLocationManager();
LocationManager.RequestAlwaysAuthorization();
LocationManager.RegionEntered += (object sender, CLRegionEventArgs e) =>
{
Console.WriteLine("region entered");
var btService = ServiceLocator.Instance.Get<IBluetoothService>();
ServiceLocator.Instance.Get<ILoggingService>().LogEvent(LoggingService.BeaconRegionEnteredEvent);
btService.ExchangeData();
};
LocationManager.RegionLeft += (object sender, CLRegionEventArgs e) =>
{
Console.WriteLine("region left");
ServiceLocator.Instance.Get<ILoggingService>().LogEvent(LoggingService.BeaconRegionLeftEvent);
};
LocationManager.DidDetermineState += (object sender, CLRegionStateDeterminedEventArgs e) =>
{
switch (e.State)
{
case CLRegionState.Inside:
Console.WriteLine("region state inside");
break;
case CLRegionState.Outside:
Console.WriteLine("region state outside");
break;
case CLRegionState.Unknown:
Console.WriteLine("region unknown");
break;
default:
Console.WriteLine("region state unknown");
break;
}
};
LocationManager.DidStartMonitoringForRegion += (object sender, CLRegionEventArgs e) =>
{
LocationManager.RequestState(e.Region);
string t_region = e.Region.Identifier.ToString();
Console.WriteLine(t_region);
};
LocationManager.DidRangeBeacons += (object sender, CLRegionBeaconsRangedEventArgs e) =>
{
if (e.Beacons.Length > 0)
{
CLBeacon beacon = e.Beacons[0];
switch (beacon.Proximity)
{
case CLProximity.Immediate:
message = "Immediate";
break;
case CLProximity.Near:
message = "Near";
break;
case CLProximity.Far:
message = "Far";
break;
case CLProximity.Unknown:
message = "Unknown";
//SendNotication();
break;
}
if (previousProximity != beacon.Proximity)
{
Console.WriteLine(message);
}
previousProximity = beacon.Proximity;
}
else
{
Console.WriteLine("nothing");
}
};
LocationManager.StartMonitoring(BeaconRegion);
LocationManager.MonitoringFailed += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("Failed monitoring");
};
LocationManager.StartRangingBeacons(BeaconRegion);
//});
}
When the Beacon is out of range of the phone, I can see that this is recognized by the app, since DidRangeBeacon does not recognize any beacon anymore and prints "nothing" to the console.
Additionally I get weird device logs regarding the location manager, it basically spams the following warning:
#Warning Tried injecting when not running

Related

How to make a listview work in Xamarin (VS 2022)

Here's this code. It sort of runs but then abends. How do I make the choice work? And additionally, I wrote (copied) this code about 18 months ago, so I am vague as to why it seems so backward:
void SwitchNumber()
{
var dialogView = LayoutInflater.Inflate(Resource.Layout.list_view, null);
Android.App.AlertDialog alertDialog;
listview = dialogView.FindViewById<ListView>(Resource.Id.listview);
textview = dialogView.FindViewById<TextView>(Resource.Id.textview);
var items = new string[] { "1","2","3" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
using (var dialog = new Android.App.AlertDialog.Builder(this))
{
listview.Adapter = adapter;
listview.ItemClick += Listview_ItemClick;
dialog.SetTitle("Switch Number");
dialog.SetMessage("Click on the number you want to switch to");
dialog.SetView(dialogView);
string newNumber = string.Empty;
dialog.SetNegativeButton("Cancel", (s, a) => { });
dialog.SetPositiveButton("OK", (s, a) =>
{
switch (prefs.GetInt("selectitemforAlert", 0))
{
case 0:
newNumber = "1";
break;
case 1:
newNumber = "2";
break;
case 2:
newNumber = "3";
break;
default:
currentNumber = "1";
break;
}
}//switch
);
if (newNumber == currentNumber) return;
ChangeNumber(newNumber);
alertDialog = dialog.Create();
// }
}
//using
dialogView.FindViewById<ListView>(Resource.Id.listview).Adapter = adapter;
alertDialog.Show();
listview.Adapter = adapter;
listview.ItemClick += Listview_ItemClick;
}
The list does appear but I get a null object reference.

Parsing serial data from sim900 module esp32

So lately I've been messing around with the idea of making my own cellphone nothing spectacular, just a basic one with touchscreen and basic functions call message calendar contacts list and the ability to connect to the internet and provide weather information via an api call..
The module Im using for gsm and GPRS communications is the popular sim900 module. I can communicate I can make calls I can do everything. But in stuck on a maybe (if not impossible to overcome) difficult roadblock.. You see sim900 module when receives a call transmits through the serial port the "RING" command followed by the "+CLIP.... (caller Id stuff)". OK I'm receiving that and I am breaking it down and accepting the command and all fine it works. But here comes the situation.. I want to read the battery capacity that is left (AT+CBC) and the gsm signal strength (AT+CSQ) all fine I'm sending those 2 commands at a fixed interval of like 3 seconds for the signal 10 for the battery. But now when a call comes it might overlap with the incoming response from trying to read the battery.. Let's say that I asked the module what is the battery level. Then the module will respond by sending "+CBC: (and battery level)" then let's say at the same exact time I receive a call.. Then all the data on the serial port just gets messed up and nothing is working.. My code is pretty rough and definitely the parsing section is awful but I'm more concerned that the parsing is not the problem. And the problem is the conflicting incoming data.. Is there any way of solving this problem? Or any other advice of where to look and how to approach the problem?
Every command from the gsm is delimited by the 0D0A sequence (CRLF)
Bellow is an example code from what i am doing
//for parsing
String incomStr;
String FirstStr;
String SecondStr;
String ThirdStr;
String FourthStr;
String FifthStr;
String SixthStr;
int strcount;
char incomChar;
boolean flagz = false;
//parsing
void getIncomingCommand() {
if (Gsm.available()) {
incomChar = Gsm.read();
//check to see if 0D0A if yes split the string
if ((incomChar == 0x0D) | flagz) {
flagz = true;
if (incomChar == 0x0A) {
switch (strcount) {
case 0:
FirstStr = incomStr;
incomStr = "";
strcount++;
flagz = false;
break;
case 1:
SecondStr = incomStr;
incomStr = "";
strcount++;
flagz = false;
break;
case 2:
ThirdStr = incomStr;
incomStr = "";
strcount++;
flagz = false;
break;
case 3:
FourthStr = incomStr;
incomStr = "";
strcount++;
flagz = false;
break;
case 4:
FifthStr = incomStr;
incomStr = "";
strcount++;
flagz = false;
break;
case 5:
SixthStr = incomStr;
incomStr = "";
strcount++;
flagz = false;
break;
default:
strcount++;
flagz = false;
incomStr = "";
}
}
} else {
incomStr += incomChar;
}
}
}
void clearIncomingCommand() {
FirstStr = "";
SecondStr = "";
ThirdStr = "";
FourthStr = "";
FifthStr = "";
SixthStr = "";
strcount = 0;
}
int getSignalLvl() {
char tempchar;
String tempstr;
Gsm.print("AT+CSQ");
Gsm.write(0x0D);
Gsm.write(0x0A);
delay(180);
while (Gsm.available()) {
tempchar = Gsm.read();
tempstr += tempchar;
}
return tempstr.substring(16, tempstr.indexOf(",")).toInt();
}
String getTime() {
char tempchar;
String tempstr;
Gsm.print("AT+CCLK?");
Gsm.write(0x0D);
Gsm.write(0x0A);
delay(180);
while (Gsm.available()) {
tempchar = Gsm.read();
tempstr += tempchar;
}
return tempstr.substring(tempstr.indexOf(",") + 1, tempstr.lastIndexOf(":"));
}
void setup() {
//start serial port
Serial.begin(115200);
//start the gsm port
Gsm.begin(9600, SERIAL_8N1, 32, 33);
strcount = 0;
updateTime(getTime());
delay(200);
updateSignal(getSignalLvl());
}
void loop() {
//stuff inside here will only be called / run only every X amount of time
// X = SECONDS/1000;
if ((millis() - lastupdate) >= 60000) {
updateTime(getTime());
lastupdate = millis();
}
getIncomingCommand();
if (SecondStr == "RING" & FourthStr.substring(0, 5) == "+CLIP") {
Serial.print("SomeOne is calling!! Number: ");
Serial.println(FourthStr.substring(8, 21));
Serial.println(phoneNums[i]);
Serial.println(FourthStr.substring(8, 21));
callerPhone = FourthStr.substring(8, 21);
clearIncomingCommand();
//important change state only once!
if (!change_state) {
came_from = state ;
change_state = 1;
}
Serial.print("coming from: ");
Serial.println(came_from);
state = 4;
flag = 0;
}
else if (SecondStr == "NO CARRIER") {
Serial.println("CALL ENDED");
clearIncomingCommand();
if (state == 3) {
state = 5;
flag = 0;
} else if (state == 4) {
state = came_from;
flag = 0;
}
change_state = 0;
}
else if (SecondStr == "MO RING") {
Serial.println("CALLING...");
clearIncomingCommand();
}
else if (SecondStr == "MO CONNECTED") {
Serial.println("CALL CONNECTED");
clearIncomingCommand();
if (state == 2) {
state = 3;
flag = 0;
}
} else if (FourthStr == "OK" | ThirdStr == "OK") {
Serial.println("Recieved ok clearing buffers");
clearIncomingCommand();
}
}

Xamarin iOS Bluetooth Low Energy - CBPeripheral.UpdatedCharacteristicValue reading TX characteristic shows unexpected data

I recently received a BLE device for Bluetooth to Serial. It uses TruConnect and I'm trying to get it to communicate with my serial device. The serial device receives communication over a serial cable and echoes back anything that is sent to it as well as any results from a command that is sent.
Right now I'm simply trying to send TruConnect commands to the BLE device to check the current baud rate that the BLE device is set for.
I wrote some code based on this TruConnect guide that I found:
https://truconnect.ack.me/1.5/apps/communicating_via_ble#reading_from_a_truconnect_device_serial_interface.
The problem seems to be that whenever I try to read anything from the tx characteristic when there should be data, the data is not right.
Setting up CBPeripheral events:
private void setupPerif(CBPeripheral perf)
{
selectedPeripheral = perf;
selectedPeripheral.UpdatedCharacterteristicValue += (sender, e) =>
{
var c = e.Characteristic;
if (c != null)
{
var uuid = c.UUID.ToString(true).ToLower();
if (uuid == UUID_RX)
{
//
}
else if (uuid == UUID_TX)
{
// expecting bytes to contain valid response data
// it almost always contains twenty 0s.
byte[] bytes = c.Value.Where(i => i != 13).ToArray();
var invalidBytes = c.Value.Where(i => i > 127).ToArray();
var nonZeros = c.Value.Where(i => i != 0).ToArray();
if (nonZeros.Length < 1)
{
return;
}
else
{
foreach (byte b in bytes)
handler.handleByteReceived((char)b);
}
}
else if (uuid == UUID_MODE)
{
//
}
}
};
selectedPeripheral.DiscoveredService += (sender, e) =>
{
var services = selectedPeripheral.Services;
if (services != null)
{
foreach (CBService service in services)
{
if (service.UUID.ToString(true).ToLower() == UUID_TRUCONNECT)
{
truConnect = service;
selectedPeripheral.DiscoverCharacteristics(truConnect);
}
}
}
};
selectedPeripheral.DiscoveredCharacteristic += (sender, e) =>
{
if (truConnect != null && truConnect.Characteristics != null)
{
foreach (CBCharacteristic c in truConnect.Characteristics)
{
var uuidString = c.UUID.ToString(true).ToLower();
if (uuidString == UUID_RX)
{
rx = c;
}
else if (uuidString == UUID_TX)
{
tx = c;
}
else if (uuidString == UUID_MODE)
{
mode = c;
// set to stream mode
selectedPeripheral.WriteValue(NSData.FromArray(new byte[] { MODE_COMMAND }), mode, CBCharacteristicWriteType.WithResponse);
}
}
}
};
selectedPeripheral.WroteCharacteristicValue += (sender, e) =>
{
// if UUID is for RX, we just wrote to RX. Drill down to
// TX characteristic and read it. This will trigger
// the UpdatedCharacteristicValue event.
string uuid = e.Characteristic.UUID.ToString(true).ToLower();
if (uuid == UUID_RX)
{
var services = selectedPeripheral.Services;
if (services != null)
{
foreach (CBService s in services)
{
if (s.UUID.ToString(true).ToLower() == UUID_TRUCONNECT)
{
var charachteristics = s.Characteristics;
if (charachteristics != null && charachteristics.Length > 0)
{
foreach (CBCharacteristic c in charachteristics)
{
if (c.UUID.ToString(true).ToLower() == UUID_TX)
{
Timer t = new Timer(new TimerCallback(delegate(object o)
{
selectedPeripheral.ReadValue(c);
}), null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(-1));
}
}
}
}
}
}
}
};
manager.ConnectPeripheral(selectedPeripheral);
}
Writes to rx. This is what should be used to actually send commands.
public void sendCommand(string command)
{
command += endString + "\n";
if (rx != null)
{
NSData d = NSData.FromString(command);
foreach (CBService s in selectedPeripheral.Services)
{
if (s.UUID.ToString(true).ToLower() == UUID_TRUCONNECT)
foreach (CBCharacteristic c in s.Characteristics)
{
if (c.UUID.ToString(true).ToLower() == UUID_RX)
selectedPeripheral.WriteValue(NSData.FromString(command), c, CBCharacteristicWriteType.WithResponse);
}
}
}
}
So my question is, why am I not getting the expected data when the CBPeripheral.UpdatedCharacteristicValue event is called? Occasionally I will get the expected data, but it is quite rare, and I can't seem to find any logical reason or pattern that would explain why this is happening.
AHA! I figured it out!
The problem was that I need to set the notify value for the appropriate characteristics. After doing that, I didn't need to call CBPeripheral.ReadValue(CBCharacteristic).
selectedPeripheral.DiscoveredCharacteristic += (sender, e) =>
{
if (truConnect != null && truConnect.Characteristics != null)
{
foreach (CBCharacteristic c in truConnect.Characteristics)
{
var uuidString = c.UUID.ToString(true).ToLower();
if (uuidString == UUID_RX)
{
rx = c;
}
else if (uuidString == UUID_TX)
{
tx = c;
// set the notify value to true and poof!
// now CBPeripheral.UpdatedCharacteristicValue
// event will be triggered at the appropriate time.
selectedPeripheral.SetNotifyValue(true, tx);
}
else if (uuidString == UUID_MODE)
{
mode = c;
// set to remote command mode
selectedPeripheral.WriteValue(NSData.FromArray(new byte[] { MODE_COMMAND }), mode, CBCharacteristicWriteType.WithResponse);
}
}
}
};

Monotouch : Get user current lcoation

How can i get use location in monotouch?
i'm trying with following code but any events does not fire(AuthorizationChanged & LocationsUpdated)
How should i do?please advise
public Task<CLLocation> test()
{
TaskCompletionSource<CLLocation> objTaskCompletionSource1 = new TaskCompletionSource<CLLocation> ();
CLLocation currentLocation = null;
CLLocationManager LocMgr = new CLLocationManager ();
if (CLLocationManager.LocationServicesEnabled)
{
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
currentLocation = e.Locations [e.Locations.Length - 1];
locationUpdated = false;
if (currentLocation != null && AllAreas != null)
{
LocationDetector.Instance.UpdateCurrentArea (new RLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude));
objTaskCompletionSource1.SetResult(currentLocation);
}
else
{
currentLocation = null;
objTaskCompletionSource1.SetResult(currentLocation);
}
};
LocMgr.AuthorizationChanged+= (object sender, CLAuthorizationChangedEventArgs e) => {
Console.WriteLine("AuthorizationChanged Fired");
};
LocMgr.Failed += (object sender, NSErrorEventArgs e) =>
{
};
LocMgr.StartUpdatingLocation ();
}
else
{
currentLocation = null;
objTaskCompletionSource1.SetResult (currentLocation);
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}
return objTaskCompletionSource1.Task;
}
Chances are you are testing this on iOS 8.
For iOS 8 you now need to request authorization.
So use something like the following in your ViewDidLoad (make LocMgr class scope level - so remove the local instance in your version):-
LocMgr = new CLLocationManager();
if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
LocMgr.RequestWhenInUseAuthorization();
}
Also, in order for the above to work, and for the dialog box to show you also need to add the following entry into your info.plist:-
<key>NSLocationWhenInUseUsageDescription</key>
<value>{some message that will be shown to the end-user}</value>
Update 1:-
Code that I am using:-
In ViewDidLoad:-
LocMgr = new CLLocationManager ();
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
LocMgr.RequestWhenInUseAuthorization ();
}
UIButton objButton1 = new UIButton (UIButtonType.RoundedRect);
objButton1.SetTitle ("Click Me", UIControlState.Normal);
objButton1.TouchUpInside += (async (o2, e2) => {
CLLocation objLocationInfo = await Test1();
Console.WriteLine("Completed");
});
this.View = objButton1;
And the Test1 function:-
public Task<CLLocation> Test1()
{
TaskCompletionSource<CLLocation> objTaskCompletionSource1 = new TaskCompletionSource<CLLocation> ();
CLLocation currentLocation = null;
if (CLLocationManager.LocationServicesEnabled) {
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
currentLocation = e.Locations [e.Locations.Length - 1];
locationUpdated = false;
//if (currentLocation != null && AllAreas != null) {
if (currentLocation != null) {
//LocationDetector.Instance.UpdateCurrentArea (new RLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude));
objTaskCompletionSource1.SetResult (currentLocation);
} else {
currentLocation = null;
objTaskCompletionSource1.SetResult (currentLocation);
}
};
LocMgr.AuthorizationChanged += (object sender, CLAuthorizationChangedEventArgs e) => {
Console.WriteLine ("AuthorizationChanged Fired");
};
LocMgr.Failed += (object sender, NSErrorEventArgs e) => {
Console.WriteLine("AHH Failed");
};
LocMgr.StartUpdatingLocation ();
} else {
currentLocation = null;
objTaskCompletionSource1.SetResult (currentLocation);
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}
return objTaskCompletionSource1.Task;
}
Your need these also at class level:-
private bool locationUpdated = false;
private CLLocation currentLocation = null;
private CLLocationManager LocMgr;
Remember you need to edit your info.plist as well.
If you then run the example putting a breakpoint at the Console.WriteLine("Completed"); you should then be able to inspect objLocationInfo and see that it has a location.

Monotouch : pause application unit dialog response

How can i pause application for prevention from running next method unit client does not selected dialog buttons?
For example i am showing location update dialog for accessing location service and i want to pause my application for dialog response
public CLLocation UpdateUserLocation()
{
CLLocation currentLocation = null;
CLLocationManager LocMgr = new CLLocationManager();
if (CLLocationManager.LocationServicesEnabled)
{
if (UIDevice.CurrentDevice.CheckSystemVersion (6, 0))
{
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
currentLocation = e.Locations [e.Locations.Length - 1];
};
}
else
{
LocMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) =>
{
currentLocation = e.NewLocation;
};
}
LocMgr.StartUpdatingLocation ();
LocMgr.Failed += (object sender, NSErrorEventArgs e) =>
{
Console.WriteLine (e.Error);
};
}
else
{
currentLocation = null;
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}
if (currentLocation != null)
{
LocationDetector.Instance.UpdateCurrentArea (new MyLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude));
}
return currentLocation;
}
If I am understanding your question correctly.
When you display a dialog box, you are wanting to stop execution of the current method from further executing until the user selects a dialog box response.
Once they have selected a response, you would then like to continue execution of the code in the same function, effectively achieving your 'pause' that you are after.
To achieve this in iOS you can use a TaskCompletionSource.
In the example below it shows a dialog box first, asking the user if they want some coffee and then waits for the user to respond.
Once the user responds, it then continues execution, within the same function, and displays a further message box that is dependent on the selection that the user made.
UIButton objButton1 = new UIButton (UIButtonType.RoundedRect);
objButton1.SetTitle ("Click Me", UIControlState.Normal);
objButton1.TouchUpInside += (async (o2, e2) => {
int intCoffeeDispenserResponse = await ShowCoffeeDispenserDialogBox();
//
switch (intCoffeeDispenserResponse)
{
case 0:
UIAlertView objUIAlertView1 = new UIAlertView();
objUIAlertView1.Title = "Coffee Dispenser";
objUIAlertView1.Message = "I hope you enjoy the coffee.";
objUIAlertView1.AddButton("OK");
objUIAlertView1.Show();
break;
case 1:
UIAlertView objUIAlertView2 = new UIAlertView();
objUIAlertView2.Title = "Coffee Dispenser";
objUIAlertView2.Message = "OK - Please come back later when you do.";
objUIAlertView2.AddButton("OK");
objUIAlertView2.Show();
break;
}
});
//
View = objButton1;
private Task<int> ShowCoffeeDispenserDialogBox()
{
TaskCompletionSource<int> objTaskCompletionSource1 = new TaskCompletionSource<int> ();
//
UIAlertView objUIAlertView1 = new UIAlertView();
objUIAlertView1.Title = "Coffee Dispenser";
objUIAlertView1.Message = "Do you want some coffee?";
objUIAlertView1.AddButton("Yes");
objUIAlertView1.AddButton("No");
//
objUIAlertView1.Clicked += ((o2, e2) => {
objTaskCompletionSource1.SetResult(e2.ButtonIndex);
});
//
objUIAlertView1.Show();
//
return objTaskCompletionSource1.Task;
}

Resources