Код: Выделить всё
[BroadcastReceiver]
[IntentFilter(new[] { BluetoothDevice.ActionAclDisconnected })]
public class BluetoothReceiver : BroadcastReceiver
{
public async override void OnReceive(Context context, Intent intent)
{
Log2("On bluetooth receive");
Log2("Intent is: " + intent.Action);
if (BluetoothDevice.ActionAclDisconnected.Equals(intent.Action))
{
BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
string deviceName = device.Name;
Log2("Device name is: " + deviceName);
if (device.Name.ToUpper().IndexOf("FOO") != -1)
{
Log2("Getting location");
var loc = await GetLocation();
if (loc == null || loc.HasValue == false)
{
Log2("Location is null or does not have value");
return;
}
Log2("Location is: " + JsonConvert.SerializeObject(loc.Value));
}
}
}
}
static Location latestLocation = null;
private async static Task GetLocation()
{
LocationManager locationManager = (LocationManager)_this.GetSystemService(LocationService);
string provider = LocationManager.NetworkProvider;
if (locationManager.IsProviderEnabled(provider) == false)
{
Log2("Provider: " + provider + " was not available");
provider = LocationManager.GpsProvider;
if (locationManager.IsProviderEnabled(provider) == false)
{
Log2("Provider: " + provider + " was not available");
provider = locationManager.GetBestProvider(new Criteria(), true);
if (provider == null)
{
Log2("Best provider was not available, returning");
return null;
}
}
}
Log2("Requested location update and waiting");
latestLocation = null;
locationManager.RequestLocationUpdates(provider, 30000, 1, _this);
Stopwatch sw = new Stopwatch();
sw.Start();
while (latestLocation == null && sw.ElapsedMilliseconds < 30000)
await Task.Delay(100);
if (latestLocation == null)
{
Log2("Latest location was null, returning");
return null;
}
double latitude = latestLocation.Latitude;
double longitude = latestLocation.Longitude;
return (latitude, longitude);
}
public void OnLocationChanged(Location location)
{
Log2("location changed event.");
latestLocation = location;
Log2($"Latitude: {location.Latitude}, Longitude: {location.Longitude}");
}
Возникает проблема, когда я подъезжаю к дому, и мой телефон отключается от Bluetooth моей машины. При тестировании с помощью Bluetooth-гарнитуры, находясь дома, этой проблемы не возникает.
Вот файл журнала:
Код: Выделить всё
[2/21/2024 5:24 PM 22674ms] On bluetooth receive
[2/21/2024 5:24 PM 0019ms] Intent is: android.bluetooth.device.action.ACL_DISCONNECTED
[2/21/2024 5:24 PM 0026ms] Device name is: FOO
[2/21/2024 5:24 PM 0021ms] Getting location
[2/21/2024 5:24 PM 0034ms] Requested location update and waiting
[2/21/2024 5:25 PM 42009ms] Latest location was null, returning
[2/21/2024 5:25 PM 0048ms] Location is null or does not have value
[2/21/2024 5:33 PM 507948ms] location changed event.
[2/21/2024 5:33 PM 0015ms] Latitude: 123.2584486, Longitude: 456.1175657
Подробнее здесь: https://stackoverflow.com/questions/780 ... n-a-result
Мобильная версия