Java Code Examples for android.bluetooth.BluetoothGattService#getType()

The following examples show how to use android.bluetooth.BluetoothGattService#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will return the copy of the service
 *
 * @param service The gatt service
 * @return a shallow-ish copy of the service
 */

public @Nullable
BluetoothGattServiceCopy copyService(@Nullable BluetoothGattService service) {
    if (null == service || null == service.getUuid()) {
        return null;
    }
    BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(service.getUuid(), service.getType());
    if (!service.getIncludedServices().isEmpty()) {
        for (BluetoothGattService includedService : service.getIncludedServices()) {
            BluetoothGattServiceCopy newGattService = new BluetoothGattServiceCopy(includedService.getUuid(), includedService.getType());
            newService.addService(newGattService);
        }
    }
    if (!service.getCharacteristics().isEmpty()) {
        // why not use the copy characteristic method, it will implicitly link itself to the null service
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            BluetoothGattCharacteristicCopy newCharacteristic = new BluetoothGattCharacteristicCopy(characteristic.getUuid(), characteristic.getProperties(), characteristic.getPermissions());
            if (characteristic.getValue() != null) {
                newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
            }
            // why not use the copy descriptor method?  It will implicitly link itself to the null characteristic
            for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                BluetoothGattDescriptorCopy newDescriptor = new BluetoothGattDescriptorCopy(descriptor.getUuid(), descriptor.getPermissions());
                if (descriptor.getValue() != null) {
                    newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
                }
                newCharacteristic.addDescriptor(newDescriptor);
            }
            newService.addCharacteristic(newCharacteristic);
        }
    }
    return newService;
}
 
Example 2
Source File: LoggerUtilBluetoothServices.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
private static String createServiceType(BluetoothGattService bluetoothGattService) {
    if (bluetoothGattService.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY) {
        return "Primary Service";
    } else {
        return "Secondary Service";
    }
}
 
Example 3
Source File: GattPeripheral.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
protected void GattCB_onServiceDiscovered()
{
    Debug.TRACE();
    __releaseFile();
    for (Iterator iterator = getGatt().getServices().iterator(); iterator.hasNext();)
    {
        BluetoothGattService bluetoothgattservice = (BluetoothGattService)iterator.next();
        StringBuilder stringbuilder = new StringBuilder();
        String s;
        Iterator iterator1;
        if (bluetoothgattservice.getType() == 0)
        {
            s = "Primary";
        } else
        {
            s = "Secondary";
        }
        Debug.DEBUG(stringbuilder.append(s).append(" service: ").append(Helper.parseUUID(bluetoothgattservice.getUuid())).toString());
        iterator1 = bluetoothgattservice.getCharacteristics().iterator();
        while (iterator1.hasNext()) 
        {
            BluetoothGattCharacteristic bluetoothgattcharacteristic = (BluetoothGattCharacteristic)iterator1.next();
            Debug.DEBUG((new StringBuilder()).append("  Characteristic: ").append(Helper.parseUUID(bluetoothgattcharacteristic.getUuid())).toString());
            Debug.DEBUG((new StringBuilder()).append("    - Properties: ").append(Helper.parseProperties(bluetoothgattcharacteristic.getProperties())).toString());
            Iterator iterator2 = bluetoothgattcharacteristic.getDescriptors().iterator();
            while (iterator2.hasNext()) 
            {
                BluetoothGattDescriptor bluetoothgattdescriptor = (BluetoothGattDescriptor)iterator2.next();
                Debug.DEBUG((new StringBuilder()).append("    Descriptor: ").append(Helper.parseUUID(bluetoothgattdescriptor.getUuid())).toString());
            }
        }
    }

    boolean flag = init();
    Debug.ASSERT_TRUE(flag);
    if (!flag)
    {
        disconnectInternal();
    }
}
 
Example 4
Source File: BluetoothLeService.java    From OpenFit with MIT License 5 votes vote down vote up
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(LOG_TAG, "onServicesDiscovered: "+status);
    if(status == BluetoothGatt.GATT_SUCCESS) {    
        // loops through available GATT Services.
        for(BluetoothGattService gattService : gatt.getServices()) {
            String uuid = gattService.getUuid().toString();
            String type = gattServiceType[gattService.getType()];

            Log.d(LOG_TAG, "onServicesDiscovered type: "+type);
            Log.d(LOG_TAG, "onServicesDiscovered uuid: "+uuid);
            //Log.d(LOG_TAG, "onServicesDiscovered: getCharacteristic: "+mWriteCharacteristic);
            for(BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
                String cUuid = gattCharacteristic.getUuid().toString();
                int cInstanceId = gattCharacteristic.getInstanceId();
                int cPermissions = gattCharacteristic.getPermissions();
                int cProperties = gattCharacteristic.getProperties();
                byte[] cValue = gattCharacteristic.getValue();
                int cWriteType = gattCharacteristic.getWriteType();

                Log.d(LOG_TAG, "onServicesDiscovered cUuid: "+cUuid);
                Log.d(LOG_TAG, "onServicesDiscovered cInstanceId: "+cInstanceId);
                Log.d(LOG_TAG, "onServicesDiscovered cPermissions: "+cPermissions);
                Log.d(LOG_TAG, "onServicesDiscovered cProperties: "+cProperties);
                Log.d(LOG_TAG, "onServicesDiscovered cValue: "+cValue);
                Log.d(LOG_TAG, "onServicesDiscovered cWriteType: "+cWriteType);
            }
        }
        Log.d(LOG_TAG, "BluetoothLe Service discovered: "+status);
        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
    }
    else {
        Log.d(LOG_TAG, "BluetoothLe onServicesDiscovered received: "+status);
    }
}
 
Example 5
Source File: BluetoothLE.java    From Makeblock-App-For-Android with MIT License 5 votes vote down vote up
private BluetoothGattCharacteristic characteristicForProperty(int property){
		List<BluetoothGattService> list = mBLE.getSupportedGattServices();
		if (list == null) return null;  
        for (BluetoothGattService gattService : list) {  
            //-----Service���ֶ���Ϣ-----//  
            int type = gattService.getType();  
            String uuid = gattService.getUuid().toString();
            //-----Characteristics���ֶ���Ϣ-----//  
            List<BluetoothGattCharacteristic> gattCharacteristics =gattService.getCharacteristics();  
            for (final BluetoothGattCharacteristic  gattCharacteristic: gattCharacteristics) {  
                  
                int permission = gattCharacteristic.getPermissions();  
                  
                int properties = gattCharacteristic.getProperties();  
//                Log.d("mb","---->char property:"+Utils.getCharPropertie(property)+" - "+(property&BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)+" - "+BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE);  
                int p = (property&properties);
                int np = property;
                if(np==p){
                	if(np==(int)(BluetoothGattCharacteristic.PROPERTY_WRITE|BluetoothGattCharacteristic.PROPERTY_READ)){
	                //UUID_KEY_DATA�ǿ��Ը�����ģ�鴮��ͨ�ŵ�Characteristic 
                		if((int)(uuid.indexOf("ffe4"))>0){
                			return gattCharacteristic; 
                		}
                	}else{
                		return gattCharacteristic; 
                	}
                }  
            }
        }//  
        return null;
	}
 
Example 6
Source File: ServicesListAdapter.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
	// get already available view or create new if necessary
	FieldReferences fields;
       if (convertView == null) {
       	convertView = mInflater.inflate(R.layout.peripheral_list_services_item, null);
       	fields = new FieldReferences();
       	fields.serviceName = (TextView)convertView.findViewById(R.id.peripheral_list_services_name);
       	fields.serviceUuid = (TextView)convertView.findViewById(R.id.peripheral_list_services_uuid);
       	fields.serviceType = (TextView)convertView.findViewById(R.id.peripheral_list_service_type);
           convertView.setTag(fields);
       } else {
           fields = (FieldReferences) convertView.getTag();
       }			
	
       // set proper values into the view
       BluetoothGattService service = mBTServices.get(position);
       String uuid = service.getUuid().toString().toLowerCase(Locale.getDefault());
       String name = BleNamesResolver.resolveServiceName(uuid);
       String type = (service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY) ? "Primary" : "Secondary";
       
       fields.serviceName.setText(name);
       fields.serviceUuid.setText(uuid);
       fields.serviceType.setText(type);

	return convertView;
}
 
Example 7
Source File: ServicesListAdapter.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
	// get already available view or create new if necessary
	FieldReferences fields;
       if (convertView == null) {
       	convertView = mInflater.inflate(R.layout.peripheral_list_services_item, null);
       	fields = new FieldReferences();
       	fields.serviceName = (TextView)convertView.findViewById(R.id.peripheral_list_services_name);
       	fields.serviceUuid = (TextView)convertView.findViewById(R.id.peripheral_list_services_uuid);
       	fields.serviceType = (TextView)convertView.findViewById(R.id.peripheral_list_service_type);
           convertView.setTag(fields);
       } else {
           fields = (FieldReferences) convertView.getTag();
       }			
	
       // set proper values into the view
       BluetoothGattService service = mBTServices.get(position);
       String uuid = service.getUuid().toString().toLowerCase(Locale.getDefault());
       String name = BleNamesResolver.resolveServiceName(uuid);
       String type = (service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY) ? "Primary" : "Secondary";
       
       fields.serviceName.setText(name);
       fields.serviceUuid.setText(uuid);
       fields.serviceType.setText(type);

	return convertView;
}
 
Example 8
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
private void showGattServerServices(DumperContext dumpContext) {
    int n = 106;
    StringBuilder builder = new StringBuilder();
    String status = PASS_STATUS;
    String error = "";
    if (!isJsonFormat) {
        for (int i = 0; i < n; i++) {
            builder.append("=");
        }
        log(dumpContext, builder.toString());
        format(dumpContext, "| %1$32s | %2$32s |\n",
            "Service UUID",
            "Type");
    }
    String serviceUuid;
    String type;
    BluetoothGattServer server = fitbitGatt.getServer().getServer();
    List<BluetoothGattService> services = server.getServices();
    JSONArray jsonArray = new JSONArray();
    try {
        for (BluetoothGattService service : services) {
            serviceUuid = service.getUuid().toString();
            switch (service.getType()) {
                case BluetoothGattService.SERVICE_TYPE_PRIMARY:
                    type = "primary";
                    break;
                case BluetoothGattService.SERVICE_TYPE_SECONDARY:
                    type = "secondary";
                    break;
                default:
                    type = "unknown";
            }
            if (!isJsonFormat) {
                format(dumpContext, "| %1$32s | %2$32s |\n", serviceUuid, type);
            } else {
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.put(RESULT_SERVICE_UUID_KEY, serviceUuid);
                map.put(RESULT_SERVICE_TYPE_KEY, type);
                JSONObject jsonObject = makeJsonObject(map);
                jsonArray.put(jsonObject);
            }
        }
    } catch (Exception e) {
        status = FAIL_STATUS;
        error = Arrays.toString(e.getStackTrace());
    }

    builder = new StringBuilder();
    if (!isJsonFormat) {
        for (int i = 0; i < n; i++) {
            builder.append("=");
        }
        log(dumpContext, builder.toString());
    } else {
        logJsonResult(dumpContext, status, error, jsonArray);
    }
}
 
Example 9
Source File: DiscoveryResultsAdapter.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
private String getServiceType(BluetoothGattService service) {
    return service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY ? "primary" : "secondary";
}
 
Example 10
Source File: BluetoothLE.java    From Makeblock-App-For-Android with MIT License 4 votes vote down vote up
private void displayGattServices(List<BluetoothGattService> gattServices) {  
        if (gattServices == null) return;  
        mIsConnected = true;
        for (BluetoothGattService gattService : gattServices) {  
            //-----Service���ֶ���Ϣ-----//  
            int type = gattService.getType();  
            Log.e(TAG,"-->service type:"+Utils.getServiceType(type));  
            Log.e(TAG,"-->includedServices size:"+gattService.getIncludedServices().size());  
            Log.e(TAG,"-->service uuid:"+gattService.getUuid());  
              
            //-----Characteristics���ֶ���Ϣ-----//  
            List<BluetoothGattCharacteristic> gattCharacteristics =gattService.getCharacteristics();  
            for (final BluetoothGattCharacteristic  gattCharacteristic: gattCharacteristics) {  
                Log.e(TAG,"---->char uuid:"+gattCharacteristic.getUuid());  
                  
                int permission = gattCharacteristic.getPermissions();  
                Log.e(TAG,"---->char permission:"+Utils.getCharPermission(permission));  
                  
                int property = gattCharacteristic.getProperties();  
                Log.e(TAG,"---->char property:"+Utils.getCharPropertie(property));  
  
                byte[] data = gattCharacteristic.getValue();  
                if (data != null && data.length > 0) {  
                    Log.e(TAG,"---->char value:"+new String(data));  
                }  
                if((gattCharacteristic.getProperties()&BluetoothGattCharacteristic.PROPERTY_NOTIFY)==BluetoothGattCharacteristic.PROPERTY_NOTIFY){
            		mBLE.setCharacteristicNotification(gattCharacteristic, true);  
            	}
                //UUID_KEY_DATA�ǿ��Ը�����ģ�鴮��ͨ�ŵ�Characteristic  
//                if(gattCharacteristic.getUuid().toString().equals(UUID_KEY_DATA)){                    
                    //���Զ�ȡ��ǰCharacteristic���ݣ��ᴥ��mOnDataAvailable.onCharacteristicRead()  
//                    mHandler.postDelayed(new Runnable() {  
//                        @Override  
//                        public void run() {  
//                            mBLE.readCharacteristic(gattCharacteristic);  
//                        }  
//                    }, 500);  
                    
                    //����Characteristic��д��֪ͨ,�յ�����ģ������ݺ�ᴥ��mOnDataAvailable.onCharacteristicWrite()  
//                	if((gattCharacteristic.getProperties()&BluetoothGattCharacteristic.PROPERTY_NOTIFY)==BluetoothGattCharacteristic.PROPERTY_NOTIFY){
//                		mBLE.setCharacteristicNotification(gattCharacteristic, true);  
//                	}
                    //������������  
//                    gattCharacteristic.setValue("send data->");  
                    //������ģ��д������  
//                    mBLE.writeCharacteristic(gattCharacteristic);  
//                }  
                  
                //-----Descriptors���ֶ���Ϣ-----//  
//                List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors();  
//                for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {  
//                    Log.e(TAG, "-------->desc uuid:" + gattDescriptor.getUuid());  
//                    int descPermission = gattDescriptor.getPermissions();  
//                    Log.e(TAG,"-------->desc permission:"+ Utils.getDescPermission(descPermission));  
//                    
//                    byte[] desData = gattDescriptor.getValue();  
//                    if (desData != null && desData.length > 0) {  
//                        Log.e(TAG, "-------->desc value:"+ new String(desData));  
//                    }  
//                 }  
            }  
        }//  

    	if(leHandler!=null){
			Message msg = leHandler.obtainMessage(MSG_CONNECTED);
			leHandler.sendMessage(msg);
		}
  
    }