forked from dotnet/android-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceControlActivity.cs
315 lines (270 loc) · 11.1 KB
/
DeviceControlActivity.cs
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
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Bluetooth;
using Android.Util;
namespace BluetoothLeGatt
{
/**
* For a given BLE device, this Activity provides the user interface to connect, display data,
* and display GATT services and characteristics supported by the device. The Activity
* communicates with {@code BluetoothLeService}, which in turn interacts with the
* Bluetooth LE API.
*/
[Activity (Label = "DeviceControlActivity")]
public class DeviceControlActivity : Activity
{
public readonly static String TAG = typeof (DeviceControlActivity).Name;
public static readonly String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
public static readonly String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
TextView mConnectionState;
TextView mDataField;
String mDeviceName;
public static String mDeviceAddress;
ExpandableListView mGattServicesList;
public static BluetoothLeService mBluetoothLeService;
List <List <BluetoothGattCharacteristic>> mGattCharacteristics =
new List <List <BluetoothGattCharacteristic>> ();
public static bool mConnected = false;
BluetoothGattCharacteristic mNotifyCharacteristic;
private readonly String LIST_NAME = "NAME";
private readonly String LIST_UUID = "UUID";
private ServiceManager mServiceManager;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.gatt_services_characteristics);
mServiceManager = new ServiceManager (this);
Intent intent = Intent;
mDeviceName = intent.GetStringExtra (EXTRAS_DEVICE_NAME);
mDeviceAddress = intent.GetStringExtra (EXTRAS_DEVICE_ADDRESS);
// Sets up UI references.
(FindViewById <TextView> (Resource.Id.device_address)).Text = mDeviceAddress;
mGattServicesList = FindViewById <ExpandableListView> (Resource.Id.gatt_services_list) ;
mGattServicesList.ChildClick += delegate(object sender, ExpandableListView.ChildClickEventArgs e) {
if (mGattCharacteristics != null) {
var groupPosition = mGattCharacteristics [e.GroupPosition];
BluetoothGattCharacteristic characteristic = groupPosition [e.ChildPosition];
var charaProp = characteristic.Properties;
if ((charaProp & GattProperty.Read) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.SetCharacteristicNotification (
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.ReadCharacteristic (characteristic);
}
if ((charaProp & GattProperty.Notify) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.SetCharacteristicNotification (characteristic, true);
}
}
};
mConnectionState = FindViewById <TextView> (Resource.Id.connection_state);
mDataField = FindViewById <TextView> (Resource.Id.data_value);
ActionBar.Title = mDeviceName;
ActionBar.SetDisplayHomeAsUpEnabled (true);
Intent gattServiceIntent = new Intent(this, typeof (BluetoothLeService));
BindService (gattServiceIntent, mServiceManager, Bind.AutoCreate);
}
protected override void OnResume ()
{
base.OnResume ();
RegisterReceiver (mServiceManager, MakeGattUpdateIntentFilter ());
if (mBluetoothLeService != null) {
bool result = mBluetoothLeService.Connect (mDeviceAddress);
Log.Debug (TAG, "Connect request result=" + result);
}
}
protected override void OnPause ()
{
base.OnPause ();
UnregisterReceiver (mServiceManager);
}
protected override void OnDestroy ()
{
base.OnDestroy ();
UnbindService (mServiceManager);
mBluetoothLeService = null;
}
public override bool OnCreateOptionsMenu (IMenu menu)
{
MenuInflater.Inflate (Resource.Menu.gatt_services, menu);
if (mConnected) {
menu.FindItem (Resource.Id.menu_connect).SetVisible (false);
menu.FindItem (Resource.Id.menu_disconnect).SetVisible (true);
} else {
menu.FindItem (Resource.Id.menu_connect).SetVisible (true);
menu.FindItem (Resource.Id.menu_disconnect).SetVisible (false);
}
return true;
}
public override bool OnOptionsItemSelected (IMenuItem item)
{
switch (item.ItemId) {
case Resource.Id.menu_connect:
mBluetoothLeService.Connect (mDeviceAddress);
return true;
case Resource.Id.menu_disconnect:
mBluetoothLeService.Disconnect ();
return true;
case Android.Resource.Id.Home:
OnBackPressed();
return true;
}
return base.OnOptionsItemSelected (item);
}
public void ClearUI ()
{
mGattServicesList.SetAdapter ((SimpleExpandableListAdapter) null);
mDataField.SetText (Resource.String.no_data);
}
public void UpdateConnectionState (int resourceId)
{
RunOnUiThread (new Action (delegate {
mConnectionState.SetText (resourceId);
}));
}
public void DisplayData (String data)
{
if (data != null) {
mDataField.Text = data;
}
}
// Demonstrates how to iterate through the supported GATT Services/Characteristics.
// In this sample, we populate the data structure that is bound to the ExpandableListView
// on the UI.
public void DisplayGattServices (IList<BluetoothGattService> gattServices)
{
if (gattServices == null)
return;
String uuid = null;
String unknownServiceString = Resources.GetString (Resource.String.unknown_service);
String unknownCharaString = Resources.GetString (Resource.String.unknown_characteristic);
List <Dictionary <String, Object>> gattServiceData = new List <Dictionary <String, Object>> ();
List <List <Dictionary <String, String>>> gattCharacteristicData
= new List <List <Dictionary <String, String>>> ();
mGattCharacteristics = new List <List <BluetoothGattCharacteristic>> ();
// Loops through available GATT Services.
foreach (BluetoothGattService gattService in gattServices) {
Dictionary <String, Object> currentServiceData = new Dictionary <String, Object>();
uuid = gattService.Uuid.ToString ();
currentServiceData.Add (
LIST_NAME, SampleGattAttributes.Lookup (uuid, unknownServiceString));
currentServiceData.Add (LIST_UUID, uuid);
gattServiceData.Add (currentServiceData);
List <Dictionary <String, String>> gattCharacteristicGroupData =
new List <Dictionary <String, String>>();
IList <BluetoothGattCharacteristic> gattCharacteristics =
gattService.Characteristics;
List <BluetoothGattCharacteristic> charas =
new List<BluetoothGattCharacteristic> ();
// Loops through available Characteristics.
foreach (BluetoothGattCharacteristic gattCharacteristic in gattCharacteristics) {
charas.Add (gattCharacteristic);
Dictionary <String, String> currentCharaData = new Dictionary <String, String>();
uuid = gattCharacteristic.Uuid.ToString();
currentCharaData.Add (
LIST_NAME, SampleGattAttributes.Lookup(uuid, unknownCharaString));
currentCharaData.Add (LIST_UUID, uuid);
gattCharacteristicGroupData.Add (currentCharaData);
}
mGattCharacteristics.Add (charas);
gattCharacteristicData.Add (gattCharacteristicGroupData);
}
SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter (
this,
(IList<IDictionary<String, Object>>) gattServiceData,
Android.Resource.Layout.SimpleExpandableListItem2,
new String[] {LIST_NAME, LIST_UUID},
new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 },
(IList<IList<IDictionary<String, Object>>>) gattCharacteristicData,
Android.Resource.Layout.SimpleExpandableListItem2,
new String[] {LIST_NAME, LIST_UUID},
new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }
);
mGattServicesList.Adapter = (IListAdapter) gattServiceAdapter;
}
private static IntentFilter MakeGattUpdateIntentFilter ()
{
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);
return intentFilter;
}
}
// Code to manage Service lifecycle.
// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device. This can be a result of read
// or notification operations.
class ServiceManager : BroadcastReceiver, IServiceConnection
{
DeviceControlActivity DCActivity;
public ServiceManager (DeviceControlActivity dca)
{
DCActivity = dca;
}
public void OnServiceConnected (ComponentName componentName, IBinder service)
{
DeviceControlActivity.mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).GetService ();
if (!DeviceControlActivity.mBluetoothLeService.Initialize ()) {
Log.Error (DeviceControlActivity.TAG, "Unable to initialize Bluetooth");
DCActivity.Finish ();
}
// Automatically connects to the device upon successful start-up initialization.
DeviceControlActivity.mBluetoothLeService.Connect (DeviceControlActivity.mDeviceAddress);
}
public void OnServiceDisconnected (ComponentName componentName)
{
DeviceControlActivity.mBluetoothLeService = null;
}
public override void OnReceive (Context context, Intent intent)
{
String action = intent.Action;
if (BluetoothLeService.ACTION_GATT_CONNECTED == action) {
DeviceControlActivity.mConnected = true;
DCActivity.UpdateConnectionState (Resource.String.connected);
DCActivity.InvalidateOptionsMenu ();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED == action) {
DeviceControlActivity.mConnected = false;
DCActivity.UpdateConnectionState(Resource.String.disconnected);
DCActivity.InvalidateOptionsMenu();
DCActivity.ClearUI ();
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED == action) {
// Show all the supported services and characteristics on the user interface.
DCActivity.DisplayGattServices (DeviceControlActivity.mBluetoothLeService.GetSupportedGattServices ());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE == action) {
DCActivity.DisplayData (intent.GetStringExtra (BluetoothLeService.EXTRA_DATA));
}
}
}
}