http://www.3aj.cn/android/article/1/1538.html
最近工作中遇到一个特殊的需求,要求代码能够从后台开机android手机蓝牙的可见性。而framework提供了一种打开可见性的操作,就是通过向用户弹出一个提示框,来询问是否允许开启可见性。而且限制了最长时间为300秒,代码如下:
<span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="com" style="color: rgb(119, 0, 0);">//启动修改蓝牙可见性的Intent</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">Intent</span><span class="pln" style="color: rgb(34, 34, 34);"> intent </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">new</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">Intent</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">ACTION_REQUEST_DISCOVERABLE</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="com" style="color: rgb(119, 0, 0);">//设置蓝牙可见性的时间,方法本身规定最多可见300秒</span><span class="pln" style="color: rgb(34, 34, 34);"> intent</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">putExtra</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">EXTRA_DISCOVERABLE_DURATION</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="lit" style="color: rgb(25, 95, 145);">300</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> startActivity</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">intent</span><span class="pun" style="color: rgb(25, 95, 145);">);</span>
但通过android的自带的settings程序,我们可以直接开机蓝牙可见性。所以下载settings的源码,进行分析。找到了开启蓝牙可见性的代码,如下:
<span class="kwd" style="color: rgb(0, 0, 255);">private</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">void</span><span class="pln" style="color: rgb(34, 34, 34);"> setEnabled</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="kwd" style="color: rgb(0, 0, 255);">boolean</span><span class="pln" style="color: rgb(34, 34, 34);"> enable</span><span class="pun" style="color: rgb(25, 95, 145);">)</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">if</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">enable</span><span class="pun" style="color: rgb(25, 95, 145);">)</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pln" style="color: rgb(34, 34, 34);"> timeout </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="pln" style="color: rgb(34, 34, 34);"> getDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">();</span><span class="pln" style="color: rgb(34, 34, 34);"> mLocalAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">timeout</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">long</span><span class="pln" style="color: rgb(34, 34, 34);"> endTimestamp </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">System</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">currentTimeMillis</span><span class="pun" style="color: rgb(25, 95, 145);">()</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">+</span><span class="pln" style="color: rgb(34, 34, 34);"> timeout </span><span class="pun" style="color: rgb(25, 95, 145);">*</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="lit" style="color: rgb(25, 95, 145);">1000L</span><span class="pun" style="color: rgb(25, 95, 145);">;</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">LocalBluetoothPreferences</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">persistDiscoverableEndTimestamp</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">mContext</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> endTimestamp</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> mLocalAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setScanMode</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">SCAN_MODE_CONNECTABLE_DISCOVERABLE</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> timeout</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> updateCountdownSummary</span><span class="pun" style="color: rgb(25, 95, 145);">();</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">else</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> mLocalAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setScanMode</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">SCAN_MODE_CONNECTABLE</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span>
<span class="kwd" style="color: rgb(0, 0, 255);">public</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">void</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pln" style="color: rgb(34, 34, 34);"> timeout</span><span class="pun" style="color: rgb(25, 95, 145);">)</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pln" style="color: rgb(34, 34, 34);"> adapter</span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">getDefaultAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">();</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">try</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">Method</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">getMethod</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="str" style="color: rgb(0, 136, 0);">"setDiscoverableTimeout"</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setAccessible</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="kwd" style="color: rgb(0, 0, 255);">true</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">Method</span><span class="pln" style="color: rgb(34, 34, 34);"> setScanMode </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">getMethod</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="str" style="color: rgb(0, 136, 0);">"setScanMode"</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setScanMode</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setAccessible</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="kwd" style="color: rgb(0, 0, 255);">true</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">invoke</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">adapter</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> timeout</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setScanMode</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">invoke</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">adapter</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">SCAN_MODE_CONNECTABLE_DISCOVERABLE</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);">timeout</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">catch</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="typ" style="color: teal;">Exception</span><span class="pln" style="color: rgb(34, 34, 34);"> e</span><span class="pun" style="color: rgb(25, 95, 145);">)</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> e</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">printStackTrace</span><span class="pun" style="color: rgb(25, 95, 145);">();</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span>
<span class="kwd" style="color: rgb(0, 0, 255);">public</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">void</span><span class="pln" style="color: rgb(34, 34, 34);"> closeDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">()</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pln" style="color: rgb(34, 34, 34);"> adapter</span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">getDefaultAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">();</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">try</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">Method</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">getMethod</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="str" style="color: rgb(0, 136, 0);">"setDiscoverableTimeout"</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setAccessible</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="kwd" style="color: rgb(0, 0, 255);">true</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">Method</span><span class="pln" style="color: rgb(34, 34, 34);"> setScanMode </span><span class="pun" style="color: rgb(25, 95, 145);">=</span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">getMethod</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="str" style="color: rgb(0, 136, 0);">"setScanMode"</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="kwd" style="color: rgb(0, 0, 255);">int</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="kwd" style="color: rgb(0, 0, 255);">class</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setScanMode</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">setAccessible</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="kwd" style="color: rgb(0, 0, 255);">true</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setDiscoverableTimeout</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">invoke</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">adapter</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="lit" style="color: rgb(25, 95, 145);">1</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> setScanMode</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">invoke</span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="pln" style="color: rgb(34, 34, 34);">adapter</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="typ" style="color: teal;">BluetoothAdapter</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">SCAN_MODE_CONNECTABLE</span><span class="pun" style="color: rgb(25, 95, 145);">,</span><span class="lit" style="color: rgb(25, 95, 145);">1</span><span class="pun" style="color: rgb(25, 95, 145);">);</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="kwd" style="color: rgb(0, 0, 255);">catch</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">(</span><span class="typ" style="color: teal;">Exception</span><span class="pln" style="color: rgb(34, 34, 34);"> e</span><span class="pun" style="color: rgb(25, 95, 145);">)</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">{</span><span class="pln" style="color: rgb(34, 34, 34);"> e</span><span class="pun" style="color: rgb(25, 95, 145);">.</span><span class="pln" style="color: rgb(34, 34, 34);">printStackTrace</span><span class="pun" style="color: rgb(25, 95, 145);">();</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span><span class="pln" style="color: rgb(34, 34, 34);"> </span><span class="pun" style="color: rgb(25, 95, 145);">}</span>
如果想实现超时后自动关闭可见性的效果,使用Handler
以上代码在android4.2以上可以允许,4.2以下会因为缺少系统权限而运行失败。