Skip to content

Instantly share code, notes, and snippets.

@EdgarMP
Created September 24, 2017 18:00
Show Gist options
  • Save EdgarMP/aa1e615dbf8540a117cec75ee37adca6 to your computer and use it in GitHub Desktop.
Save EdgarMP/aa1e615dbf8540a117cec75ee37adca6 to your computer and use it in GitHub Desktop.
package com.edgarmarcopolo.mybleapp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
public class MainActivity extends AppCompatActivity implements BluetoothAdapter.LeScanCallback{
public static final String TAG = MainActivity.class.getSimpleName();
BluetoothAdapter bluetoothAdapter;
RecyclerView recyclerView;
BLEAdapter bleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.listDevices);
BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
if(bluetoothManager==null){
//El dispositivo no tiene bluetooth
Log.wtf(TAG, "El dispositivo no tiene bluetooth");
}
assert bluetoothManager != null;
bluetoothAdapter = bluetoothManager.getAdapter();
if(bluetoothAdapter!=null){
scanLeDevice(true);
}
//bleAdapter = new BLEAdapter(this);
//recyclerView.setAdapter(bleAdapter);
}
private void scanLeDevice(final boolean enable){
if(enable){
bluetoothAdapter.startLeScan(this);
}else{
bluetoothAdapter.stopLeScan(this);
}
}
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
if(bluetoothDevice.getAddress().equals("34:81:F4:06:C1:A2")){
bluetoothAdapter.stopLeScan(this);
connectDevice(bluetoothDevice);
}
Log.wtf(TAG, bluetoothDevice.getName());
Log.wtf(TAG, bluetoothDevice.getAddress());
}
private void connectDevice(BluetoothDevice device){
device.createBond();
device.connectGatt(this, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS){
if(newState == BluetoothGatt.STATE_CONNECTED){}
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if(status == BluetoothGatt.GATT_SUCCESS){
for(BluetoothGattService service: gatt.getServices()){
Log.wtf(TAG, "UUID: "+service.getUuid());
}
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment