<div class="blogpost-body" id="cnblogs_post_body">
<p><strong>内容简介:</strong>本文详细分析了android4.0 中蓝牙使能的过程,相比较android2.3,4.0中的蓝牙最大的差别在于UI上on/off的伪开关。在android4.0中加入了 adapter的状态机。所谓的状态机就类似于状态转换图,在一个状态收到某个特定的命令会变成另外一个状态,不同的命令可以跳转到不同的状态(当然也有 可能到同一状态)。adapter的初始状态为poweroff,在android系统启动的时候会进入warmup状态,同时会进行UUID的add, 该操作会引起propertychanged的UUID signal,该signal会使得状态从warmup变换到hotoff状态。因此在UI端off时其实adapter已经处于hotoff状态而不是 poweroff状态。这一点是很关键的。在正文中,我会从假如我不知道这些开始来描绘整个使能的过程。</p>
<p><strong>正文:</strong></p>
<p>毫无疑问,bluetooth的打开是在Settings中进行的操作。因此,冤有头,债有主,我们来到了Settings.java中,果然发现了相关的代码如下:</p>
<p>mBluetoothEnabler =new BluetoothEnabler(context, new Switch(context));</p>
<p>于是,我们得以进入真正的蓝牙操作的殿堂,好好进去看看吧。</p>
<p><strong>代码来源:<a href="http://code.metager.de/source/xref/android/">http://code.metager.de/source/xref/android/</a></strong></p>
<div>
<div>
<a title="复制代码"><img alt="复制代码" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-48304ba5e6f9fe08f3fa1abda7d326ab.gif"></a>
</div>
<pre class="blockcode">
1 <strong>1、BluetoothEnabler的构造函数 </strong>
2
3
4
5 public BluetoothEnabler(Context context,Switch switch_) {
6
7 mContext = context;
8
9 mSwitch = switch_;
10
11 //很简单了,去调用一个LocalBluetoothManager类的getInstance,其实会构造该类的
12
13 LocalBluetoothManager manager =LocalBluetoothManager.getInstance(context);
14
15 if (manager == null) {
16
17 // Bluetooth is not supported
18
19 mLocalAdapter = null;
20
21 mSwitch.setEnabled(false);
22
23 } else {
24
25 //构造成功后,通过manager得到bluetooth的adapter
26
27 mLocalAdapter =manager.getBluetoothAdapter();
28
29 }
30
31 //同时新建一个intent,用于接收ACTION_STATE_CHANGED
32
33 mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
34
35 }
36
37
38
39 <strong>2、LocalBluetoothManager类的getInstance </strong> 40
41 public static synchronized LocalBluetoothManager getInstance(Context context) {
42
43 if (sInstance == null) {
44
45 //2.1同样的,这个会去调用LocalBluetoothAdapter的getInstance,也会构造该类
46
47 LocalBluetoothAdapter adapter =LocalBluetoothAdapter.getInstance();
48
49 if (adapter == null) {
50
51 return null;
52
53 }
54
55 // This will be around as long asthis process is
56
57 Context appContext =context.getApplicationContext();
58
59 //2.2构造LocalBluetoothManager类
60
61 sInstance = new LocalBluetoothManager(adapter, appContext);
62
63 }
64
65
66
67 return sInstance;
68
69 }
70
71 <strong>2.1 LocalBluetoothAdapter的getInstance </strong> 72
73
74
75 static synchronized LocalBluetoothAdapter getInstance() {
76
77 if (sInstance == null) {
78
79 //2.1.1通过BluetoothAdapter得到DefaultAdapter
80
81 BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();
82
83 if (adapter != null) {
84
85 //2.1.2若有该DefaultAdapter,则构造LocalBluetoothAdapter
86
87 sInstance = new LocalBluetoothAdapter(adapter);
88
89 }
90
91 }
92
93
94
95 return sInstance;
96
97 }
98
99
100
101 <strong>2.1.1BluetoothAdapter得到DefaultAdapter </strong> 102
103
104
105 public static synchronized BluetoothAdapter getDefaultAdapter() {
106
107 if (sAdapter == null) {
108
109 IBinder b =ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
110
111 if (b != null) {
112
113 IBluetooth service =IBluetooth.Stub.asInterface(b);
114
115 sAdapter = new BluetoothAdapter(service);
116
117 }
118
119 }
120
121 return sAdapter;
122
123 }
124
125
126
127 <strong>2.1.2构造LocalBluetoothAdapter </strong> 128
129 //其实就是 mAdapter的初始化而已
130
131 private LocalBluetoothAdapter(BluetoothAdapter adapter) {
1 |
|