Skip to content

Instantly share code, notes, and snippets.

@jdmunro
Created October 28, 2014 14:58
Show Gist options
  • Save jdmunro/5e9abdef4b48f18679c5 to your computer and use it in GitHub Desktop.
Save jdmunro/5e9abdef4b48f18679c5 to your computer and use it in GitHub Desktop.
Scanning for Bluetooth devices
<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