Example usage for android.bluetooth BluetoothGatt getServices

List of usage examples for android.bluetooth BluetoothGatt getServices

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGatt getServices.

Prototype

public List<BluetoothGattService> getServices() 

Source Link

Document

Returns a list of GATT services offered by the remote device.

Usage

From source file:ble.AndroidBle.java

@Override
public boolean requestConnect(String address) {
    BluetoothGatt gatt = mBluetoothGatts.get(address);
    if (gatt != null && gatt.getServices().size() == 0) {
        return false;
    }//from  w ww . j  a  v  a2s .co m

    mService.addBleRequest(new BleRequest(RequestType.CONNECT_GATT, address));
    return true;
}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

private void discoverCharacteristics(final BluetoothGatt gatt) {
    if (gatt.getServices().size() == 0) {
        Log.i(TAG, "No services found.");
    }/*from   www  .  j av  a 2s.c o  m*/

    for (BluetoothGattService gattService : gatt.getServices()) {
        storeCharacteristicsFromService(gattService);
    }

    sendBroadcast(new Intent(SERVICES_AVAILABLE));
}

From source file:com.beestar.ble.ble.ui.BleScanActivity.java

private void setBleServiceListener() {
    mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
        @Override/* w ww. j av a  2s .  c  o m*/
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                gattServiceList = gatt.getServices();
                serviceList.clear();
                for (BluetoothGattService service : gattServiceList) {
                    String serviceUuid = service.getUuid().toString();
                    serviceList.add(MyGattAttributes.lookup(serviceUuid, "Unknown") + "\n" + serviceUuid);
                    Log.i(TAG, MyGattAttributes.lookup(serviceUuid, "Unknown") + "\n" + serviceUuid);

                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                    String[] charArra = new String[characteristics.size()];
                    for (int i = 0; i < characteristics.size(); i++) {
                        String charUuid = characteristics.get(i).getUuid().toString();
                        charArra[i] = MyGattAttributes.lookup(charUuid, "Unknown") + "\n" + charUuid;
                    }
                    characteristicList.add(charArra);
                }
                mHandler.sendEmptyMessage(SERVICE_SHOW);
            }
        }
    });
    //        //Ble??
    //        mBleService.setOnLeScanListener(new BleService.OnLeScanListener() {
    //            @Override
    //            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    //                //???Ble?????
    //            }
    //        });
    //        //Ble
    //        mBleService.setOnConnectListener(new BleService.OnConnectListener() {
    //            @Override
    //            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    //                if (newState == BluetoothProfile.STATE_DISCONNECTED) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_CONNECTING) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_CONNECTED) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
    //                    //Ble
    //                }
    //            }
    //        });
    //        //Ble??
    //        mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
    //            @Override
    //            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    //
    //            }
    //        });
    //        //Ble?
    //        mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener() {
    //            @Override
    //            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    //                //???
    //            }
    //
    //            @Override
    //            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    //                //??
    //            }
    //        @Override
    //        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    //
    //        }
    //        });

    mBleService.setOnReadRemoteRssiListener(new BleService.OnReadRemoteRssiListener() {
        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            Log.i(TAG, "onReadRemoteRssi: rssi = " + rssi);
        }
    });
}

From source file:com.junkchen.blelib.sample.BleScanActivity.java

