Created
October 28, 2014 14:58
-
-
Save jdmunro/5e9abdef4b48f18679c5 to your computer and use it in GitHub Desktop.
Scanning for Bluetooth devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.BLUETOOTH"/> | |
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> | |
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); | |
bluetoothAdapter.startDiscovery(); | |
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); | |
registerReceiver(bluetoothReceiver, filter); | |
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() { | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
if (BluetoothDevice.ACTION_FOUND.equals(action)) { | |
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); | |
int rssi = Math.abs(intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, (short) 0)); | |
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { | |
// Scan again... | |
bluetoothAdapter.startDiscovery(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment