在Android的后台服务中获取Bluetooth BLE状态

时间:2019-01-09 06:35:32

标签: android bluetooth xamarin.android bluetooth-lowenergy

我在Bluetooth Gatt回调中具有以下方法,该方法可响应任何蓝牙连接更改:

public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
{
    base.OnConnectionStateChange(gatt, status, newState);

    if(newState == ProfileState.Connected)
    {
        gatt.DiscoverServices();
    }

    else if(newState == ProfileState.Disconnected)
    {
        gatt.Close();
        Log.Info("BLE", "Status: Disconnected");
    }

}

在我的后台服务中,我想监听BLE设备连接或断开连接时是否存在断开连接。

我的服务

 [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            try
            {
                Toast.MakeText(this, "Background service started", ToastLength.Long);

// I want to listen for any connectivity changes here

            base.OnStartCommand(intent, flags, startId);
            return StartCommandResult.Sticky;

        }

任何人都可以帮助我实现这一目标吗?

1 个答案:

答案 0 :(得分:1)

首先,可以创建一个BroadcastReceiver来监听蓝牙的状态,如下所示:

public class BluetoothStateBroadcastReceive : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
        string action = intent.Action;
        BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
        switch (action)
        {
            case BluetoothDevice.ActionAclConnected:
                Toast.MakeText(context, "BluetoothDevice:" + device.Name + "Connected", ToastLength.Long).Show();
                break;
            case BluetoothDevice.ActionAclDisconnected:
                Toast.MakeText(context, "BluetoothDevice:" + device.Name + "Disconnected", ToastLength.Short).Show();
                break;
            case BluetoothAdapter.ActionStateChanged:
                Toast.MakeText(context, "BluetoothDevice:" + device.Name + "Changed", ToastLength.Short).Show();
                //int blueState = intent.GetIntExtra(BluetoothAdapter.ExtraState, 0);
                //switch (blueState)
                //{
                //    case BluetoothAdapter.STATE_OFF:
                //        Toast.MakeText(context, "off", ToastLength.Short).Show();
                //        break;
                //    case BluetoothAdapter.STATE_ON:
                //        Toast.MakeText(context, "on", ToastLength.Short).Show();
                //        break;
                //}
                break;
        }
    }
}

service中,您可以注册它:

public class BluetoothService : Service
{
    private BluetoothStateBroadcastReceive mReceive;

    public BluetoothService()
    {
    }

    public BluetoothService(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        registerBluetoothReceiver();
        return base.OnStartCommand(intent, flags, startId);
    }

    public override IBinder OnBind(Intent intent)
    {
        throw new NotImplementedException();
    }

    private void registerBluetoothReceiver()
    {
        if (mReceive == null)
        {
            mReceive = new BluetoothStateBroadcastReceive();
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.AddAction(BluetoothAdapter.ActionStateChanged);
        intentFilter.AddAction(BluetoothDevice.ActionAclConnected);
        intentFilter.AddAction(BluetoothDevice.ActionAclDisconnected);
        //intentFilter.AddAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        //intentFilter.AddAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        RegisterReceiver(mReceive, intentFilter);
    }

    public override void OnDestroy()
    {
        unregisterBluetoothReceiver();
        base.OnDestroy();
    }

    private void unregisterBluetoothReceiver()
    {
        if (mReceive != null)
        {
            UnregisterReceiver(mReceive);
            mReceive = null;
        }
    }
}

然后您可以在所需的地方尝试使用。service时,可以参考此document