private void setBleServiceListener() {
    mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
        @Override// ww w.  j  a  v a2 s.  com
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                gattServiceList = gatt.getServices();
                characteristicList = new ArrayList<>();
                serviceList.clear();
                for (BluetoothGattService service : gattServiceList) {
                    String serviceUuid = service.getUuid().toString();
                    serviceList.add(MyGattAttributes.lookup(serviceUuid, "Unknown") + "\n" + serviceUuid);
                    Log.i(TAG, MyGattAttributes.lookup(serviceUuid, "Unknown") + "\n" + serviceUuid);

                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                    String[] charArra = new String[characteristics.size()];
                    for (int i = 0; i < characteristics.size(); i++) {
                        String charUuid = characteristics.get(i).getUuid().toString();
                        charArra[i] = MyGattAttributes.lookup(charUuid, "Unknown") + "\n" + charUuid;
                    }
                    characteristicList.add(charArra);
                }
                mHandler.sendEmptyMessage(SERVICE_SHOW);
            }
        }
    });
    //        //Ble??
    //        mBleService.setOnLeScanListener(new BleService.OnLeScanListener() {
    //            @Override
    //            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    //                //???Ble?????
    //            }
    //        });
    //        //Ble
    //        mBleService.setOnConnectListener(new BleService.OnConnectListener() {
    //            @Override
    //            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    //                if (newState == BluetoothProfile.STATE_DISCONNECTED) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_CONNECTING) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_CONNECTED) {
    //                    //Ble
    //                } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
    //                    //Ble
    //                }
    //            }
    //        });
    //        //Ble??
    //        mBleService.setOnServicesDiscoveredListener(new BleService.OnServicesDiscoveredListener() {
    //            @Override
    //            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    //
    //            }
    //        });
    //        //Ble?
    //        mBleService.setOnDataAvailableListener(new BleService.OnDataAvailableListener() {
    //            @Override
    //            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    //                //???
    //            }
    //
    //            @Override
    //            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    //                //??
    //            }
    //        @Override
    //        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    //
    //        }
    //        });

    mBleService.setOnReadRemoteRssiListener(new BleService.OnReadRemoteRssiListener() {
        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            Log.i(TAG, "onReadRemoteRssi: rssi = " + rssi);
        }
    });
}

From source file:ble.AndroidBle.java

@Override
public ArrayList<BleGattService> getServices(String address) {
    BluetoothGatt gatt = mBluetoothGatts.get(address);
    if (gatt == null) {
        return null;
    }//from w  ww .ja  v a 2 s  .c  om

    ArrayList<BleGattService> list = new ArrayList<BleGattService>();
    List<BluetoothGattService> services = gatt.getServices();
    for (BluetoothGattService s : services) {
        BleGattService service = new BleGattService(s);
        // service.setInfo(btQuery.getGattServiceInfo(s.getUuid()));
        list.add(service);
    }
    return list;
}

From source file:kr.ac.kaist.resl.sensorservice.BluetoothService.java

private boolean readCharacteristics(BluetoothGatt gatt) {
    if (null == gatt)
        return false;

    String deviceName = gatt.getDevice().getName();
    List<BluetoothGattService> gattServices = gatt.getServices();

    if (gattServices == null)
        return false;

    String unknownServiceString = getResources().getString(R.string.unknown_service);
    String unknownCharaString = getResources().getString(R.string.unknown_characteristic);

    // Find the device node from device-service-characteristic tree
    Node<String> deviceNode = null;
    for (Node<String> node : device_service_characteristicTree.getChildren()) {
        if (node.data.equals(deviceName)) {
            deviceNode = node;//w w w.j  a  v  a  2 s  . co  m
            break;
        }
    }

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        String serviceUuid = gattService.getUuid().toString();
        String serviceName = SampleGattAttributes.lookup(serviceUuid, unknownServiceString);

        // Find desired service
        for (Node<String> serviceNode : deviceNode.getChildren()) {
            if (serviceName.equals(serviceNode.data)) {
                List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();

                // Loops through available Characteristics.
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                    String characteristicUuid = gattCharacteristic.getUuid().toString();
                    String characteristicName = SampleGattAttributes.lookup(characteristicUuid,
                            unknownCharaString);

                    // Find desired characteristic of selected service
                    for (Node<String> characteristicNode : serviceNode.getChildren()) {
                        if (characteristicName.equals(characteristicNode.data)) {
                            Log.i(null, "FOUND DESIRED characteristic!!!");
                            readCharacteristic(gatt, gattCharacteristic);
                            break;
                        }
                    }

                }
            }
        }
    }
    return true;

}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

