List of usage examples for android.bluetooth BluetoothGatt discoverServices
public boolean discoverServices()
From source file:org.physical_web.physicalweb.BluetoothSite.java
@Override public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { Log.i(TAG, "MTU changed to " + mtu); transferRate = mtu - 5;// ww w . j a v a 2 s.co m gatt.discoverServices(); }
From source file:com.megster.cordova.rfduino.Peripheral.java
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothGatt.STATE_CONNECTED) { connected = true;// w w w. java 2s. c o m gatt.discoverServices(); } else { connected = false; if (connectCallback != null) { connectCallback.error("Disconnected"); connectCallback = null; } } }
From source file:net.emilymaier.movebot.HeartFragment.java
@Override @SuppressWarnings("deprecation") public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { Log.d("HeartFragment", "Bluetooth LE device found"); BluetoothGatt bluetoothGatt = device.connectGatt(act, false, new BluetoothGattCallback() { @Override//w w w . j av a 2 s .c o m public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.d("HeartFragment", "Connected to LE device"); gatt.discoverServices(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattCharacteristic characteristic = null; for (BluetoothGattService service : gatt.getServices()) { characteristic = service .getCharacteristic(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb")); if (characteristic != null) { break; } } if (characteristic != null) { Log.d("HeartFragment", "Found device with HRM characteristic"); HeartDevice device = new HeartDevice(); device.bluetoothGatt = gatt; device.characteristic = characteristic; for (HeartDevice testDevice : heartDevices) { if (testDevice.bluetoothGatt.getDevice().getAddress() .equals(device.bluetoothGatt.getDevice().getAddress())) { heartDevices.remove(testDevice); } } heartDevices.add(device); } else { Log.d("HeartFragment", "Device does not have HRM characteristic"); gatt.disconnect(); gatt.close(); } } else { Log.w("HeartFragment", "Failed to discover device services"); gatt.disconnect(); gatt.close(); } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; } final int heartRate = characteristic.getIntValue(format, 1); act.runOnUiThread(new Runnable() { @Override public void run() { act.updateHeart(heartRate); } }); } }); }
From source file:ble.AndroidBle.java
@Override public boolean discoverServices(String address) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null) { return false; }/*w ww. j av a2 s .c o m*/ boolean ret = gatt.discoverServices(); if (!ret) { disconnect(address); } return ret; }
From source file:com.google.android.apps.forscience.ble.MyBleService.java
public boolean discoverServices(String address) { BluetoothGatt bluetoothGatt = addressToGattClient.get(address); return bluetoothGatt != null && bluetoothGatt.discoverServices(); }
From source file:fr.bmartel.android.notti.service.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection/*from w ww. j av a 2s .co m*/ * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; Log.i(TAG, "Disconnected from GATT server."); try { JSONObject object = new JSONObject(); object.put(BluetoothConst.DEVICE_ADDRESS, getAddress()); object.put(BluetoothConst.DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } if (BluetoothDeviceConn.this.gatt != null) { BluetoothDeviceConn.this.gatt.close(); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new NottiDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { JSONObject object = new JSONObject(); object.put(BluetoothConst.DEVICE_ADDRESS, getAddress()); object.put(BluetoothConst.DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (device != null) { device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:com.github.akinaru.bleanalyzer.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection/*from w w w.j a v a 2s . c o m*/ * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; Log.i(TAG, "Disconnected from GATT server."); try { JSONObject object = new JSONObject(); object.put(JsonConstants.BT_ADDRESS, getAddress()); object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } if (manager.getWaitingMap().containsKey(deviceAddr)) { manager.getWaitingMap().get(deviceAddr).cancel(true); manager.getWaitingMap().remove(deviceAddr); } if (BluetoothDeviceConn.this.gatt != null) { Log.i(TAG, "connection close clean"); BluetoothDeviceConn.this.gatt.close(); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new RfduinoDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { JSONObject object = new JSONObject(); object.put(JsonConstants.BT_ADDRESS, getAddress()); object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { Log.i(TAG, "onCharacteristicRead"); device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { manager.getEventManager().set(); if (device != null) { Log.i(TAG, "onCharacteristicChanged"); device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection// w w w. j a va2 s. co m * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.v(TAG, "Connected to GATT server."); Log.v(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; Log.v(TAG, "Disconnected from GATT server."); if (status != 133) { manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, new ArrayList<String>()); } else { manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_RETRY, new ArrayList<String>()); } if (manager.getWaitingMap().containsKey(deviceAddr)) { manager.getWaitingMap().get(deviceAddr).cancel(true); manager.getWaitingMap().remove(deviceAddr); } if (BluetoothDeviceConn.this.gatt != null) { Log.v(TAG, "connection close clean"); BluetoothDeviceConn.this.gatt.close(); } if (remove) { manager.getConnectionList().remove(deviceAddr); manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_REMOVED, new ArrayList<String>()); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new RfduinoDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { JSONObject object = new JSONObject(); object.put(JsonConstants.BT_ADDRESS, getAddress()); object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { Log.v(TAG, "onCharacteristicRead"); device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { manager.getEventManager().set(); if (device != null) { Log.v(TAG, "onCharacteristicChanged"); device.notifyCharacteristicChangeReceived(characteristic); } } }; }
From source file:org.physical_web.physicalweb.BluetoothSite.java
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED && status == gatt.GATT_SUCCESS) { Log.i(TAG, "Connected to GATT server"); mBluetoothGatt = gatt;/*from w w w . j a va2 s.c o m*/ if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH); gatt.requestMtu(505); } else { gatt.discoverServices(); } } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.i(TAG, "Disconnected to GATT server"); // ensure progress dialog is removed and running is set false close(); } else if (status != gatt.GATT_SUCCESS) { Log.i(TAG, "Status is " + status); close(); } }
From source file:fr.bmartel.android.bluetooth.connection.BluetoothDeviceConn.java
/** * Build Bluetooth device connection// w ww.j a v a2 s. c om * * @param address */ @SuppressLint("NewApi") public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) { this.deviceAddr = address; this.deviceName = deviceName; this.manager = manager; gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ActionFilterGatt.ACTION_GATT_CONNECTED; Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; intentAction = ActionFilterGatt.ACTION_GATT_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); manager.broadcastUpdate(ActionFilterGatt.ACTION_GATT_DISCONNECTED); if (BluetoothDeviceConn.this.gatt != null) { BluetoothDeviceConn.this.gatt.close(); } } } @Override // New services discovered public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { Runnable test = new Runnable() { @Override public void run() { //you can improve this by using reflection device = new DottiDevice(BluetoothDeviceConn.this); device.addInitListener(new IDeviceInitListener() { @Override public void onInit() { try { String name = ""; JSONObject object = new JSONObject(); object.put("address", getAddress()); object.put("deviceName", getDeviceName()); ArrayList<String> values = new ArrayList<String>(); values.add(object.toString()); connected = true; //when device is fully intitialized broadcast service discovery manager.broadcastUpdateStringList( ActionFilterGatt.ACTION_GATT_SERVICES_DISCOVERED, values); } catch (JSONException e) { e.printStackTrace(); } } }); device.init(); } }; Thread testThread = new Thread(test); testThread.start(); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { System.out.println("characteristic write received "); manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicWriteReceived(characteristic); } } @Override // Result of a characteristic read operation public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { manager.getEventManager().set(); if (device != null) { device.notifyCharacteristicReadReceived(characteristic); } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { System.out.println("descriptor write received "); manager.getEventManager().set(); } @Override // Characteristic notification public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { System.out.println("descriptor change received "); if (device != null) { device.notifyCharacteristicChangeReceived(characteristic); } } }; }