本文整理匯總了Java中android.bluetooth.BluetoothGattCharacteristic類的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothGattCharacteristic類的具體用法?Java BluetoothGattCharacteristic怎麽用?Java BluetoothGattCharacteristic使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BluetoothGattCharacteristic類屬於android.bluetooth包,在下文中一共展示了BluetoothGattCharacteristic類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
/**
* Sends the read request to the given characteristic.
*
* @param characteristic the characteristic to read
* @return true if request has been sent
*/
protected final boolean readCharacteristic(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_READ) == 0)
return false;
Logger.v(mLogSession, "Reading characteristic " + characteristic.getUuid());
Logger.d(mLogSession, "gatt.readCharacteristic(" + characteristic.getUuid() + ")");
return gatt.readCharacteristic(characteristic);
}
示例2: readData
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
public void readData(UUID service, UUID Characteristics) {
if (!isConnectedToGatt || myGatBand == null) {
Log.d(TAG, "Cant read from BLE, not initialized.");
return;
}
Log.d(TAG, "* Getting gatt service, UUID:" + service.toString());
BluetoothGattService myGatService =
myGatBand.getService(service /*Consts.UUID_SERVICE_GENERIC*/);
if (myGatService != null) {
Log.d(TAG, "* Getting gatt Characteristic. UUID: " + Characteristics.toString());
BluetoothGattCharacteristic myGatChar
= myGatService.getCharacteristic(Characteristics /*Consts.UUID_CHARACTERISTIC_DEVICE_NAME*/);
if (myGatChar != null) {
Log.d(TAG, "* Reading data");
boolean status = myGatBand.readCharacteristic(myGatChar);
Log.d(TAG, "* Read status :" + status);
}
}
}
示例3: setCharacteristicNotification
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
private void setCharacteristicNotification(
@NonNull BluetoothGatt bluetoothgatt,
@NonNull BluetoothGattCharacteristic bluetoothgattcharacteristic,
boolean flag
) {
bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, flag);
if (FIND_ME_CHARACTERISTIC.equals(bluetoothgattcharacteristic.getUuid())) {
BluetoothGattDescriptor descriptor = bluetoothgattcharacteristic.getDescriptor(
CLIENT_CHARACTERISTIC_CONFIG
);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothgatt.writeDescriptor(descriptor);
}
}
}
示例4: 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;
}
示例5: writeData
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
public void writeData(UUID service, UUID Characteristics,byte[] data) {
if (!isConnectedToGatt || myGatBand == null) {
Log.d(TAG, "Cant read from BLE, not initialized.");
return;
}
Log.d(TAG, "* Getting gatt service, UUID:" + service.toString());
BluetoothGattService myGatService =
myGatBand.getService(service /*Consts.UUID_SERVICE_HEARTBEAT*/);
if (myGatService != null) {
Log.d(TAG, "* Getting gatt Characteristic. UUID: " + Characteristics.toString());
BluetoothGattCharacteristic myGatChar
= myGatService.getCharacteristic(Characteristics /*Consts.UUID_START_HEARTRATE_CONTROL_POINT*/);
if (myGatChar != null) {
Log.d(TAG, "* Writing trigger");
myGatChar.setValue(data /*Consts.BYTE_NEW_HEART_RATE_SCAN*/);
boolean status = myGatBand.writeCharacteristic(myGatChar);
Log.d(TAG, "* Writting trigger status :" + status);
}
}
}
示例6: enableIndications
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
@Override
public 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;
gatt.setCharacteristicNotification(characteristic, true);
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
return gatt.writeDescriptor(descriptor);
}
return false;
}
示例7: onCharacteristicWrite
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
/**
* 收到BLE終端寫入數據回調
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
final BluetoothGattCharacteristic characteristic, int status) {
Log.e(TAG,"onCharWrite "+gatt.getDevice().getName()
+" write "
+characteristic.getUuid().toString()
+" -> "
+new String(characteristic.getValue()));
runOnUiThread(new Runnable() {
@Override
public void run() {
int t = test[0];
Integer.toString(t);
ResponseContent.setText(Integer.toString(t));
}
});
}
示例8: onCharacteristicReadRequest
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
@Override
public void onCharacteristicReadRequest(final BluetoothDevice device, final int requestId, final int offset, final BluetoothGattCharacteristic characteristic) {
Logger.d(mLogSession, "[Server callback] Read request for characteristic " + characteristic.getUuid() + " (requestId=" + requestId + ", offset=" + offset + ")");
Logger.i(mLogSession, "[Server] READ request for characteristic " + characteristic.getUuid() + " received");
byte[] value = characteristic.getValue();
if (value != null && offset > 0) {
byte[] offsetValue = new byte[value.length - offset];
System.arraycopy(value, offset, offsetValue, 0, offsetValue.length);
value = offsetValue;
}
if (value != null)
Logger.d(mLogSession, "server.sendResponse(GATT_SUCCESS, value=" + ParserUtils.parse(value) + ")");
else
Logger.d(mLogSession, "server.sendResponse(GATT_SUCCESS, value=null)");
mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
Logger.v(mLogSession, "[Server] Response sent");
}
示例9: setOpCode
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
/**
* Writes given operation parameters to the characteristic
*
* @param characteristic
* the characteristic to write. This must be the Record Access Control Point characteristic
* @param opCode
* the operation code
* @param operator
* the operator (see {@link #OPERATOR_NULL} and others
* @param params
* optional parameters (one for >=, <=, two for the range, none for other operators)
*/
private void setOpCode(final BluetoothGattCharacteristic characteristic, final int opCode, final int operator, final Integer... params) {
final int size = 2 + ((params.length > 0) ? 1 : 0) + params.length * 2; // 1 byte for opCode, 1 for operator, 1 for filter type (if parameters exists) and 2 for each parameter
characteristic.setValue(new byte[size]);
// write the operation code
int offset = 0;
characteristic.setValue(opCode, BluetoothGattCharacteristic.FORMAT_UINT8, offset);
offset += 1;
// write the operator. This is always present but may be equal to OPERATOR_NULL
characteristic.setValue(operator, BluetoothGattCharacteristic.FORMAT_UINT8, offset);
offset += 1;
// if parameters exists, append them. Parameters should be sorted from minimum to maximum. Currently only one or two params are allowed
if (params.length > 0) {
// our implementation use only sequence number as a filer type
characteristic.setValue(FILTER_TYPE_SEQUENCE_NUMBER, BluetoothGattCharacteristic.FORMAT_UINT8, offset);
offset += 1;
for (final Integer i : params) {
characteristic.setValue(i, BluetoothGattCharacteristic.FORMAT_UINT16, offset);
offset += 2;
}
}
}
示例10: 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);
}
示例11: onCharacteristicChanged
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
/**
* 相當於一個監聽器, 當藍牙設備有數據返回時執行
*
* @param gatt
* @param characteristic
*/
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.e(TAG, "onCharacteristicChanged 返回數據: " + Arrays.toString(characteristic.getValue()));
Message message = new Message();
message.what = Constant.RESULT_DATA;
message.obj = characteristic.getValue();
handler.sendMessage(message);
//回調
onCharacteristicRefresh(gatt, characteristic);
}
示例12: writeCharacteristic
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
private void writeCharacteristic(String serviceGuid, String characteristic, int value, int type) {
if(!isConnected()) {
logi("writeCharacteristic() :: Not connected. Returning");
return;
}
BluetoothGattService s = getService(UUID.fromString(serviceGuid));
if(s == null) {
logi("writeCharacteristic() :: Service not found");
return;
}
BluetoothGattCharacteristic c = s.getCharacteristic(UUID.fromString(characteristic));
if(c == null) {
logi("writeCharacteristic() :: characteristic not found");
return;
}
c.setValue(value, type, 0);
int ret = writeCharacteristic(c);
logi("writeCharacteristic() :: returns - " + ret);
}
示例13: onCharacteristicReadRequest
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
@Override
public void onCharacteristicReadRequest(
BluetoothDevice device,
int requestId,
int offset,
BluetoothGattCharacteristic chr) {
int status = BluetoothGatt.GATT_FAILURE;
byte[] bytes = null;
if (offset != 0) {
status = BluetoothGatt.GATT_INVALID_OFFSET;
} else if (chr.equals(U2FGattService.U2F_CONTROL_POINT_LENGTH)) {
status = BluetoothGatt.GATT_SUCCESS;
bytes = new byte[] { 0x02, 0x00 }; /* Length == 512, see U2F BT 6.1 */
} else if (chr.equals(U2FGattService.U2F_SERVICE_REVISION_BITFIELD)) {
status = BluetoothGatt.GATT_SUCCESS;
bytes = new byte[] { 0x40 }; /* Version == 1.2, see U2F BT 6.1 */
}
Log.d(getClass().getCanonicalName(), Integer.valueOf(bytes.length).toString());
mGattServer.sendResponse(device, requestId, status, 0, bytes);
}
示例14: onCharacteristicWriteRequest
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
BluetoothGattCharacteristic characteristic, boolean preparedWrite,
boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
if (mProvClientDev.getPeerDevice().equals(device)) {
if (characteristic.getUuid().compareTo(UUID.fromString(UUID_PB_CHAR_DATA_IN)) == 0)
{
String strAddr=device.getAddress();
byte[] addr = stringToAddress(strAddr);
provServerPduInNative(addr, value);
}
}
}
示例15: handleCharacteristicWriteCallback
import android.bluetooth.BluetoothGattCharacteristic; //導入依賴的package包/類
/**
* 處理向特征碼寫入數據的回調
* @param bleCallback
*/
private void handleCharacteristicWriteCallback(final BleCharactCallback bleCallback) {
if (bleCallback != null) {
// 添加連接回調到LiteBluetooth的回調集合中
listenAndTimer(bleCallback, MSG_WRIATE_CHA, new BluetoothGattCallback() {
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
handler.removeMessages(MSG_WRIATE_CHA, this);
if (status == BluetoothGatt.GATT_SUCCESS) {
bleCallback.onSuccess(characteristic);
} else {
bleCallback.onFailure(new GattException(status));
}
}
});
}
}