-
Notifications
You must be signed in to change notification settings - Fork 19
/
BasicBLEActivity.java
166 lines (143 loc) · 6.45 KB
/
BasicBLEActivity.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
package com.ublox.BLE.activities;
import android.app.Activity;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import com.ublox.BLE.R;
import com.ublox.BLE.services.BluetoothLeService;
import java.util.List;
public class BasicBLEActivity extends Activity {
private static final String DEVICE_UUID = ""; // The UUID for the device that we wanna connect to.
private boolean mConnected = false; // True if we are connected to a device.
private BluetoothLeService mBluetoothLeService;
private List<BluetoothGattService> mServices; // Were we save all services that the device have
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_ble);
// Bind service with the activity
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onResume() {
super.onResume();
// register the receiver for the service to be able ro receive data
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
if (mBluetoothLeService != null) {
mBluetoothLeService.connect(DEVICE_UUID);
}
}
@Override
protected void onPause() {
super.onPause();
// Disconnect from the device and close the connection
// Try/Catch because Androids Bluetooth implementation may crash on some devices.
try {
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
mConnected = false;
} catch (Exception ignore) {}
// Unregister the receiver
unregisterReceiver(mGattUpdateReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Unbind from the service so that it can shutdown properly
unbindService(mServiceConnection);
mBluetoothLeService = null;
}
/**
* Tells the service that we wanna read some data
* @param characteristic The characteristic that we wanna read
*/
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
try {
mBluetoothLeService.readCharacteristic(characteristic);
} catch (Exception ignore) {}
}
/**
* Function to tell the service that we wanna write some data to the device
* @param characteristic The characteristic that we wanna write to
* @param value The value that we wanna write to the characteristic
*/
public void writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] value) {
try {
mBluetoothLeService.writeCharacteristic(characteristic, value);
} catch (Exception ignore) {}
}
/**
* Tells the service that we want notifications for the characteristic
* @param characteristic The characteristic that we want to get notified about
*/
public void notifyCharacteristic(BluetoothGattCharacteristic characteristic) {
try {
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
} catch (Exception ignore) {}
}
/**
* Function for sending a message to the serial service on the device
* @param characteristic The fifo characteristic
* @param message The message that are going to be sent as a byte array
*/
public void sendMessageToSerial(BluetoothGattCharacteristic characteristic, byte[] message) {
try {
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
mBluetoothLeService.writeCharacteristic(characteristic, message);
} catch (Exception ignore) {}
}
// Code to manage Service lifecycle.
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
finish();
}
// Automatically connects to the device upon successful start-up initialization.
mBluetoothLeService.connect(DEVICE_UUID);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
public final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
mServices = mBluetoothLeService.getSupportedGattServices();
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
byte[] extraData = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA); // The data that we received from the device
} else if (BluetoothLeService.ACTION_RSSI_UPDATE.equals(action)) {
int rssi = intent.getIntExtra(BluetoothLeService.EXTRA_RSSI, 0); // Device current rssi
}
}
};
/**
* This is were we create the intent filter that will tell the service what we are interested in.
* @return IntentFilter for handling the BLEService
*/
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
intentFilter.addAction(BluetoothLeService.ACTION_RSSI_UPDATE);
return intentFilter;
}
}