本文整理匯總了Java中android.bluetooth.BluetoothGatt類的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothGatt類的具體用法?Java BluetoothGatt怎麽用?Java BluetoothGatt使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BluetoothGatt類屬於android.bluetooth包,在下文中一共展示了BluetoothGatt類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: start
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
protected void start(Connection connection, BluetoothGatt gatt) {
BluetoothGattService service = gatt.getService(serviceUUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
if (characteristic != null) {
NeatleLogger.d("Reading characteristics " + characteristicUUID);
if (gatt.readCharacteristic(characteristic)) {
return;
}
NeatleLogger.d("Read failed" + characteristicUUID);
} else {
NeatleLogger.e("Could not find characteristics " + characteristicUUID);
}
} else {
NeatleLogger.e("Could not find service " + serviceUUID);
}
finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
}
示例2: onConnectionStateChange
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_CONNECTED;
connectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" + bluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_DISCONNECTED;
connectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
示例3: onConnectionStateChange
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (this.mGatt != gatt) {
this.mGatt = gatt;
}
if (newState == 2) {
QNLog.log("連接設備成功");
this.qnDecoder = null;
this.mGatt.discoverServices();
} else if (newState == 0) {
this.mGatt.close();
this.uiHandler.post(new 1 (this));
this.qnDecoder = null;
} else {
gatt.disconnect();
QNLog.error("連接狀態異常:", Integer.valueOf(status));
this.uiHandler.post(new 2 (this));
if (this.bleCallback != null) {
this.bleCallback.onCompete(5);
}
}
}
示例4: onCharacteristicChanged
import android.bluetooth.BluetoothGatt; //導入依賴的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);
}
示例5: onCharacteristicWrite
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (writeCallback != null) {
if (writeQueue.size() > 0){
byte[] data = writeQueue.get(0);
writeQueue.remove(0);
doWrite(characteristic, data);
} else {
if (status == BluetoothGatt.GATT_SUCCESS) {
writeCallback.invoke();
} else {
Log.e(LOG_TAG, "Error onCharacteristicWrite:" + status);
writeCallback.invoke("Error writing status: " + status);
}
writeCallback = null;
}
}else
Log.e(LOG_TAG, "No callback on write");
}
示例6: onConnectionStateChange
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.d("BLUETOOTH", "Estado de conexión bluetooth: " + (newState == BluetoothProfile.STATE_CONNECTED ? "Connected" : "Disconnected"));
if(newState == BluetoothProfile.STATE_CONNECTED){
//setState(State.CONNECTED);
mBluetoothGatt.discoverServices();
}
else{
//setState(State.IDDLE);
//TODO Realizar todas las tareas necesarias cuando se desconecte la pulsera
Log.d("BLUETOOTH", "Se ha desconectado el dispostivo bluetooth");
}
}
示例7: obtenerCaracteristicasDescriptoresAccelGyroCC2650
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
private boolean obtenerCaracteristicasDescriptoresAccelGyroCC2650(BluetoothGatt gatt){
boolean todoCorrecto = false;
BluetoothGattService acelerometroService = gatt.getService(UUID_MOVEMENT_SERVICE);
if(acelerometroService != null){
this.movementCharacteristic = acelerometroService.getCharacteristic(UUID_MOVEMENT_DATA);
this.movementConf = acelerometroService.getCharacteristic(UUID_MOVEMENT_CONF);
this.movementPeriod = acelerometroService.getCharacteristic(UUID_MOVEMENT_PERIOD);
if(movementCharacteristic != null && movementConf != null){
this.config = movementCharacteristic.getDescriptor(UUID_CCC);
todoCorrecto = true;
}
}
return todoCorrecto;
}
示例8: refreshDeviceCache
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
/**
* Clears the device cache. After uploading new hello4 the DFU target will have other services than before.
* 清除設備緩存。 在上傳新的hello4之後,DFU目標將具有比以前更多的服務。
*/
public boolean refreshDeviceCache() {
/*
* There is a refresh() method in BluetoothGatt class but for now it's hidden. We will call it using reflections.
* 使用反射調用
*/
try {
final Method refresh = BluetoothGatt.class.getMethod("refresh");
if (refresh != null) {
final boolean success = (Boolean) refresh.invoke(getBluetoothGatt());
Log.i(TAG, "Refreshing result: " + success);
return success;
}
} catch (Exception e) {
Log.e(TAG, "An exception occured while refreshing device", e);
}
return false;
}
示例9: readCharacteristic
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
/**
* Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported asynchronously through the
* {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)} callback.
*
* @param characteristic The characteristic to read from.
*/
public void readCharacteristic(final String mac, final BluetoothGattCharacteristic characteristic) {
try {
voidBlockingQueue.put(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
BluetoothGatt bluetoothGatt = checkAndGetGattItem(mac);
boolean success = false;
if (bluetoothGatt != null && characteristic != null) {
success = bluetoothGatt.readCharacteristic(characteristic);
}
return success;
}
});
} catch (InterruptedException e) {
if (BuildConfig.DEBUG)
Log.e(TAG, "readCharacteristic: " + e.getMessage());
}
}
示例10: onServicesDiscovered
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
boolean connected = false;
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_COUNTER_UUID);
if (characteristic != null) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_CONFIG);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
connected = gatt.writeDescriptor(descriptor);
}
}
}
mListener.onConnected(connected);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
示例11: handleCharacteristicWriteCallback
import android.bluetooth.BluetoothGatt; //導入依賴的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));
}
}
});
}
}
示例12: disconnect
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
/**
* Disconnects an existing connection or cancel a pending connection. The disconnection result is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} callback.
*/
public void disconnect(String address) {
if (mBtAdapter == null) {
Log.w(TAG, "disconnect: BluetoothAdapter not initialized");
return;
}
final BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
BluetoothGatt bluetoothGatt = checkAndGetGattItem(address);
if (bluetoothGatt != null) {
Log.i(TAG, "disconnect");
if (connectionState != BluetoothProfile.STATE_DISCONNECTED) {
bluetoothGatt.disconnect();
} else {
Log.w(TAG, "Attempt to disconnect in state: " + connectionState);
}
}
}
示例13: onCharacteristicChanged
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
public final void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
final String data = ParserUtils.parse(characteristic);
if (isBatteryLevelCharacteristic(characteristic)) {
Logger.i(mLogSession, "Notification received from " + characteristic.getUuid() + ", value: " + data);
final int batteryValue = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
Logger.a(mLogSession, "Battery level received: " + batteryValue + "%");
mCallbacks.onBatteryValueReceived(batteryValue);
} else {
final BluetoothGattDescriptor cccd = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
final boolean notifications = cccd == null || cccd.getValue() == null || cccd.getValue().length != 2 || cccd.getValue()[0] == 0x01;
if (notifications) {
Logger.i(mLogSession, "Notification received from " + characteristic.getUuid() + ", value: " + data);
onCharacteristicNotified(gatt, characteristic);
} else { // indications
Logger.i(mLogSession, "Indication received from " + characteristic.getUuid() + ", value: " + data);
onCharacteristicIndicated(gatt, characteristic);
}
}
}
示例14: onCharacteristicNotified
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
protected void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
// TODO this method is called when a notification has been received
// This method may be removed from this class if not required
if (mLogSession != null)
Logger.a(mLogSession, TemplateParser.parse(characteristic));
int value;
final int flags = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
if ((flags & 0x01) > 0) {
value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 1);
} else {
value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
}
//This will send callback to the Activity when new value is received from HR device
mCallbacks.onSampleValueReceived(value);
}
示例15: onServicesDiscovered
import android.bluetooth.BluetoothGatt; //導入依賴的package包/類
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
Logger.i("onServicesDiscovered status=" + GattError.parseConnectionError(status));
if (status == BluetoothGatt.GATT_SUCCESS) {
//start subscribe data
runOnUiThread(new Runnable() {
@Override
public void run() {
onDiscoverServicesSuccess(gatt);
if (gatt != null){
startSubscribe(gatt);
}
}
});
}else {
Logger.e("onServicesDiscovered fail!");
runOnUiThread(new Runnable() {
@Override
public void run() {
onDiscoverServicesFail(gatt);
}
});
}
if (getBluetoothGattCallback() != null) getBluetoothGattCallback().onServicesDiscovered(gatt, status);
}