Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
pain/svn/pristine/44/44c7079a3b2b9e7c041f60a3eedeede315d71db1.svn-base@0:832122ce6748, 2021-04-07 (annotated)
- Committer:
- darienf
- Date:
- Wed Apr 07 22:37:59 2021 +0000
- Revision:
- 0:832122ce6748
painnie
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darienf | 0:832122ce6748 | 1 | /******************************************************************************* |
darienf | 0:832122ce6748 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
darienf | 0:832122ce6748 | 3 | * <p> |
darienf | 0:832122ce6748 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
darienf | 0:832122ce6748 | 5 | * copy of this software and associated documentation files (the "Software"), |
darienf | 0:832122ce6748 | 6 | * to deal in the Software without restriction, including without limitation |
darienf | 0:832122ce6748 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
darienf | 0:832122ce6748 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
darienf | 0:832122ce6748 | 9 | * Software is furnished to do so, subject to the following conditions: |
darienf | 0:832122ce6748 | 10 | * <p> |
darienf | 0:832122ce6748 | 11 | * The above copyright notice and this permission notice shall be included |
darienf | 0:832122ce6748 | 12 | * in all copies or substantial portions of the Software. |
darienf | 0:832122ce6748 | 13 | * <p> |
darienf | 0:832122ce6748 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
darienf | 0:832122ce6748 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
darienf | 0:832122ce6748 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
darienf | 0:832122ce6748 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
darienf | 0:832122ce6748 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
darienf | 0:832122ce6748 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
darienf | 0:832122ce6748 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
darienf | 0:832122ce6748 | 21 | * <p> |
darienf | 0:832122ce6748 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
darienf | 0:832122ce6748 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
darienf | 0:832122ce6748 | 24 | * Products, Inc. Branding Policy. |
darienf | 0:832122ce6748 | 25 | * <p> |
darienf | 0:832122ce6748 | 26 | * The mere transfer of this software does not imply any licenses |
darienf | 0:832122ce6748 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
darienf | 0:832122ce6748 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
darienf | 0:832122ce6748 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
darienf | 0:832122ce6748 | 30 | * ownership rights. |
darienf | 0:832122ce6748 | 31 | * ****************************************************************************** |
darienf | 0:832122ce6748 | 32 | */ |
darienf | 0:832122ce6748 | 33 | package com.example.android.bluetoothlegatt; |
darienf | 0:832122ce6748 | 34 | |
darienf | 0:832122ce6748 | 35 | import android.app.Activity; |
darienf | 0:832122ce6748 | 36 | import android.app.ListActivity; |
darienf | 0:832122ce6748 | 37 | import android.bluetooth.BluetoothAdapter; |
darienf | 0:832122ce6748 | 38 | import android.bluetooth.BluetoothDevice; |
darienf | 0:832122ce6748 | 39 | import android.bluetooth.BluetoothManager; |
darienf | 0:832122ce6748 | 40 | import android.content.Context; |
darienf | 0:832122ce6748 | 41 | import android.content.Intent; |
darienf | 0:832122ce6748 | 42 | import android.content.pm.PackageManager; |
darienf | 0:832122ce6748 | 43 | import android.os.Bundle; |
darienf | 0:832122ce6748 | 44 | import android.os.Handler; |
darienf | 0:832122ce6748 | 45 | import android.view.LayoutInflater; |
darienf | 0:832122ce6748 | 46 | import android.view.Menu; |
darienf | 0:832122ce6748 | 47 | import android.view.MenuItem; |
darienf | 0:832122ce6748 | 48 | import android.view.View; |
darienf | 0:832122ce6748 | 49 | import android.view.ViewGroup; |
darienf | 0:832122ce6748 | 50 | import android.widget.BaseAdapter; |
darienf | 0:832122ce6748 | 51 | import android.widget.ListView; |
darienf | 0:832122ce6748 | 52 | import android.widget.TextView; |
darienf | 0:832122ce6748 | 53 | import android.widget.Toast; |
darienf | 0:832122ce6748 | 54 | |
darienf | 0:832122ce6748 | 55 | import java.util.ArrayList; |
darienf | 0:832122ce6748 | 56 | |
darienf | 0:832122ce6748 | 57 | /** |
darienf | 0:832122ce6748 | 58 | * Activity for scanning and displaying available Bluetooth LE devices. |
darienf | 0:832122ce6748 | 59 | */ |
darienf | 0:832122ce6748 | 60 | public class DeviceScanActivity extends ListActivity { |
darienf | 0:832122ce6748 | 61 | public static final String EXTRA_DEVICE = "extra_device"; |
darienf | 0:832122ce6748 | 62 | private LeDeviceListAdapter mLeDeviceListAdapter; |
darienf | 0:832122ce6748 | 63 | private BluetoothAdapter mBluetoothAdapter; |
darienf | 0:832122ce6748 | 64 | private boolean mScanning; |
darienf | 0:832122ce6748 | 65 | private Handler mHandler; |
darienf | 0:832122ce6748 | 66 | |
darienf | 0:832122ce6748 | 67 | private static final int REQUEST_ENABLE_BT = 1; |
darienf | 0:832122ce6748 | 68 | // Stops scanning after 10 seconds. |
darienf | 0:832122ce6748 | 69 | private static final long SCAN_PERIOD = 10000; |
darienf | 0:832122ce6748 | 70 | |
darienf | 0:832122ce6748 | 71 | @Override |
darienf | 0:832122ce6748 | 72 | public void onCreate(Bundle savedInstanceState) { |
darienf | 0:832122ce6748 | 73 | super.onCreate(savedInstanceState); |
darienf | 0:832122ce6748 | 74 | getActionBar().setTitle("BLE Device Scan"); |
darienf | 0:832122ce6748 | 75 | mHandler = new Handler(); |
darienf | 0:832122ce6748 | 76 | |
darienf | 0:832122ce6748 | 77 | // Use this check to determine whether BLE is supported on the device. Then you can |
darienf | 0:832122ce6748 | 78 | // selectively disable BLE-related features. |
darienf | 0:832122ce6748 | 79 | if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { |
darienf | 0:832122ce6748 | 80 | Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show(); |
darienf | 0:832122ce6748 | 81 | finish(); |
darienf | 0:832122ce6748 | 82 | } |
darienf | 0:832122ce6748 | 83 | |
darienf | 0:832122ce6748 | 84 | // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to |
darienf | 0:832122ce6748 | 85 | // BluetoothAdapter through BluetoothManager. |
darienf | 0:832122ce6748 | 86 | final BluetoothManager bluetoothManager = |
darienf | 0:832122ce6748 | 87 | (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); |
darienf | 0:832122ce6748 | 88 | mBluetoothAdapter = bluetoothManager.getAdapter(); |
darienf | 0:832122ce6748 | 89 | |
darienf | 0:832122ce6748 | 90 | // Checks if Bluetooth is supported on the device. |
darienf | 0:832122ce6748 | 91 | if (mBluetoothAdapter == null) { |
darienf | 0:832122ce6748 | 92 | Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show(); |
darienf | 0:832122ce6748 | 93 | finish(); |
darienf | 0:832122ce6748 | 94 | return; |
darienf | 0:832122ce6748 | 95 | } |
darienf | 0:832122ce6748 | 96 | } |
darienf | 0:832122ce6748 | 97 | |
darienf | 0:832122ce6748 | 98 | @Override |
darienf | 0:832122ce6748 | 99 | public boolean onCreateOptionsMenu(Menu menu) { |
darienf | 0:832122ce6748 | 100 | getMenuInflater().inflate(R.menu.main, menu); |
darienf | 0:832122ce6748 | 101 | if (!mScanning) { |
darienf | 0:832122ce6748 | 102 | menu.findItem(R.id.menu_stop).setVisible(false); |
darienf | 0:832122ce6748 | 103 | menu.findItem(R.id.menu_scan).setVisible(true); |
darienf | 0:832122ce6748 | 104 | menu.findItem(R.id.menu_refresh).setActionView(null); |
darienf | 0:832122ce6748 | 105 | } else { |
darienf | 0:832122ce6748 | 106 | menu.findItem(R.id.menu_stop).setVisible(true); |
darienf | 0:832122ce6748 | 107 | menu.findItem(R.id.menu_scan).setVisible(false); |
darienf | 0:832122ce6748 | 108 | menu.findItem(R.id.menu_refresh).setActionView( |
darienf | 0:832122ce6748 | 109 | R.layout.actionbar_indeterminate_progress); |
darienf | 0:832122ce6748 | 110 | } |
darienf | 0:832122ce6748 | 111 | return true; |
darienf | 0:832122ce6748 | 112 | } |
darienf | 0:832122ce6748 | 113 | |
darienf | 0:832122ce6748 | 114 | @Override |
darienf | 0:832122ce6748 | 115 | public boolean onOptionsItemSelected(MenuItem item) { |
darienf | 0:832122ce6748 | 116 | switch (item.getItemId()) { |
darienf | 0:832122ce6748 | 117 | case R.id.menu_scan: |
darienf | 0:832122ce6748 | 118 | mLeDeviceListAdapter.clear(); |
darienf | 0:832122ce6748 | 119 | scanLeDevice(true); |
darienf | 0:832122ce6748 | 120 | break; |
darienf | 0:832122ce6748 | 121 | case R.id.menu_stop: |
darienf | 0:832122ce6748 | 122 | scanLeDevice(false); |
darienf | 0:832122ce6748 | 123 | break; |
darienf | 0:832122ce6748 | 124 | } |
darienf | 0:832122ce6748 | 125 | return true; |
darienf | 0:832122ce6748 | 126 | } |
darienf | 0:832122ce6748 | 127 | |
darienf | 0:832122ce6748 | 128 | @Override |
darienf | 0:832122ce6748 | 129 | protected void onResume() { |
darienf | 0:832122ce6748 | 130 | super.onResume(); |
darienf | 0:832122ce6748 | 131 | |
darienf | 0:832122ce6748 | 132 | // Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled, |
darienf | 0:832122ce6748 | 133 | // fire an intent to display a dialog asking the user to grant permission to enable it. |
darienf | 0:832122ce6748 | 134 | if (!mBluetoothAdapter.isEnabled()) { |
darienf | 0:832122ce6748 | 135 | if (!mBluetoothAdapter.isEnabled()) { |
darienf | 0:832122ce6748 | 136 | Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); |
darienf | 0:832122ce6748 | 137 | startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); |
darienf | 0:832122ce6748 | 138 | } |
darienf | 0:832122ce6748 | 139 | } |
darienf | 0:832122ce6748 | 140 | |
darienf | 0:832122ce6748 | 141 | // Initializes list view adapter. |
darienf | 0:832122ce6748 | 142 | mLeDeviceListAdapter = new LeDeviceListAdapter(); |
darienf | 0:832122ce6748 | 143 | setListAdapter(mLeDeviceListAdapter); |
darienf | 0:832122ce6748 | 144 | scanLeDevice(true); |
darienf | 0:832122ce6748 | 145 | } |
darienf | 0:832122ce6748 | 146 | |
darienf | 0:832122ce6748 | 147 | @Override |
darienf | 0:832122ce6748 | 148 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
darienf | 0:832122ce6748 | 149 | // User chose not to enable Bluetooth. |
darienf | 0:832122ce6748 | 150 | if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) { |
darienf | 0:832122ce6748 | 151 | finish(); |
darienf | 0:832122ce6748 | 152 | return; |
darienf | 0:832122ce6748 | 153 | } |
darienf | 0:832122ce6748 | 154 | super.onActivityResult(requestCode, resultCode, data); |
darienf | 0:832122ce6748 | 155 | } |
darienf | 0:832122ce6748 | 156 | |
darienf | 0:832122ce6748 | 157 | @Override |
darienf | 0:832122ce6748 | 158 | protected void onPause() { |
darienf | 0:832122ce6748 | 159 | super.onPause(); |
darienf | 0:832122ce6748 | 160 | scanLeDevice(false); |
darienf | 0:832122ce6748 | 161 | mLeDeviceListAdapter.clear(); |
darienf | 0:832122ce6748 | 162 | } |
darienf | 0:832122ce6748 | 163 | |
darienf | 0:832122ce6748 | 164 | @Override |
darienf | 0:832122ce6748 | 165 | protected void onListItemClick(ListView l, View v, int position, long id) { |
darienf | 0:832122ce6748 | 166 | final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); |
darienf | 0:832122ce6748 | 167 | if (device == null) return; |
darienf | 0:832122ce6748 | 168 | //final Intent intent = new Intent(this, DeviceControlActivity.class); |
darienf | 0:832122ce6748 | 169 | //intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName()); |
darienf | 0:832122ce6748 | 170 | //intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress()); |
darienf | 0:832122ce6748 | 171 | if (mScanning) { |
darienf | 0:832122ce6748 | 172 | mBluetoothAdapter.stopLeScan(mLeScanCallback); |
darienf | 0:832122ce6748 | 173 | mScanning = false; |
darienf | 0:832122ce6748 | 174 | } |
darienf | 0:832122ce6748 | 175 | //startActivity(intent); |
darienf | 0:832122ce6748 | 176 | |
darienf | 0:832122ce6748 | 177 | final Intent intent = new Intent(this, SensorActivity.class); |
darienf | 0:832122ce6748 | 178 | intent.putExtra(EXTRA_DEVICE, device); |
darienf | 0:832122ce6748 | 179 | startActivity(intent); |
darienf | 0:832122ce6748 | 180 | } |
darienf | 0:832122ce6748 | 181 | |
darienf | 0:832122ce6748 | 182 | private void scanLeDevice(final boolean enable) { |
darienf | 0:832122ce6748 | 183 | if (enable) { |
darienf | 0:832122ce6748 | 184 | // Stops scanning after a pre-defined scan period. |
darienf | 0:832122ce6748 | 185 | mHandler.postDelayed(new Runnable() { |
darienf | 0:832122ce6748 | 186 | @Override |
darienf | 0:832122ce6748 | 187 | public void run() { |
darienf | 0:832122ce6748 | 188 | mScanning = false; |
darienf | 0:832122ce6748 | 189 | mBluetoothAdapter.stopLeScan(mLeScanCallback); |
darienf | 0:832122ce6748 | 190 | invalidateOptionsMenu(); |
darienf | 0:832122ce6748 | 191 | } |
darienf | 0:832122ce6748 | 192 | }, SCAN_PERIOD); |
darienf | 0:832122ce6748 | 193 | |
darienf | 0:832122ce6748 | 194 | mScanning = true; |
darienf | 0:832122ce6748 | 195 | mBluetoothAdapter.startLeScan(mLeScanCallback); |
darienf | 0:832122ce6748 | 196 | } else { |
darienf | 0:832122ce6748 | 197 | mScanning = false; |
darienf | 0:832122ce6748 | 198 | mBluetoothAdapter.stopLeScan(mLeScanCallback); |
darienf | 0:832122ce6748 | 199 | } |
darienf | 0:832122ce6748 | 200 | invalidateOptionsMenu(); |
darienf | 0:832122ce6748 | 201 | } |
darienf | 0:832122ce6748 | 202 | |
darienf | 0:832122ce6748 | 203 | // Adapter for holding devices found through scanning. |
darienf | 0:832122ce6748 | 204 | private class LeDeviceListAdapter extends BaseAdapter { |
darienf | 0:832122ce6748 | 205 | private ArrayList<BluetoothDevice> mLeDevices; |
darienf | 0:832122ce6748 | 206 | private LayoutInflater mInflator; |
darienf | 0:832122ce6748 | 207 | |
darienf | 0:832122ce6748 | 208 | public LeDeviceListAdapter() { |
darienf | 0:832122ce6748 | 209 | super(); |
darienf | 0:832122ce6748 | 210 | mLeDevices = new ArrayList<BluetoothDevice>(); |
darienf | 0:832122ce6748 | 211 | mInflator = DeviceScanActivity.this.getLayoutInflater(); |
darienf | 0:832122ce6748 | 212 | } |
darienf | 0:832122ce6748 | 213 | |
darienf | 0:832122ce6748 | 214 | public void addDevice(BluetoothDevice device) { |
darienf | 0:832122ce6748 | 215 | if (!mLeDevices.contains(device)) { |
darienf | 0:832122ce6748 | 216 | mLeDevices.add(device); |
darienf | 0:832122ce6748 | 217 | } |
darienf | 0:832122ce6748 | 218 | } |
darienf | 0:832122ce6748 | 219 | |
darienf | 0:832122ce6748 | 220 | public BluetoothDevice getDevice(int position) { |
darienf | 0:832122ce6748 | 221 | return mLeDevices.get(position); |
darienf | 0:832122ce6748 | 222 | } |
darienf | 0:832122ce6748 | 223 | |
darienf | 0:832122ce6748 | 224 | public void clear() { |
darienf | 0:832122ce6748 | 225 | mLeDevices.clear(); |
darienf | 0:832122ce6748 | 226 | } |
darienf | 0:832122ce6748 | 227 | |
darienf | 0:832122ce6748 | 228 | @Override |
darienf | 0:832122ce6748 | 229 | public int getCount() { |
darienf | 0:832122ce6748 | 230 | return mLeDevices.size(); |
darienf | 0:832122ce6748 | 231 | } |
darienf | 0:832122ce6748 | 232 | |
darienf | 0:832122ce6748 | 233 | @Override |
darienf | 0:832122ce6748 | 234 | public Object getItem(int i) { |
darienf | 0:832122ce6748 | 235 | return mLeDevices.get(i); |
darienf | 0:832122ce6748 | 236 | } |
darienf | 0:832122ce6748 | 237 | |
darienf | 0:832122ce6748 | 238 | @Override |
darienf | 0:832122ce6748 | 239 | public long getItemId(int i) { |
darienf | 0:832122ce6748 | 240 | return i; |
darienf | 0:832122ce6748 | 241 | } |
darienf | 0:832122ce6748 | 242 | |
darienf | 0:832122ce6748 | 243 | @Override |
darienf | 0:832122ce6748 | 244 | public View getView(int i, View view, ViewGroup viewGroup) { |
darienf | 0:832122ce6748 | 245 | ViewHolder viewHolder; |
darienf | 0:832122ce6748 | 246 | // General ListView optimization code. |
darienf | 0:832122ce6748 | 247 | if (view == null) { |
darienf | 0:832122ce6748 | 248 | view = mInflator.inflate(R.layout.listitem_device, null); |
darienf | 0:832122ce6748 | 249 | viewHolder = new ViewHolder(); |
darienf | 0:832122ce6748 | 250 | viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address); |
darienf | 0:832122ce6748 | 251 | viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name); |
darienf | 0:832122ce6748 | 252 | view.setTag(viewHolder); |
darienf | 0:832122ce6748 | 253 | } else { |
darienf | 0:832122ce6748 | 254 | viewHolder = (ViewHolder) view.getTag(); |
darienf | 0:832122ce6748 | 255 | } |
darienf | 0:832122ce6748 | 256 | |
darienf | 0:832122ce6748 | 257 | BluetoothDevice device = mLeDevices.get(i); |
darienf | 0:832122ce6748 | 258 | final String deviceName = device.getName(); |
darienf | 0:832122ce6748 | 259 | if (deviceName != null && deviceName.length() > 0) |
darienf | 0:832122ce6748 | 260 | viewHolder.deviceName.setText(deviceName); |
darienf | 0:832122ce6748 | 261 | else |
darienf | 0:832122ce6748 | 262 | viewHolder.deviceName.setText(R.string.unknown_device); |
darienf | 0:832122ce6748 | 263 | viewHolder.deviceAddress.setText(device.getAddress()); |
darienf | 0:832122ce6748 | 264 | |
darienf | 0:832122ce6748 | 265 | return view; |
darienf | 0:832122ce6748 | 266 | } |
darienf | 0:832122ce6748 | 267 | } |
darienf | 0:832122ce6748 | 268 | |
darienf | 0:832122ce6748 | 269 | // Device scan callback. |
darienf | 0:832122ce6748 | 270 | private BluetoothAdapter.LeScanCallback mLeScanCallback = |
darienf | 0:832122ce6748 | 271 | new BluetoothAdapter.LeScanCallback() { |
darienf | 0:832122ce6748 | 272 | |
darienf | 0:832122ce6748 | 273 | @Override |
darienf | 0:832122ce6748 | 274 | public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { |
darienf | 0:832122ce6748 | 275 | runOnUiThread(new Runnable() { |
darienf | 0:832122ce6748 | 276 | @Override |
darienf | 0:832122ce6748 | 277 | public void run() { |
darienf | 0:832122ce6748 | 278 | mLeDeviceListAdapter.addDevice(device); |
darienf | 0:832122ce6748 | 279 | mLeDeviceListAdapter.notifyDataSetChanged(); |
darienf | 0:832122ce6748 | 280 | } |
darienf | 0:832122ce6748 | 281 | }); |
darienf | 0:832122ce6748 | 282 | } |
darienf | 0:832122ce6748 | 283 | }; |
darienf | 0:832122ce6748 | 284 | |
darienf | 0:832122ce6748 | 285 | static class ViewHolder { |
darienf | 0:832122ce6748 | 286 | TextView deviceName; |
darienf | 0:832122ce6748 | 287 | TextView deviceAddress; |
darienf | 0:832122ce6748 | 288 | } |
darienf | 0:832122ce6748 | 289 | } |