本文整理匯總了Java中android.bluetooth.BluetoothGattCharacteristic.getProperties方法的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothGattCharacteristic.getProperties方法的具體用法?Java BluetoothGattCharacteristic.getProperties怎麽用?Java BluetoothGattCharacteristic.getProperties使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.bluetooth.BluetoothGattCharacteristic
的用法示例。
在下文中一共展示了BluetoothGattCharacteristic.getProperties方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onCharacteristicWriteRequest
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public void onCharacteristicWriteRequest(final BluetoothDevice device, final int requestId, final BluetoothGattCharacteristic characteristic, final boolean preparedWrite, final boolean responseNeeded, final int offset, final byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
Log.d(TAG, "onCharacteristicWriteRequest characteristic: " + characteristic.getUuid() + ", value: " + Arrays.toString(value));
if (gattServer == null) {
return;
}
if (responseNeeded) {
if (BleUuidUtils.matches(CHARACTERISTIC_REPORT, characteristic.getUuid())) {
if (characteristic.getProperties() == (BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) {
// Output Report
onOutputReport(value);
// send empty
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, EMPTY_BYTES);
} else {
// send empty
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, EMPTY_BYTES);
}
}
}
}
示例2: findReadableCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
private BluetoothGattCharacteristic findReadableCharacteristic(BluetoothGattService service, UUID characteristicUUID) {
BluetoothGattCharacteristic characteristic = null;
int read = BluetoothGattCharacteristic.PROPERTY_READ;
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic c : characteristics) {
if ((c.getProperties() & read) != 0 && characteristicUUID.equals(c.getUuid())) {
characteristic = c;
break;
}
}
// As a last resort, try and find ANY characteristic with this UUID, even if it doesn't have the correct properties
if (characteristic == null) {
characteristic = service.getCharacteristic(characteristicUUID);
}
return characteristic;
}
示例3: readBatteryLevel
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Reads the battery level from the device.
*
* @return true if request has been sent
*/
public final boolean readBatteryLevel() {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null)
return false;
final BluetoothGattService batteryService = gatt.getService(BATTERY_SERVICE);
if (batteryService == null)
return false;
final BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC);
if (batteryLevelCharacteristic == null)
return false;
// Check characteristic property
final int properties = batteryLevelCharacteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) == 0) {
return setBatteryNotifications(true);
}
Logger.a(mLogSession, "Reading battery level...");
return readCharacteristic(batteryLevelCharacteristic);
}
示例4: broadcastUpdate
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
// This is special handling for the Heart Rate Measurement profile. Data parsing is
// carried out as per profile specifications:
// http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
}
final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate));
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
} else {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
}
}
sendBroadcast(intent);
}
示例5: enableIndications
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Enables indications on given characteristic
*
* @return true is the request has been sent, false if one of the arguments was <code>null</code> or the characteristic does not have the CCCD.
*/
protected final boolean enableIndications(final BluetoothGattCharacteristic characteristic) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null || characteristic == null)
return false;
// Check characteristic property
final int properties = characteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0)
return false;
Logger.d(mLogSession, "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
gatt.setCharacteristicNotification(characteristic, true);
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
Logger.v(mLogSession, "Enabling indications for " + characteristic.getUuid());
Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x02-00)");
return gatt.writeDescriptor(descriptor);
}
return false;
}
示例6: setBatteryNotifications
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* This method tries to enable notifications on the Battery Level characteristic.
*
* @param enable <code>true</code> to enable battery notifications, false to disable
* @return true if request has been sent
*/
public boolean setBatteryNotifications(final boolean enable) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null) {
return false;
}
final BluetoothGattService batteryService = gatt.getService(BATTERY_SERVICE);
if (batteryService == null)
return false;
final BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC);
if (batteryLevelCharacteristic == null)
return false;
// Check characteristic property
final int properties = batteryLevelCharacteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
return false;
gatt.setCharacteristicNotification(batteryLevelCharacteristic, enable);
final BluetoothGattDescriptor descriptor = batteryLevelCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
if (enable) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Logger.a(mLogSession, "Enabling battery level notifications...");
Logger.v(mLogSession, "Enabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
} else {
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
Logger.a(mLogSession, "Disabling battery level notifications...");
Logger.v(mLogSession, "Disabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x00-00)");
}
return gatt.writeDescriptor(descriptor);
}
return false;
}
示例7: setCharacteristicIndication
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
private void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled)
{
if ( mBluetoothAdapter == null || mBluetoothGatt == null )
{
NLog.d("BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor desc = characteristic.getDescriptor(CONFIG_DESCRIPTOR);
if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) == BluetoothGattCharacteristic.PROPERTY_INDICATE )
{
// Enabled remote indication
desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
}
else if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == BluetoothGattCharacteristic.PROPERTY_NOTIFY)
{
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
}
else
{
NLog.d("Error : Characteristic is not notify or indicate");
return;
}
mBluetoothGatt.writeDescriptor(desc);
}
示例8: enableCharacteristicNotification
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* enable characteristic notification
*/
public boolean enableCharacteristicNotification(BluetoothGattCharacteristic charact,
BleCharactCallback bleCallback) {
if ((charact.getProperties() | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
handleCharacteristicNotificationCallback(bleCallback);
// 設置允許獲取特征碼刷新回來的數據
return setCharacteristicNotification(getBluetoothGatt(), charact, true);
} else {
if (bleCallback != null) {
bleCallback.onFailure(new OtherException("Characteristic [not supports] readable!"));
}
return false;
}
}
示例9: findWritableCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
private BluetoothGattCharacteristic findWritableCharacteristic(BluetoothGattService service, UUID characteristicUUID, int writeType) {
try {
BluetoothGattCharacteristic characteristic = null;
// get write property
int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE;
if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
}
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic c : characteristics) {
if ((c.getProperties() & writeProperty) != 0 && characteristicUUID.equals(c.getUuid())) {
characteristic = c;
break;
}
}
// As a last resort, try and find ANY characteristic with this UUID, even if it doesn't have the correct properties
if (characteristic == null) {
characteristic = service.getCharacteristic(characteristicUUID);
}
return characteristic;
}catch (Exception e) {
Log.e(LOG_TAG, "Error on findWritableCharacteristic", e);
return null;
}
}
示例10: notifyCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* 操作2:通知
* 操作結果返回到 GattCallback 中 onCharacteristicChanged()回調方法當中
*
* @param characteristic 需要操作的特征
* @param enable 若為 true 表示要開啟notify,若為 false 表示要停止 notify
* @return 若為 true 表示設置 notify 狀態成功
*/
private boolean notifyCharacteristic(BluetoothGattCharacteristic characteristic,
boolean enable) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
LogUtils.w(TAG, "藍牙適配器為 null:notify");
return false;
}
if (mState != State.STATE_CONNECTED) {
LogUtils.w(TAG, "notify(): 當前狀態為非連接狀態");
return false;
}
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) {
LogUtils.w(TAG, "characteristic不能被notify");
return false;
}
boolean suc = mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
if (!suc) {
//notify 操作失敗
LogUtils.w(TAG, "set notify 操作失敗");
return false;
}
//特殊操作,注意三元表達式
BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(Attributes.UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR);
if (null != descriptor) {
descriptor.setValue(enable ?
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
return mBluetoothGatt.writeDescriptor(descriptor);
}
return false;
}
示例11: writeCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
/**
* Writes the characteristic value to the given characteristic.
*
* @param characteristic the characteristic to write to
* @return true if request has been sent
*/
protected final boolean writeCharacteristic(final BluetoothGattCharacteristic characteristic) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null || characteristic == null)
return false;
// Check characteristic property
final int properties = characteristic.getProperties();
if ((properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) == 0)
return false;
Logger.v(mLogSession, "Writing characteristic " + characteristic.getUuid() + " (" + getWriteType(characteristic.getWriteType()) + ")");
Logger.d(mLogSession, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")");
return gatt.writeCharacteristic(characteristic);
}
示例12: notifyDevices
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public void notifyDevices(BluetoothGattCharacteristic changedCharacteristic) {
if (!connectedDevices.isEmpty() && gattServer != null) {
boolean indicate = (changedCharacteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) == BluetoothGattCharacteristic.PROPERTY_INDICATE;
for (BluetoothDevice device : connectedDevices) {
gattServer.notifyCharacteristicChanged(device, changedCharacteristic, indicate);
}
}
}
示例13: start
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
protected void start(Connection connection, BluetoothGatt gatt) {
List<BluetoothGattService> services = gatt.getServices();
for (BluetoothGattService service : services) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
int props = characteristic.getProperties();
if ((props & BluetoothGattCharacteristic.PROPERTY_READ) != 0) {
queue.add(characteristic);
}
}
}
readNext(gatt);
}
示例14: writeCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
@Override
public final boolean writeCharacteristic(final BluetoothGattCharacteristic characteristic) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null || characteristic == null)
return false;
// Check characteristic property
final int properties = characteristic.getProperties();
if ((properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) == 0)
return false;
return gatt.writeCharacteristic(characteristic);
}
示例15: isCharacteristicReadable
import android.bluetooth.BluetoothGattCharacteristic; //導入方法依賴的package包/類
public static boolean isCharacteristicReadable(BluetoothGattCharacteristic c) {
return ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0);
}