-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBluetoothLeService.java
350 lines (303 loc) · 11.1 KB
/
BluetoothLeService.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.example.TestTaxi.Hy_Bluetooth;
import android.app.Activity;
import android.app.ActivityManager;
import android.bluetooth.*;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.List;
/**
* 蓝牙服务管理类
*/
public class BluetoothLeService{
private BluetoothManager mBluetoothManager;//用来获取蓝牙适配器
private BluetoothAdapter mBluetoothAdapter;//蓝牙适配器,处理系统蓝牙是否打开,搜索设备
private BluetoothGatt mBluetoothGatt;//发现蓝牙服务,根据特征值处理数据交互
private BluetoothDevice mBluetoothDevice = null;//蓝牙设备
private static BluetoothLeService INSTANCE = null;//单列模式
private static Context mContext;//上下文
/**
* 是否连接
*/
private boolean isConnected = false;
public boolean isConnected() {
return isConnected;
}
public void setConnected(boolean connected) {
isConnected = connected;
}
/**
* 获取上下文
*
* @return context
*/
private Context getContext() {
return mContext;
}
private BluetoothLeService() {
boolean value = initialize();
if (!value) log("蓝牙适配器adapter初始化失败!!!");
}
/**
* 单列模式,确保唯一性
*
* @param context 上下文
* @return 对象
*/
public static BluetoothLeService getInstance(Context context,Activity... activities) {
mContext = context;
if (INSTANCE == null) {
synchronized (BluetoothLeService.class) {
if (INSTANCE == null) {
INSTANCE = new BluetoothLeService();
}
}
}
return INSTANCE;
}
/**
* 初始化bluetooth adapter
*
* @return 为null返回false
*/
private boolean initialize() {
if (mBluetoothManager == null) {
mBluetoothManager = (BluetoothManager) getContext().getSystemService(Context.BLUETOOTH_SERVICE);
}
if (mBluetoothManager == null) return false;
mBluetoothAdapter = mBluetoothManager.getAdapter();
return mBluetoothAdapter != null;
}
/**
* 启用或者禁用通知\标志返回特性 true, if the requested notification status was set successfully
*
* @param characteristic 蓝牙特征对象
* @param enabled 是否允许
* @return 设置成功或失败
*/
public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
return !(mBluetoothAdapter == null || mBluetoothGatt == null)
&& mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
}
public boolean writeDescriptor(BluetoothGattDescriptor bluetoothGattDescriptor) {
return !(bluetoothGattDescriptor == null || mBluetoothGatt == null)
&& mBluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
}
/**
* 发现数据通道服务
*
* @return true or false
*/
public boolean discoverServices() {
return !(!isConnected() || mBluetoothGatt == null)
&& mBluetoothGatt.discoverServices();
}
/**
* 读取蓝牙数据
*
* @param characteristic 蓝牙特征值
* @return true or false
*/
public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) {
return !(mBluetoothAdapter == null || mBluetoothGatt == null)
&& mBluetoothGatt.readCharacteristic(characteristic);
}
/**
* 往设备中写入数据
*
* @param characteristic 蓝牙特征值
* @return true or false
*/
public boolean writeCharecteristic(BluetoothGattCharacteristic characteristic) {
return !(mBluetoothAdapter == null || mBluetoothGatt == null)
&& mBluetoothGatt.writeCharacteristic(characteristic);
}
/**
* 获取gatt服务列表
*
* @return 远程设备提供的gatt服务列表(功能通道)
*/
public List<BluetoothGattService> getSupportedGattServices() {
if (mBluetoothGatt == null) return null;
return mBluetoothGatt.getServices();
}
/**
* 获取RSSI值
*
* @return null false
*/
public boolean getRssiValue() {
return mBluetoothGatt != null
&& mBluetoothGatt.readRemoteRssi();
}
/**
* 获取蓝牙设备对象
*
* @return BluetoothDevice
*/
public BluetoothDevice getDevice() {
return mBluetoothDevice;
}
/**
* 判断设备是否连接
*
* @param address 设备地址
* @return true 已连接
*/
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
log("蓝牙适配器没有初始化获取mac地址未指明。");
return false;
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
log("蓝牙设备没有发现,无法连接。");
return false;
}
close();//每次连接之前先释放掉原来的资源
mBluetoothGatt = device.connectGatt(getContext(), false, mGattCallback);//创建新的连接
return true;
}
/**
* 断开连接
*/
public void disconnect() {
if (mBluetoothGatt != null) mBluetoothGatt.disconnect();
}
/**
* 释放资源
*/
public void close() {
if (mBluetoothGatt != null) {
mBluetoothGatt.close();
mBluetoothDevice = null;
}
}
/**
* 蓝牙协议回调
*/
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
/**
* 连接状态
*/
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (mStateChangeListener == null) return;
if (newState == BluetoothProfile.STATE_CONNECTED) {// 连接状态
mStateChangeListener.connected(gatt.getDevice());
setConnected(true);
mBluetoothDevice = gatt.getDevice();
discoverServices();//设备连接成功,查找服务!
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {// 断开连接
close();
mStateChangeListener.disconnected();
setConnected(false);
mBluetoothDevice = null;
}
}
/**
* 是否发现服务
*/
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
if (mCharacteristicListener != null)
mCharacteristicListener.onServicesDiscovered();
} else {
log("蓝牙通信服务回调失败:status=" + status);
}
}
/**
* 读操作回调
*/
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (mCharacteristicListener != null) {
mCharacteristicListener.onCharacteristicRead(gatt, characteristic, status);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
if (mCharacteristicListener != null) {
mCharacteristicListener.onCharacteristicChanged(gatt, characteristic);
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (mCharacteristicListener != null) {
mCharacteristicListener.onCharacteristicWrite(gatt, characteristic, status);
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
log("onDescriptorWrite: status=" + status + " uuid=" + descriptor.getUuid().toString());
}
/**
* 信号强度
*/
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
if (mCharacteristicListener != null) {
mCharacteristicListener.onReadRemoteRssi(gatt, rssi, status);
}
}
};
/**
* 连接状态接口
*/
public interface OnConnectionStateChangeListener {
void connected(BluetoothDevice device);
void disconnected();
}
/**
* 发现服务、数据读写操作接口
*/
public interface OnCharacteristicListener {
void onServicesDiscovered();
void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status);
void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic);
void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status);
void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status);
}
private OnConnectionStateChangeListener mStateChangeListener = null;
private OnCharacteristicListener mCharacteristicListener = null;
public void setOnConnectionStateChangeListener(OnConnectionStateChangeListener listener) {
this.mStateChangeListener = listener;
}
public void setOnCharacteristicListener(OnCharacteristicListener listener) {
this.mCharacteristicListener = listener;
}
public void startAlarmService(){
//启动警报蓝牙服务
if (!isServiceWork()){
mContext.startService(new Intent(mContext, BluetoothService.class));
}
}
/**
* 判断服务是否正在运行
* @return true代表正在运行,false代表服务没有正在运行
*/
public boolean isServiceWork() {
boolean isWork = false;
ActivityManager myAM = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
if (myList.size() <= 0) {
return false;
}
for (int i = 0; i < myList.size(); i++) {
String mName = myList.get(i).service.getClassName().toString();
if (mName.equals("com.example.TestTaxi.Hy_Bluetooth.BluetoothService")) {
isWork = true;
break;
}
}
return isWork;
}
public static void log(String msg)
{
Log.d("Bluetooth", "-------------->蓝牙服务日志 : " + msg + " <----------------");
}
}