Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#PROPERTY_WRITE

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#PROPERTY_WRITE . 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: SerialSocket.java    From SimpleBluetoothLeTerminal with MIT License 6 votes vote down vote up
@Override
boolean connectCharacteristics(BluetoothGattService gattService) {
    Log.d(TAG, "service nrf uart");
    BluetoothGattCharacteristic rw2 = gattService.getCharacteristic(BLUETOOTH_LE_NRF_CHAR_RW2);
    BluetoothGattCharacteristic rw3 = gattService.getCharacteristic(BLUETOOTH_LE_NRF_CHAR_RW3);
    if (rw2 != null && rw3 != null) {
        int rw2prop = rw2.getProperties();
        int rw3prop = rw3.getProperties();
        boolean rw2write = (rw2prop & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
        boolean rw3write = (rw3prop & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
        Log.d(TAG, "characteristic properties " + rw2prop + "/" + rw3prop);
        if (rw2write && rw3write) {
            onSerialConnectError(new IOException("multiple write characteristics (" + rw2prop + "/" + rw3prop + ")"));
        } else if (rw2write) {
            writeCharacteristic = rw2;
            readCharacteristic = rw3;
        } else if (rw3write) {
            writeCharacteristic = rw3;
            readCharacteristic = rw2;
        } else {
            onSerialConnectError(new IOException("no write characteristic (" + rw2prop + "/" + rw3prop + ")"));
        }
    }
    return true;
}
 
Example 2
Source File: BleServicesAdapter.java    From BleSensorTag with MIT License 6 votes vote down vote up
private static String getModeString(int prop) {
    final StringBuilder modeBuilder = new StringBuilder();
    if ((prop & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        modeBuilder.append(MODE_READ);
    }
    if ((prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        if (modeBuilder.length() > 0) {
            modeBuilder.append("/");
        }
        modeBuilder.append(MODE_NOTIFY);
    }
    if ((prop & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
        if (modeBuilder.length() > 0) {
            modeBuilder.append("/");
        }
        modeBuilder.append(MODE_WRITE);
    }
    return modeBuilder.toString();
}
 
Example 3
Source File: CompositeClientTransactionTests.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testSecondTransactionInCompositeExecutesOnConnectionThread() {
    byte[] someData = new byte[]{0x16, 0x11, 0x0F};
    BluetoothGattCharacteristic characteristic =
            new BluetoothGattCharacteristic(characteristicUuid,
                    BluetoothGattCharacteristic.PROPERTY_WRITE,
                    BluetoothGattCharacteristic.PERMISSION_WRITE);
    WriteGattCharacteristicMockTransaction writeGattCharacteristicMockTransaction =
            new WriteGattCharacteristicMockTransaction(conn, GattState.WRITE_CHARACTERISTIC_SUCCESS, characteristic, someData, false);
    WriteGattCharacteristicMockTransaction writeGattCharacteristicMockTransactionTwo =
            new WriteGattCharacteristicMockTransaction(conn, GattState.WRITE_CHARACTERISTIC_SUCCESS, characteristic, someData, false);
    ArrayList<GattTransaction> txList = new ArrayList<>(2);
    txList.add(writeGattCharacteristicMockTransaction);
    txList.add(writeGattCharacteristicMockTransactionTwo);
    CompositeClientTransaction compositeClientTransaction = new CompositeClientTransaction(conn, txList);
    conn.runTx(compositeClientTransaction, callback -> {
        // ensure that we are on the fake main thread
        Assert.assertEquals(Thread.currentThread().getName(), conn.getMainHandler().getLooper().getThread().getName());
        Assert.assertEquals(callback.resultStatus, TransactionResult.TransactionResultStatus.SUCCESS);
    });
}
 
Example 4
Source File: P_DeviceServiceManager.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
private static int getProperty(BleDevice.ReadWriteListener.Type type)
{
	switch(type)
	{
		case READ:
		case POLL:
		case PSUEDO_NOTIFICATION:	return		BluetoothGattCharacteristic.PROPERTY_READ;
		
           case WRITE_NO_RESPONSE:     return      BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
           case WRITE_SIGNED:          return      BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE;
           case WRITE:					return		BluetoothGattCharacteristic.PROPERTY_WRITE;
   
		case ENABLING_NOTIFICATION:
		case DISABLING_NOTIFICATION:
		case NOTIFICATION:
		case INDICATION:			return		BluetoothGattCharacteristic.PROPERTY_INDICATE			|
												BluetoothGattCharacteristic.PROPERTY_NOTIFY				;
	}
	
	return 0x0;
}
 
Example 5
Source File: CompositeClientTransactionTests.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testThatRunningTxOnWrongThreadWillThrow() throws InterruptedException {
    byte[] someData = new byte[]{0x16, 0x11, 0x0F};
    BluetoothGattCharacteristic characteristic =
            new BluetoothGattCharacteristic(characteristicUuid,
                    BluetoothGattCharacteristic.PROPERTY_WRITE,
                    BluetoothGattCharacteristic.PERMISSION_WRITE);
    CountDownLatch cdl = new CountDownLatch(1);
    WriteGattCharacteristicMockTransaction writeGattCharacteristicMockTransaction =
            new WriteGattCharacteristicMockTransaction(conn, GattState.WRITE_CHARACTERISTIC_SUCCESS, characteristic, someData, false);
    singleThreadExecutor.execute(() -> {
        try {
            writeGattCharacteristicMockTransaction.commit(callback -> {
                Assert.fail("Should always throw illegal state exception");
                cdl.countDown();
            });
        } catch (IllegalStateException ex) {
            assertTrue(ex.getMessage().contains(device.getName()));
            cdl.countDown();
        }
    });
    cdl.await(1000, TimeUnit.MILLISECONDS);
}
 
Example 6
Source File: CharacteristicDetailActivity.java    From AndroidBleManager with Apache License 2.0 6 votes vote down vote up
private String getPropertyString(int property){
    StringBuilder sb = new StringBuilder();
    // 可读
    if ((property & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        sb.append("Read ");
    }
    // 可写,注:要 & 其可写的两个属性
    if ((property & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0
            || (property & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
        sb.append("Write ");
    }
    // 可通知,可指示
    if ((property & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0
            || (property & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
        sb.append("Notity Indicate ");
    }
    // 广播
    if ((property & BluetoothGattCharacteristic.PROPERTY_BROADCAST) > 0){
        sb.append("Broadcast ");
    }
    sb.deleteCharAt(sb.length() - 1);
    return sb.toString();
}
 
Example 7
Source File: MainActivity.java    From easyble-x with Apache License 2.0 6 votes vote down vote up
private String getPropertiesString(Item node) {
    StringBuilder sb = new StringBuilder();
    int[] properties = new int[]{BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PROPERTY_INDICATE,
            BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE, BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE};
    String[] propertyStrs = new String[]{"WRITE", "INDICATE", "NOTIFY", "READ", "SIGNED_WRITE", "WRITE_NO_RESPONSE"};
    for (int i = 0; i < properties.length; i++) {
        int property = properties[i];
        if ((node.characteristic.getProperties() & property) != 0) {
            if (sb.length() != 0) {
                sb.append(", ");
            }
            sb.append(propertyStrs[i]);
            if (property == BluetoothGattCharacteristic.PROPERTY_NOTIFY || property == BluetoothGattCharacteristic.PROPERTY_INDICATE) {
                node.hasNotifyProperty = true;
            }
            if (property == BluetoothGattCharacteristic.PROPERTY_WRITE || property == BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) {
                node.hasWriteProperty = true;
            }
            if (property == BluetoothGattCharacteristic.PROPERTY_READ) {
                node.hasReadProperty = true;
            }
        }
    }
    return sb.toString();
}
 
Example 8
Source File: ParserUtils.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String getProperties(final BluetoothGattCharacteristic characteristic) {
    final int properties = characteristic.getProperties();
    final StringBuilder builder = new StringBuilder();
    if ((properties & BluetoothGattCharacteristic.PROPERTY_BROADCAST) > 0)
        builder.append("B ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS) > 0)
        builder.append("E ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0)
        builder.append("I ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)
        builder.append("N ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) > 0)
        builder.append("R ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) > 0)
        builder.append("SW ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0)
        builder.append("W ");
    if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0)
        builder.append("WNR ");

    if (builder.length() > 0) {
        builder.setLength(builder.length() - 1);
        builder.insert(0, "[");
        builder.append("]");
    }
    return builder.toString();
}
 
Example 9
Source File: CharacteristicDetailsActivity.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String getCharacteristicPropertiesString(int props) {
 String propertiesString = String.format("0x%04X [", props);
 if((props & BluetoothGattCharacteristic.PROPERTY_READ) != 0) propertiesString += "read ";
 if((props & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) propertiesString += "write ";
 if((props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) propertiesString += "notify ";
 if((props & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) propertiesString += "indicate ";
 if((props & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0) propertiesString += "write_no_response ";
 propertiesString += "]";
 return propertiesString;

}
 
Example 10
Source File: ChangeCharActivity.java    From bleTester with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
	// TODO Auto-generated method stub
	bleService = ((BleService.LocalBinder) service).getService();
	gattChar = bleService.mBluetoothGatt.getService(serUuid)
			.getCharacteristic(charUuid);
	bleService.mBluetoothGatt.readCharacteristic(gattChar);
	if (gattChar.getDescriptors().size() != 0) {
		BluetoothGattDescriptor des = gattChar.getDescriptors().get(0);
		des.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
		bleService.mBluetoothGatt.writeDescriptor(des);
	}
	int prop = gattChar.getProperties();
	if ((prop & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
		bleService.mBluetoothGatt.setCharacteristicNotification(
				gattChar, false);
	}
	if ((prop & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
		bleService.mBluetoothGatt.setCharacteristicNotification(
				gattChar, false);
	}
	if ((prop & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
		bleService.mBluetoothGatt.setCharacteristicNotification(
				gattChar, false);
	}
	if ((prop & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
		bleService.mBluetoothGatt.setCharacteristicNotification(
				gattChar, true);
	}
}
 
Example 11
Source File: BleGattCallback.java    From attach with GNU General Public License v3.0 5 votes vote down vote up
private String getProperties(int bitmask) {
    String properties = "";

    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_BROADCAST) != 0) {
        properties += "broadcast, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS) != 0) {
        properties += "extended props, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
        properties += "indicate, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
        properties += "notify, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_READ) != 0) {
        properties += "read, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0) {
        properties += "signed write, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) {
        properties += "write, ";
    }
    if ((bitmask & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0) {
        properties += "write no response, ";
    }

    return properties.isEmpty() ? "" : properties.substring(0, properties.length() - 2);
}
 
Example 12
Source File: GattNotifyIndicateTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void NotifyServerCharacteristicTest() {
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED);
    characteristic.setValue(fakeData);
    NotifyGattServerCharacteristicMockTransaction notifyChar = new NotifyGattServerCharacteristicMockTransaction(FitbitGatt.getInstance().getServer(), device, GattState.NOTIFY_CHARACTERISTIC_SUCCESS, characteristic, false, false);
    conn.runTx(notifyChar, result -> {
        assert(result.resultStatus.equals(TransactionResult.TransactionResultStatus.SUCCESS));
    });
}
 
Example 13
Source File: ConnectionModule.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
@Provides
static CharacteristicPropertiesParser provideCharacteristicPropertiesParser() {
    return new CharacteristicPropertiesParser(BluetoothGattCharacteristic.PROPERTY_BROADCAST,
            BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
            BluetoothGattCharacteristic.PROPERTY_WRITE,
            BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PROPERTY_INDICATE,
            BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE);
}
 
Example 14
Source File: GattNotifyIndicateTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void failToSubscribeToNotificationTest() {
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED);
    SubscribeToCharacteristicNotificationsMockTransaction subscribeChar = new SubscribeToCharacteristicNotificationsMockTransaction(conn, GattState.ENABLE_CHARACTERISTIC_NOTIFICATION_SUCCESS, characteristic, fakeData, true);
    conn.runTx(subscribeChar, result -> {
        assert(result.resultStatus.equals(TransactionResult.TransactionResultStatus.TIMEOUT));
    });
}
 
Example 15
Source File: GattNotifyIndicateTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void subscribeToNotificationTest() {
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED);
    SubscribeToCharacteristicNotificationsMockTransaction subscribeChar = new SubscribeToCharacteristicNotificationsMockTransaction(conn, GattState.ENABLE_CHARACTERISTIC_NOTIFICATION_SUCCESS, characteristic, fakeData, false);
    conn.runTx(subscribeChar, result -> {
        assert(result.resultStatus.equals(TransactionResult.TransactionResultStatus.SUCCESS) && result.resultState.equals(subscribeChar.getSuccessState()));
    });
}
 
Example 16
Source File: BluetoothUtils.java    From AndroidBleManager with Apache License 2.0 5 votes vote down vote up
public static boolean isCharacteristicWrite(int property){
    if ((property & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0
            || (property & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
        return true;
    }
    return false;
}
 
Example 17
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 4 votes vote down vote up
@Override
public void write(final BleDevice device, String serviceUuid, String writeUuid, byte[] data, final BleWriteCallback callback) {
    checkNotNull(callback, BleWriteCallback.class);
    if (data == null || data.length < 1 || data.length > 509) {
        callback.onFailure(BleCallback.FAIL_OTHER, data == null ? "data is null" :
                "data length must range from 1 to 509", device);
        return;
    }
    if (data.length > 20) {
        Logger.w("data length is greater than the default(20 bytes), make sure  MTU >= " + (data.length + 3));
    }
    if (!checkConnection(device, callback)) {
        return;
    }
    BluetoothGatt gatt = mGattMap.get(device.address);
    if (!checkUuid(serviceUuid, writeUuid, gatt, device, callback)) {
        return;
    }

    OperationIdentify identify = getOperationIdentifyFromMap(mWriteCallbackMap, device.address, serviceUuid, writeUuid);
    if (identify != null) {
        mWriteCallbackMap.put(identify, callback);
    } else {
        mWriteCallbackMap.put(new OperationIdentify(device.address, serviceUuid, writeUuid), callback);
    }

    BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(writeUuid));
    boolean writable = (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE |
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0;
    if (!writable) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "the characteristic is not writable", device);
            }
        });
        return;
    }
    if (!characteristic.setValue(data) || !gatt.writeCharacteristic(characteristic)) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onFailure(BleCallback.FAIL_OTHER, "write fail because of unknown reason", device);
            }
        });
    }
}
 
Example 18
Source File: P_DeviceServiceManager.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
static BleDevice.ReadWriteListener.Type modifyResultType(BleCharacteristicWrapper char_native, BleDevice.ReadWriteListener.Type type)
{
	if( !char_native.isNull())
	{
		if( type == Type.NOTIFICATION )
		{
			if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0x0 )
			{
				if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0x0 )
				{
					type = Type.INDICATION;
				}
			}
		}
		else if( type == Type.WRITE )
		{
			if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0x0 )
			{
				if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0x0 )
				{
					type = Type.WRITE_NO_RESPONSE;
				}
				else if( (char_native.getCharacteristic().getProperties() & BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE) != 0x0 )
				{
					type = Type.WRITE_SIGNED;
				}
			}
			//--- RB > Check the write type on the characteristic, in case this char has multiple write types
			int writeType = char_native.getCharacteristic().getWriteType();
			if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE)
			{
				type = Type.WRITE_NO_RESPONSE;
			}
			else if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_SIGNED)
			{
				type = Type.WRITE_SIGNED;
			}
		}
	}
	
	return type;
}
 
Example 19
Source File: GattDatabase.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public final Properties write()
{
    m_properties |= BluetoothGattCharacteristic.PROPERTY_WRITE;
    return this;
}
 
Example 20
Source File: BleConnectWorker.java    From Android-BluetoothKit with Apache License 2.0 4 votes vote down vote up
private boolean isCharacteristicWritable(BluetoothGattCharacteristic characteristic) {
    return characteristic != null && (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
}