Java tutorial
/* * 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. */ package net.kenevans.android.bleexplorer; import android.Manifest; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; /** * Activity for scanning and displaying available Bluetooth LE devices. */ @SuppressLint("InflateParams") public class DeviceScanActivity extends ListActivity implements IConstants { private LeDeviceListAdapter mLeDeviceListAdapter; private BluetoothAdapter mBluetoothAdapter; private boolean mScanning; private Handler mHandler; private boolean mCoarseLocationPermissionAsked = false; private boolean mAllowScan; private static final int REQUEST_ENABLE_BT = 1; // Stops scanning after 10 seconds. private static final long SCAN_PERIOD = 10000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setTitle(R.string.title_devices); } mHandler = new Handler(); // Use this check to determine whether BLE is supported on the device. // Then you can // selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show(); finish(); } // Initializes a Bluetooth adapter. For API level 18 and above, get a // reference to BluetoothAdapter through BluetoothManager. mBluetoothAdapter = null; final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) { Toast.makeText(this, R.string.error_bluetooth_manager, Toast.LENGTH_LONG).show(); finish(); return; } try { mBluetoothAdapter = bluetoothManager.getAdapter(); } catch (NullPointerException ex) { Toast.makeText(this, R.string.error_bluetooth_adapter, Toast.LENGTH_LONG).show(); } // Checks if Bluetooth is supported on the device. if (mBluetoothAdapter == null) { String msg = getString(R.string.bluetooth_not_supported); Utils.errMsg(this, msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); finish(); } // Set result CANCELED in case the user backs out setResult(Activity.RESULT_CANCELED); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); if (!mScanning) { menu.findItem(R.id.menu_stop).setVisible(false); menu.findItem(R.id.menu_scan).setVisible(true); menu.findItem(R.id.menu_refresh).setActionView(null); } else { menu.findItem(R.id.menu_stop).setVisible(true); menu.findItem(R.id.menu_scan).setVisible(false); menu.findItem(R.id.menu_refresh).setActionView(R.layout.actionbar_indeterminate_progress); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_scan: mLeDeviceListAdapter.clear(); startScan(); break; case R.id.menu_stop: endScan(); break; } return true; } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume: mScanning=" + mScanning); // Ensures Bluetooth is enabled on the device. If Bluetooth is not // currently enabled, // fire an intent to display a dialog asking the user to grant // permission to enable it. if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } // Initializes list view adapter. if (mBluetoothAdapter.isEnabled()) { mLeDeviceListAdapter = new LeDeviceListAdapter(); setListAdapter(mLeDeviceListAdapter); startScan(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case PERMISSION_ACCESS_COARSE_LOCATION: // COARSE_LOCATION if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mAllowScan = true; startScan(); } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) { mAllowScan = false; } break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // User chose not to enable Bluetooth. if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) { finish(); return; } super.onActivityResult(requestCode, resultCode, data); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause: mScanning=" + mScanning); if (mBluetoothAdapter.isEnabled()) { endScan(); } if (mLeDeviceListAdapter != null) { mLeDeviceListAdapter.clear(); } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); if (device == null) return; final Intent intent = new Intent(this, DeviceControlActivity.class); intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName()); intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress()); if (mScanning) { mBluetoothAdapter.stopLeScan(mLeScanCallback); mScanning = false; } startActivity(intent); } private void endScan() { Log.d(TAG, "endScan"); // Stop if (mScanning) { mBluetoothAdapter.stopLeScan(mLeScanCallback); } mScanning = false; invalidateOptionsMenu(); } private void startScan() { Log.d(TAG, "startScan"); // Check for coarse location permission if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted mAllowScan = false; if (!mCoarseLocationPermissionAsked) { Log.d(TAG, "startScan: requestPermission"); mCoarseLocationPermissionAsked = true; ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, PERMISSION_ACCESS_COARSE_LOCATION); } else { Log.d(TAG, "startScan: infoMsg"); String msg = getString(R.string.permission_coarse_location); if (mCoarseLocationPermissionAsked) { Utils.infoMsg(this, msg); } } } else { mAllowScan = true; } if (mAllowScan) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); invalidateOptionsMenu(); } }, SCAN_PERIOD); mScanning = true; mBluetoothAdapter.startLeScan(mLeScanCallback); } invalidateOptionsMenu(); } // Adapter for holding devices found through scanning. private class LeDeviceListAdapter extends BaseAdapter { private final ArrayList<BluetoothDevice> mLeDevices; private final LayoutInflater mInflator; private LeDeviceListAdapter() { super(); mLeDevices = new ArrayList<>(); mInflator = DeviceScanActivity.this.getLayoutInflater(); } private void addDevice(BluetoothDevice device) { if (!mLeDevices.contains(device)) { mLeDevices.add(device); } } private BluetoothDevice getDevice(int position) { return mLeDevices.get(position); } private void clear() { mLeDevices.clear(); } @Override public int getCount() { return mLeDevices.size(); } @Override public Object getItem(int i) { return mLeDevices.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; // General ListView optimization code. if (view == null) { view = mInflator.inflate(R.layout.listitem_device, null); viewHolder = new ViewHolder(); viewHolder.deviceAddress = view.findViewById(R.id.device_address); viewHolder.deviceName = view.findViewById(R.id.device_name); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } BluetoothDevice device = mLeDevices.get(i); final String deviceName = device.getName(); if (deviceName != null && deviceName.length() > 0) { viewHolder.deviceName.setText(deviceName); } else { viewHolder.deviceName.setText(R.string.unknown_device); viewHolder.deviceAddress.setText(device.getAddress()); } return view; } } // Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { mLeDeviceListAdapter.addDevice(device); mLeDeviceListAdapter.notifyDataSetChanged(); } }); } }; static class ViewHolder { TextView deviceName; TextView deviceAddress; } }