Using Xamarin Forms 4.1 and this is android specific code for dealing with a BT scanner. I'm connected to a Bluetooth device using BluetoothAdapter, BluetoothDevice and BluetoothSocket. Everything is is working well when the device is powered on and bonded. I'm trying to catch when the device is turned off or out of range etc...
BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
BluetoothDevice device = (from bd in adapter.BondedDevices
where bd.Name == name
select bd).FirstOrDefault();
UUID uuid = UUID.FromString(BluetoothGattServiceUuid);
BluetoothSocket socket;
if ((int) Android.OS.Build.VERSION.SdkInt >= 10) // Gingerbread 2.3.3 2.3.4
{
socket = device.CreateInsecureRfcommSocketToServiceRecord(uuid);
}
else
{
socket = device.CreateRfcommSocketToServiceRecord(uuid);
}
var reader = new InputStreamReader(socket.InputStream);
var buffer = new BufferedReader(reader);
char[] chr = new char[100];
while (ct.IsCancellationRequested == false)
{
// HERE inside this loop I need to know if the device disconnected
// something was scanned
if (buffer.Ready())
{
// process the barcode
}
}
Socket.IsConnected is always true. BluetoothAdapter.State is always On.
So how can I tell when the BT device is no longer there?