package st.wow.git.ble; import java.lang.Runnable; import android.util.Log; import java.lang.Class; import java.lang.ClassLoader; import java.lang.reflect.Constructor; import android.app.Activity; import android.app.Fragment; import android.os.Handler; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothProfile; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.Manifest; public class BleConnect extends Fragment { BluetoothManager manager; BluetoothAdapter adapter; final int PERMISSION_REQUEST = 1; final int REQUEST_ENABLE_BT = 1; public BleConnect() { Log.d("gio", "BleConnect()"); } @Override public void onAttach(Context ctx) { super.onAttach(ctx); Log.d("gio", "BleConnect: onAttach()"); ctx.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); manager = (BluetoothManager) ctx.getSystemService(ctx.BLUETOOTH_SERVICE); Log.d("gio", "BleUtil Enable: adapter"); adapter = manager.getAdapter(); if (!enabled()) { Log.d("gio", "BleConnect: enabling adapter"); Handler handler = new Handler(ctx.getMainLooper()); Intent enableBtIntent = new Intent(adapter.ACTION_REQUEST_ENABLE); Log.d("gio", "BleConnect: handler.post"); handler.post(new Runnable() { public void run() { startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }); } else { Log.d("gio", "BleConnect: adapter is enabled"); } if (ctx.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST); } installComplete(this); } public boolean enabled() { return (adapter != null && adapter.isEnabled()); } private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("gio", "Received broadcast"); if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { updateState(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)); } } }; @Override public void onDestroy() { Log.d("gio","onDestroy()"); stopScan(); getContext().unregisterReceiver(receiver); super.onDestroy(); } private final BluetoothAdapter.LeScanCallback scanCallback = new BluetoothAdapter.LeScanCallback() { public void onLeScan(final BluetoothDevice dev, int rssi, byte[] scanRecord) { Log.d("gio","onLeScan(): " + dev.getName()); onScan(dev.getName(), dev.getAddress(), rssi, dev); } }; public void scan() { if (!enabled()) { return; } adapter.startLeScan(scanCallback); } public void stopScan() { if (!enabled()) { return; } Log.d("gio", "Stop scan"); adapter.stopLeScan(scanCallback); } private BluetoothDevice device; private BluetoothGatt bluetoothGatt; private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.d("gio", "Connected"); String addr = device.getAddress(); Log.d("gio", "Address = " + addr); onConnect(device.getAddress()); } } public void onServicesDiscovered(BluetoothGatt gatt, int status) { for (BluetoothGattService serv : gatt.getServices()) { onDiscoverService(device.getAddress(), serv.getUuid().toString()); } } }; public void connect(BluetoothDevice dev) { if (dev == null) { return; } Log.d("gio","BleConnect: connect"); device = dev; bluetoothGatt = dev.connectGatt(getContext(), false, gattCallback); } public void disconnect() { device = null; if (bluetoothGatt == null) { return; } Log.d("gio","BleConnect: disconnect"); bluetoothGatt.disconnect(); } public void discoverServices(BluetoothDevice dev) { if (bluetoothGatt == null || device == null) { return; } if (!dev.getAddress().equals(device.getAddress())) { disconnect(); return; } bluetoothGatt.discoverServices(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("gio", "BleConnect: onActivityResult()"); if (requestCode == REQUEST_ENABLE_BT) { Log.d("gio", "BleConnect: onActivityResult() REQUEST_ENABLE_BT"); switch (resultCode) { case Activity.RESULT_OK: { Log.d("gio", "BleConnect: onActivityResult() -- OK"); break; } case Activity.RESULT_CANCELED: { Log.d("gio", "BleConnect: onActivityResult() -- Cancelled"); break; } } } } static private native void installComplete(BleConnect p); static private native void updateState(int s); static private native void onScan(String name, String id, int rssi, BluetoothDevice dev); static private native void onConnect(String id); static private native void onDiscoverService(String id, String uuid); }