How can I detect a Bluetooth device disconnect in XF Android?

MarkErickson.91MarkErickson.91 ✭✭USMember ✭✭

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?

Answers

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    In the android, you can try to create a BroadcastReceiver to receive the following broadcast to detect the .

    <intent-filter> 
        <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
        <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
        <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
    </intent-filter>
    

    You had better create a foreground service(keep the service always running when you use your application), then registers a broadcast receiver for Bluetooth status change, so your app knows when Bluetooth was turned off like this thread.
    https://stackoverflow.com/questions/13147653/android-bluetooth-detect-a-disconnection-from-a-device

  • MarkErickson.91MarkErickson.91 ✭✭ USMember ✭✭

    Thanks @LeonLu I'll give that a try. I did see that SO thread but assumed there would be something more Forms related. I guess it is specific to Android. :) Thanks again! I'll post what I came up with.

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    Ok, waitting for your update.

  • MarkErickson.91MarkErickson.91 ✭✭ USMember ✭✭

    So I added the Broadcast receiver and registered a variety of intents. I am still not getting any indication when the device is turned off or goes out of range. There is not much in the OnReceive as I just want to see if this is possible using this approach. Any thoughts on what I'm missing?

    in MainActivity.cs

    _receiver = new BluetoothBroadcastReceiver();
    RegisterReceiver(_receiver, new IntentFilter(BluetoothAdapter.ActionConnectionStateChanged));
    RegisterReceiver(_receiver, new IntentFilter(BluetoothAdapter.ActionStateChanged));
    RegisterReceiver(_receiver, new IntentFilter(BluetoothAdapter.ExtraConnectionState));
    RegisterReceiver(_receiver, new IntentFilter(BluetoothAdapter.ActionDiscoveryStarted));
    RegisterReceiver(_receiver, new IntentFilter(BluetoothDevice.ActionAclConnected));
    RegisterReceiver(_receiver, new IntentFilter(BluetoothDevice.ActionAclDisconnectRequested));
    RegisterReceiver(_receiver, new IntentFilter(BluetoothDevice.ActionAclDisconnected));
    

    BluetoothBroadcastRecevier.cs

        [BroadcastReceiver]
        public class BluetoothBroadcastReceiver : BroadcastReceiver
        {
                public override void OnReceive(Context context, Intent intent)
                {
                    string action = intent.Action;
                }
        }
    
  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    What is your device that you used? I test in pixel it worked wht following IntentFilter

    IntentFilter f1 = new IntentFilter(BluetoothDevice.ActionAclDisconnectRequested  );
    IntentFilter f2 = new IntentFilter(BluetoothDevice.ActionAclDisconnected );
    
Sign In or Register to comment.