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.
Dependencies: mbed MAX14720 MAX30205 USBDevice
DeviceScanActivity.java
00001 /******************************************************************************* 00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * <p> 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * <p> 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * <p> 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * <p> 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * <p> 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 * ****************************************************************************** 00032 */ 00033 package com.example.android.bluetoothlegatt; 00034 00035 import android.app.Activity; 00036 import android.app.ListActivity; 00037 import android.bluetooth.BluetoothAdapter; 00038 import android.bluetooth.BluetoothDevice; 00039 import android.bluetooth.BluetoothManager; 00040 import android.content.Context; 00041 import android.content.Intent; 00042 import android.content.pm.PackageManager; 00043 import android.os.Bundle; 00044 import android.os.Handler; 00045 import android.view.LayoutInflater; 00046 import android.view.Menu; 00047 import android.view.MenuItem; 00048 import android.view.View; 00049 import android.view.ViewGroup; 00050 import android.widget.BaseAdapter; 00051 import android.widget.ListView; 00052 import android.widget.TextView; 00053 import android.widget.Toast; 00054 00055 import java.util.ArrayList; 00056 00057 /** 00058 * Activity for scanning and displaying available Bluetooth LE devices. 00059 */ 00060 public class DeviceScanActivity extends ListActivity { 00061 public static final String EXTRA_DEVICE = "extra_device"; 00062 private LeDeviceListAdapter mLeDeviceListAdapter; 00063 private BluetoothAdapter mBluetoothAdapter; 00064 private boolean mScanning; 00065 private Handler mHandler; 00066 00067 private static final int REQUEST_ENABLE_BT = 1; 00068 // Stops scanning after 10 seconds. 00069 private static final long SCAN_PERIOD = 10000; 00070 00071 @Override 00072 public void onCreate(Bundle savedInstanceState) { 00073 super.onCreate(savedInstanceState); 00074 getActionBar().setTitle("BLE Device Scan"); 00075 mHandler = new Handler(); 00076 00077 // Use this check to determine whether BLE is supported on the device. Then you can 00078 // selectively disable BLE-related features. 00079 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 00080 Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show(); 00081 finish(); 00082 } 00083 00084 // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to 00085 // BluetoothAdapter through BluetoothManager. 00086 final BluetoothManager bluetoothManager = 00087 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 00088 mBluetoothAdapter = bluetoothManager.getAdapter(); 00089 00090 // Checks if Bluetooth is supported on the device. 00091 if (mBluetoothAdapter == null) { 00092 Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show(); 00093 finish(); 00094 return; 00095 } 00096 } 00097 00098 @Override 00099 public boolean onCreateOptionsMenu(Menu menu) { 00100 getMenuInflater().inflate(R.menu.main, menu); 00101 if (!mScanning) { 00102 menu.findItem(R.id.menu_stop).setVisible(false); 00103 menu.findItem(R.id.menu_scan).setVisible(true); 00104 menu.findItem(R.id.menu_refresh).setActionView(null); 00105 } else { 00106 menu.findItem(R.id.menu_stop).setVisible(true); 00107 menu.findItem(R.id.menu_scan).setVisible(false); 00108 menu.findItem(R.id.menu_refresh).setActionView( 00109 R.layout.actionbar_indeterminate_progress); 00110 } 00111 return true; 00112 } 00113 00114 @Override 00115 public boolean onOptionsItemSelected(MenuItem item) { 00116 switch (item.getItemId()) { 00117 case R.id.menu_scan: 00118 mLeDeviceListAdapter.clear(); 00119 scanLeDevice(true); 00120 break; 00121 case R.id.menu_stop: 00122 scanLeDevice(false); 00123 break; 00124 } 00125 return true; 00126 } 00127 00128 @Override 00129 protected void onResume() { 00130 super.onResume(); 00131 00132 // Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled, 00133 // fire an intent to display a dialog asking the user to grant permission to enable it. 00134 if (!mBluetoothAdapter.isEnabled()) { 00135 if (!mBluetoothAdapter.isEnabled()) { 00136 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 00137 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 00138 } 00139 } 00140 00141 // Initializes list view adapter. 00142 mLeDeviceListAdapter = new LeDeviceListAdapter(); 00143 setListAdapter(mLeDeviceListAdapter); 00144 scanLeDevice(true); 00145 } 00146 00147 @Override 00148 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 00149 // User chose not to enable Bluetooth. 00150 if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) { 00151 finish(); 00152 return; 00153 } 00154 super.onActivityResult(requestCode, resultCode, data); 00155 } 00156 00157 @Override 00158 protected void onPause() { 00159 super.onPause(); 00160 scanLeDevice(false); 00161 mLeDeviceListAdapter.clear(); 00162 } 00163 00164 @Override 00165 protected void onListItemClick(ListView l, View v, int position, long id) { 00166 final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); 00167 if (device == null) return; 00168 //final Intent intent = new Intent(this, DeviceControlActivity.class); 00169 //intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName()); 00170 //intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress()); 00171 if (mScanning) { 00172 mBluetoothAdapter.stopLeScan(mLeScanCallback); 00173 mScanning = false; 00174 } 00175 //startActivity(intent); 00176 00177 final Intent intent = new Intent(this, SensorActivity.class); 00178 intent.putExtra(EXTRA_DEVICE, device); 00179 startActivity(intent); 00180 } 00181 00182 private void scanLeDevice(final boolean enable) { 00183 if (enable) { 00184 // Stops scanning after a pre-defined scan period. 00185 mHandler.postDelayed(new Runnable() { 00186 @Override 00187 public void run() { 00188 mScanning = false; 00189 mBluetoothAdapter.stopLeScan(mLeScanCallback); 00190 invalidateOptionsMenu(); 00191 } 00192 }, SCAN_PERIOD); 00193 00194 mScanning = true; 00195 mBluetoothAdapter.startLeScan(mLeScanCallback); 00196 } else { 00197 mScanning = false; 00198 mBluetoothAdapter.stopLeScan(mLeScanCallback); 00199 } 00200 invalidateOptionsMenu(); 00201 } 00202 00203 // Adapter for holding devices found through scanning. 00204 private class LeDeviceListAdapter extends BaseAdapter { 00205 private ArrayList<BluetoothDevice> mLeDevices; 00206 private LayoutInflater mInflator; 00207 00208 public LeDeviceListAdapter() { 00209 super(); 00210 mLeDevices = new ArrayList<BluetoothDevice>(); 00211 mInflator = DeviceScanActivity.this.getLayoutInflater(); 00212 } 00213 00214 public void addDevice(BluetoothDevice device) { 00215 if (!mLeDevices.contains(device)) { 00216 mLeDevices.add(device); 00217 } 00218 } 00219 00220 public BluetoothDevice getDevice(int position) { 00221 return mLeDevices.get(position); 00222 } 00223 00224 public void clear() { 00225 mLeDevices.clear(); 00226 } 00227 00228 @Override 00229 public int getCount() { 00230 return mLeDevices.size(); 00231 } 00232 00233 @Override 00234 public Object getItem(int i) { 00235 return mLeDevices.get(i); 00236 } 00237 00238 @Override 00239 public long getItemId(int i) { 00240 return i; 00241 } 00242 00243 @Override 00244 public View getView(int i, View view, ViewGroup viewGroup) { 00245 ViewHolder viewHolder; 00246 // General ListView optimization code. 00247 if (view == null) { 00248 view = mInflator.inflate(R.layout.listitem_device, null); 00249 viewHolder = new ViewHolder(); 00250 viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address); 00251 viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name); 00252 view.setTag(viewHolder); 00253 } else { 00254 viewHolder = (ViewHolder) view.getTag(); 00255 } 00256 00257 BluetoothDevice device = mLeDevices.get(i); 00258 final String deviceName = device.getName(); 00259 if (deviceName != null && deviceName.length() > 0) 00260 viewHolder.deviceName.setText(deviceName); 00261 else 00262 viewHolder.deviceName.setText(R.string.unknown_device); 00263 viewHolder.deviceAddress.setText(device.getAddress()); 00264 00265 return view; 00266 } 00267 } 00268 00269 // Device scan callback. 00270 private BluetoothAdapter.LeScanCallback mLeScanCallback = 00271 new BluetoothAdapter.LeScanCallback() { 00272 00273 @Override 00274 public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 00275 runOnUiThread(new Runnable() { 00276 @Override 00277 public void run() { 00278 mLeDeviceListAdapter.addDevice(device); 00279 mLeDeviceListAdapter.notifyDataSetChanged(); 00280 } 00281 }); 00282 } 00283 }; 00284 00285 static class ViewHolder { 00286 TextView deviceName; 00287 TextView deviceAddress; 00288 } 00289 }
Generated on Thu Jul 28 2022 18:07:13 by
1.7.2