I wanted to write a simple application that sends bits to a bluetooth device. Currently I don't know what is wrong and I don't know where I got the bug. I've got all permissions and i am asking for them (only localization showing up on screen)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I am using Plugin.BLE NuGet package. The StateChangedEvent works just fine. However when I scan no devices are found (even though I have devices available for discovery).
public partial class MainPage : ContentPage
{
IBluetoothLE ble;
IAdapter adapter;
...
}
public MainPage()
{
InitializeComponent();
ble = CrossBluetoothLE.Current;
adapter = CrossBluetoothLE.Current.Adapter;
Devices = new ObservableCollection<Device_bluetooth>();
ble.StateChanged += (s, e) =>
{
labelTest.Text=($"The bluetooth state changed to {e.NewState}");
DisplayAlert("Notice", e.NewState.ToString(), "OK !");
};
}
The function for scanning to discover devices below:
// Issue is here
private async void ScanAsync()
{
try
{
Devices.Clear();
adapter.DeviceDiscovered += (s, a) =>
{
Devices.Add(new Device_bluetooth() { Device = a.Device, Name = "Name: " + a.Device.Name.ToString(), Uuid = "UUID:" + a.Device.Id.ToString() });
};
if (!ble.Adapter.IsScanning)
{
await adapter.StartScanningForDevicesAsync();
}
}
catch (Exception ex)
{
await DisplayAlert("Notice", ex.Message.ToString(), "Error !");
}
Device_bluetooth is my class for for storing devices, and I am showing them in a ListView like below:
<ListView x:Name="mylistview" ItemsSource="{Binding Devices}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="100">
<StackLayout>
<Frame BackgroundColor="White"
HasShadow="true">
<StackLayout HeightRequest="100">
<Label Text="{Binding Name ,Mode=TwoWay}" />
<Label Text="{Binding Uuid ,Mode=TwoWay}" />
</StackLayout>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Please help. I have tried almost everything and I cannot cope with it.