Skip to content

Instantly share code, notes, and snippets.

@sapher
Created February 5, 2017 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapher/9c31898d80077aa9e8fa0f5ce4de7bcc to your computer and use it in GitHub Desktop.
Save sapher/9c31898d80077aa9e8fa0f5ce4de7bcc to your computer and use it in GitHub Desktop.
Bluetooth with Android +6
package gardenator.sapher.com.gardenator.activities;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Receiver;
import gardenator.sapher.com.gardenator.R;
@EActivity(R.layout.activity_device_wizard)
public class MyActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
BluetoothAdapter bluetoothAdapter;
@AfterViews
public void init() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null) {
Log.d(TAG, "init: this device does not support bluetooth");
}
else {
// Android 6.0
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d(TAG, "init: version lollipop");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[] { "android.permission.ACCESS_COARSE_LOCATION" }, 0);
Log.d(TAG, "init: request permission");
bluetoothAdapter.startDiscovery();
}
}
}
}
@Receiver(actions = BluetoothDevice.ACTION_FOUND)
protected void onDeviceFound(Intent intent) {
Log.d(TAG, "onDeviceFound: triggered");
if(intent == null) return;
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "onDeciveFound: " + device.getAddress());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment