Maui控制蓝牙

Maui控制蓝牙

  1. 安卓
    安卓蓝牙权限在AndroidManifest.xml文件中设置。在安卓设备上开启程序的位置获取权限。

    1
    2
    3
    4
    5
    
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> // 蓝牙扫描需要开启
    

    使用包InTheHand.Bluetooth.Permissions检查权限,代码var state = await Permissions.RequestAsync<InTheHand.Bluetooth.Permissions.Bluetooth>();
    检查蓝牙是否打开。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    #if ANDROID
    
                var bluetoothManager = (Android.Bluetooth.BluetoothManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.BluetoothService);
                if (bluetoothManager != null)
                {
                    var bluetoothAdapter = bluetoothManager.Adapter;
                    if (bluetoothAdapter != null && bluetoothAdapter.IsEnabled == false)
                    {
                        bluetoothAdapter.Enable();
                        //var enable = new Android.Content.Intent(Android.Bluetooth.BluetoothAdapter.ActionRequestEnable);
                        //enable.SetFlags(Android.Content.ActivityFlags.NewTask);
                        //Android.App.Application.Context.StartActivity(enable);
                    }
                }
    #endif
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    #if ANDROID
    
            var bluetoothManager = (Android.Bluetooth.BluetoothManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.BluetoothService);
            var bluetoothAdapter = bluetoothManager.Adapter;
            if(bluetoothAdapter.IsEnabled == true )
            {
                bluetoothAdapter.Disable();
            }
            else
            {
                bluetoothAdapter.Enable();
            }
    #endif
    

    You can use the following code after requesting the permission and it work on all the android versions:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    #if ANDROID
            var enable = new Android.Content.Intent(
                        Android.Bluetooth.BluetoothAdapter.ActionRequestEnable);
            enable.SetFlags(Android.Content.ActivityFlags.NewTask);
    
            var disable = new Android.Content.Intent(
                        Android.Bluetooth.BluetoothAdapter.ActionRequestDiscoverable);
            disable.SetFlags(Android.Content.ActivityFlags.NewTask);
    
              var bluetoothManager = (Android.Bluetooth.BluetoothManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.BluetoothService);
              var bluetoothAdapter = bluetoothManager.Adapter;
              if(bluetoothAdapter.IsEnabled == true )
              {
                Android.App.Application.Context.StartActivity(disable);
                //  disable the bluetooth;
            }
            else
              {
                // enable the bluetooth
                Android.App.Application.Context.StartActivity(enable);
    
            }
    #endif
    
  2. Windows

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy