• 【Android11】AOSPSettings增加蓝牙开关


    基于Android11增加一个蓝牙开关按钮然后控制蓝牙开关

    首先控制蓝牙开关的逻辑很简单,bluetoothAdapter.disable();就可以关闭。这里需要用到android.bluetooth.BluetoothAdapter;

    1.找到蓝牙界面的xml文件加个按钮

    --- a/packages/apps/Settings/res/xml/connected_devices.xml
    +++ b/packages/apps/Settings/res/xml/connected_devices.xml
    @@ -20,6 +20,11 @@
         android:key="connected_devices_screen"
         android:title="@string/connected_devices_dashboard_title">
    
    +    <SwitchPreference
    +        android:key="toggle_bluetooth"
    +        android:title="@string/bluetooth_switch_title"
    +        android:summary="@string/bluetooth_switch_summary"/>
    + 
         <com.android.settings.slices.SlicePreference
             android:key="bt_nearby_slice"
             android:title="@string/summary_placeholder"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2. 在Fragment.java里面添加控制逻辑

    这里我曾经尝试写新的controller去控制最后没成功,然后又催的比较急。就粗暴一点直接写在Fragment里面了。

    --- a/packages/apps/Settings/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
    +++ b/packages/apps/Settings/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
    @@ -28,6 +28,11 @@ import com.android.settings.dashboard.DashboardFragment;
     import com.android.settings.search.BaseSearchIndexProvider;
     import com.android.settings.slices.SlicePreferenceController;
     import com.android.settingslib.search.SearchIndexable;
    +import androidx.preference.SwitchPreferenceCompat;
    +import androidx.preference.SwitchPreference;
    +import androidx.preference.Preference;
    +import android.bluetooth.BluetoothAdapter;
    +import android.os.Bundle;
    
     @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
     public class ConnectedDeviceDashboardFragment extends DashboardFragment {
    @@ -38,6 +43,7 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment {
         static final String KEY_CONNECTED_DEVICES = "connected_device_list";
         @VisibleForTesting
         static final String KEY_AVAILABLE_DEVICES = "available_device_list";
    +    static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
    
         @Override
         public int getMetricsCategory() {
    @@ -77,6 +83,37 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment {
                     : null);
         }
    
    +    @Override
    +    public void onCreate(Bundle savedInstanceState) {
    +        super.onCreate(savedInstanceState);
    +        SwitchPreference bluetoothSwitch = findPreference(KEY_TOGGLE_BLUETOOTH);
    +        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    +        if (bluetoothSwitch != null) {
    +            bluetoothSwitch.setChecked(bluetoothAdapter.isEnabled());
    +            bluetoothSwitch.setOnPreferenceChangeListener(
    +                new Preference.OnPreferenceChangeListener() {
    +                    @Override
    +                    public boolean onPreferenceChange(Preference preference, Object newValue) {
    +                        boolean isChecked = (Boolean) newValue;
    +                        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    +                        if (bluetoothAdapter != null) {
    +                            if (isChecked) {
    +                                if (!bluetoothAdapter.isEnabled()) {
    +                                    bluetoothAdapter.enable();
    +                                }
    +                            } else {
    +                                if (bluetoothAdapter.isEnabled()) {
    +                                    bluetoothAdapter.disable();
    +                                }
    +                            }
    +                        }
    +                        return true;
    +                    }
    +                }
    +            );
    +        }
    +    }
    +
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    一开始是写在里面自带的一个onAttach函数里面,但是发现没效果,可能是正因为函数执行的时候界面还为加载完毕所以没有获取到里面的元素。所以我自己重写了onCreate方法,写在这里面。

  • 相关阅读:
    Filebeat自定义index和fields
    mybatis一级缓存和二级缓存区别点
    用度量思维,呈现工作成果
    文件上传漏洞——文件上传检测与绕过
    [强网杯 2022]factor有感
    1008. 前序遍历构造二叉搜索树
    2023上半年京东运动鞋服市场数据分析(京东数据运营)
    【Android】画面卡顿优化列表流畅度三之RecyclerView刷新机制notifyItemRangeInserted
    Rigetti、IonQ公布2022年Q2财报后,股票大涨!
    Java中Map转对象
  • 原文地址:https://blog.csdn.net/xuanyulevel6/article/details/134523686