本文整理汇总了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);
}