public void printServices(String address) {
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    if (bluetoothGatt == null) {
        Log.d(TAG, "No connection found for: " + address);
        return;//from w w  w . j  a  va 2  s. c om
    }
    for (BluetoothGattService service : bluetoothGatt.getServices()) {
        Log.d(TAG, "Service ================================");
        Log.d(TAG, "Service UUID: " + service.getUuid());
        Log.d(TAG, "Service Type: " + service.getType());

        for (BluetoothGattCharacteristic charact : service.getCharacteristics()) {
            Log.d(TAG, "Charact UUID: " + charact.getUuid());
            Log.d(TAG, "Charact prop: " + charact.getProperties());

            if (charact.getValue() != null) {
                Log.d(TAG, "Charact Value: " + new String(charact.getValue()));
            }
        }
    }
}

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  a2  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:com.megster.cordova.ble.central.Peripheral.java

public JSONObject asJSONObject(BluetoothGatt gatt) {

    JSONObject json = asJSONObject();/* ww w . j  a  va 2  s .  c  om*/

    try {
        JSONArray servicesArray = new JSONArray();
        JSONArray characteristicsArray = new JSONArray();
        json.put("services", servicesArray);
        json.put("characteristics", characteristicsArray);

        if (connected && gatt != null) {
            for (BluetoothGattService service : gatt.getServices()) {
                servicesArray.put(UUIDHelper.uuidToString(service.getUuid()));

                for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                    JSONObject characteristicsJSON = new JSONObject();
                    characteristicsArray.put(characteristicsJSON);

                    characteristicsJSON.put("service", UUIDHelper.uuidToString(service.getUuid()));
                    characteristicsJSON.put("characteristic",
                            UUIDHelper.uuidToString(characteristic.getUuid()));
                    //characteristicsJSON.put("instanceId", characteristic.getInstanceId());

                    characteristicsJSON.put("properties", Helper.decodeProperties(characteristic));
                    // characteristicsJSON.put("propertiesValue", characteristic.getProperties());

                    if (characteristic.getPermissions() > 0) {
                        characteristicsJSON.put("permissions", Helper.decodePermissions(characteristic));
                        // characteristicsJSON.put("permissionsValue", characteristic.getPermissions());
                    }

                    JSONArray descriptorsArray = new JSONArray();

                    for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                        JSONObject descriptorJSON = new JSONObject();
                        descriptorJSON.put("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
                        descriptorJSON.put("value", descriptor.getValue()); // always blank

                        if (descriptor.getPermissions() > 0) {
                            descriptorJSON.put("permissions", Helper.decodePermissions(descriptor));
                            // descriptorJSON.put("permissionsValue", descriptor.getPermissions());
                        }
                        descriptorsArray.put(descriptorJSON);
                    }
                    if (descriptorsArray.length() > 0) {
                        characteristicsJSON.put("descriptors", descriptorsArray);
                    }
                }
            }
        }
    } catch (JSONException e) { // TODO better error handling
        e.printStackTrace();
    }

    return json;
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

private void getServicesManage(BluetoothGatt gatt, int status) {
    String deviceAddress = getDeviceAddress(gatt);
    JSONObject obj = new JSONObject();
    JSONArray ary = new JSONArray();
    if (getServicesCC.get(deviceAddress) != null) {
        if (deviceServices.get(deviceAddress) == null) {
            deviceServices.put(deviceAddress, gatt.getServices());
        }// w  w w.ja va  2  s  .  c  o m
        if (deviceServices.get(deviceAddress) != null) {
            deviceServices.get(deviceAddress).remove(deviceAddress);
            deviceServices.put(deviceAddress, gatt.getServices());
        }
        Tools.addProperty(obj, Tools.DEVICE_ADDRESS, deviceAddress);
        for (int i = 0; i < deviceServices.get(deviceAddress).size(); i++) {
            JSONObject infoObj = new JSONObject();
            Tools.addProperty(infoObj, Tools.SERVICE_INDEX, i);
            Tools.addProperty(infoObj, Tools.SERVICE_UUID, deviceServices.get(deviceAddress).get(i).getUuid());
            Tools.addProperty(infoObj, Tools.SERVICE_NAME,
                    Tools.lookup(deviceServices.get(deviceAddress).get(i).getUuid()));
            ary.put(infoObj);
        }
        Tools.addProperty(obj, Tools.SERVICES, ary);
        getServicesCC.get(deviceAddress).success(obj);
        getServicesCC.remove(deviceAddress);
    }
}