Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#getPermissions()

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#getPermissions() . 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
/**
 * To prevent the characteristic from changing out from under us we need to copy it
 * <p>
 * This may happen under high throughput/concurrency
 *
 * @param characteristic The characteristic to be copied
 * @return The shallow-ish copy of the characteristic
 */
public @Nullable
BluetoothGattCharacteristicCopy copyCharacteristic(@Nullable BluetoothGattCharacteristic characteristic) {
    if (null == characteristic || null == characteristic.getUuid()) {
        return null;
    }
    BluetoothGattCharacteristicCopy newCharacteristic =
            new BluetoothGattCharacteristicCopy(characteristic.getUuid(),
                    characteristic.getProperties(), characteristic.getPermissions());
    if (characteristic.getValue() != null) {
        newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
    }
    if (!characteristic.getDescriptors().isEmpty()) {
        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);
        }
    }
    if (characteristic.getService() != null) {
        BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(characteristic.getService().getUuid(), characteristic.getService().getType());
        newService.addCharacteristic(newCharacteristic);
    }
    return newCharacteristic;
}
 
Example 2
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 3
Source File: Helper.java    From react-native-ble-manager with Apache License 2.0 5 votes vote down vote up
public static WritableMap decodePermissions(BluetoothGattCharacteristic characteristic) {

		// NOTE: props strings need to be consistent across iOS and Android
		WritableMap props = Arguments.createMap();
		int permissions = characteristic.getPermissions();

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_READ) != 0x0 ) {
			props.putString("Read", "Read");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_WRITE) != 0x0 ) {
			props.putString("Write", "Write");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED) != 0x0 ) {
			props.putString("ReadEncrypted", "ReadEncrypted");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED) != 0x0 ) {
			props.putString("WriteEncrypted", "WriteEncrypted");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM) != 0x0 ) {
			props.putString("ReadEncryptedMITM", "ReadEncryptedMITM");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM) != 0x0 ) {
			props.putString("WriteEncryptedMITM", "WriteEncryptedMITM");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED) != 0x0 ) {
			props.putString("WriteSigned", "WriteSigned");
		}

		if ((permissions & BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM) != 0x0 ) {
			props.putString("WriteSignedMITM", "WriteSignedMITM");
		}

		return props;
	}
 
Example 4
Source File: BleGattCharacter.java    From Android-BluetoothKit with Apache License 2.0 5 votes vote down vote up
public BleGattCharacter(BluetoothGattCharacteristic characteristic) {
    this.uuid = new ParcelUuid(characteristic.getUuid());
    this.property = characteristic.getProperties();
    this.permissions = characteristic.getPermissions();

    for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
        getDescriptors().add(new BleGattDescriptor(descriptor));
    }
}
 
Example 5
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 6
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 7
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
String getStringRepresentationOfPermissionsForCharacteristic(BluetoothGattCharacteristic characteristic) {
    StringBuilder permissionBuilder = new StringBuilder();
    ArrayList<Integer> permissions = new ArrayList<>(8);
    int characteristicPermissions = characteristic.getPermissions();
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_READ) == BluetoothGattCharacteristic.PERMISSION_READ) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED) == BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM) == BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE) == BluetoothGattCharacteristic.PERMISSION_WRITE) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED) == BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM) == BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED) == BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM) == BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM);
    }
    for (int i = 0; i < permissions.size(); i++) {
        int permission = permissions.get(i);
        switch (permission) {
            case BluetoothGattCharacteristic.PERMISSION_READ:
                permissionBuilder.append("read");
                break;
            case BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED:
                permissionBuilder.append("read-encrypted");
                break;
            case BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM:
                permissionBuilder.append("read-encrypted-mitm");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE:
                permissionBuilder.append("write");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED:
                permissionBuilder.append("write-encrypted");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM:
                permissionBuilder.append("write-encrypted-mitm");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED:
                permissionBuilder.append("write-signed");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM:
                permissionBuilder.append("write-signed-mitm");
                break;
            default:
                permissionBuilder.append("unknown");
        }
        if (i < permissions.size() - 1) {
            permissionBuilder.append(", ");
        }
    }
    return permissionBuilder.toString();
}
 
Example 8
Source File: Peripheral.java    From react-native-ble-manager with Apache License 2.0 4 votes vote down vote up
public WritableMap asWritableMap(BluetoothGatt gatt) {

		WritableMap map = asWritableMap();

		WritableArray servicesArray = Arguments.createArray();
		WritableArray characteristicsArray = Arguments.createArray();

		if (connected && gatt != null) {
			for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext();) {
				BluetoothGattService service = it.next();
				WritableMap serviceMap = Arguments.createMap();
				serviceMap.putString("uuid", UUIDHelper.uuidToString(service.getUuid()));

				for (Iterator<BluetoothGattCharacteristic> itCharacteristic = service.getCharacteristics()
						.iterator(); itCharacteristic.hasNext();) {
					BluetoothGattCharacteristic characteristic = itCharacteristic.next();
					WritableMap characteristicsMap = Arguments.createMap();

					characteristicsMap.putString("service", UUIDHelper.uuidToString(service.getUuid()));
					characteristicsMap.putString("characteristic", UUIDHelper.uuidToString(characteristic.getUuid()));

					characteristicsMap.putMap("properties", Helper.decodeProperties(characteristic));

					if (characteristic.getPermissions() > 0) {
						characteristicsMap.putMap("permissions", Helper.decodePermissions(characteristic));
					}

					WritableArray descriptorsArray = Arguments.createArray();

					for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
						WritableMap descriptorMap = Arguments.createMap();
						descriptorMap.putString("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
						if (descriptor.getValue() != null) {
							descriptorMap.putString("value",
									Base64.encodeToString(descriptor.getValue(), Base64.NO_WRAP));
						} else {
							descriptorMap.putString("value", null);
						}

						if (descriptor.getPermissions() > 0) {
							descriptorMap.putMap("permissions", Helper.decodePermissions(descriptor));
						}
						descriptorsArray.pushMap(descriptorMap);
					}
					if (descriptorsArray.size() > 0) {
						characteristicsMap.putArray("descriptors", descriptorsArray);
					}
					characteristicsArray.pushMap(characteristicsMap);
				}
				servicesArray.pushMap(serviceMap);
			}
			map.putArray("services", servicesArray);
			map.putArray("characteristics", characteristicsArray);
		}

		return map;
	}
 
Example 9
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);
		}
  
    }