List of usage examples for android.bluetooth BluetoothDevice connectGatt
public BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback)
From source file:com.megster.cordova.rfduino.RFduinoPlugin.java
private void connect(CallbackContext callbackContext, String uuid) { Peripheral peripheral = peripherals.get(uuid); // note uuid is mac address on android BluetoothDevice device = peripheral.getDevice(); peripheral.setConnectCallback(callbackContext); gatt = device.connectGatt(cordova.getActivity(), false, peripheral); activePeripheral = peripheral;//from w w w . j a v a 2s . c om PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); result.setKeepCallback(true); callbackContext.sendPluginResult(result); }
From source file:com.artur.softwareproject.BluetoothService.java
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "Bluetoothservice was started."); this.serviceIntent = intent; if (intent != null) { BluetoothDevice bDevice; bDevice = (BluetoothDevice) intent.getExtras().get("device"); if (bDevice != null) { mBluetoothGatt = bDevice.connectGatt(this, false, mGattCallback); }/* w w w . ja v a 2 s .c om*/ } return super.onStartCommand(intent, flags, startId); }
From source file:net.emilymaier.movebot.HeartFragment.java
@Override @SuppressWarnings("deprecation") public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { Log.d("HeartFragment", "Bluetooth LE device found"); BluetoothGatt bluetoothGatt = device.connectGatt(act, false, new BluetoothGattCallback() { @Override/*from w ww. j a va 2 s .c o m*/ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.d("HeartFragment", "Connected to LE device"); gatt.discoverServices(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattCharacteristic characteristic = null; for (BluetoothGattService service : gatt.getServices()) { characteristic = service .getCharacteristic(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb")); if (characteristic != null) { break; } } if (characteristic != null) { Log.d("HeartFragment", "Found device with HRM characteristic"); HeartDevice device = new HeartDevice(); device.bluetoothGatt = gatt; device.characteristic = characteristic; for (HeartDevice testDevice : heartDevices) { if (testDevice.bluetoothGatt.getDevice().getAddress() .equals(device.bluetoothGatt.getDevice().getAddress())) { heartDevices.remove(testDevice); } } heartDevices.add(device); } else { Log.d("HeartFragment", "Device does not have HRM characteristic"); gatt.disconnect(); gatt.close(); } } else { Log.w("HeartFragment", "Failed to discover device services"); gatt.disconnect(); gatt.close(); } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; } final int heartRate = characteristic.getIntValue(format, 1); act.runOnUiThread(new Runnable() { @Override public void run() { act.updateHeart(heartRate); } }); } }); }
From source file:ble.AndroidBle.java
@Override public boolean connect(String address) { BluetoothDevice device = mBtAdapter.getRemoteDevice(address); BluetoothGatt gatt = device.connectGatt(mService, false, mGattCallback); if (gatt == null) { mBluetoothGatts.remove(address); return false; } else {// w ww . j a v a 2 s.co m // TODO: if state is 141, it can be connected again after about 15 // seconds mBluetoothGatts.put(address, gatt); return true; } }
From source file:iha_au.ppmonitor.Services.BleService.java
/** * Opretter forbindelse til HW-bluetooth modulets GATT server. * @param device Det fundne device fra LeScanCallBack. *//*from w w w .ja v a 2 s.co m*/ private void connecToGatt(BluetoothDevice device) { Log.v("DEBUG", "Get device name: " + device.getName()); bGatt = device.connectGatt(getApplicationContext(), false, gattCallBack); }
From source file:com.wolkabout.hexiwear.service.FirmwareUpdateService.java
public void updateFirmware(final BluetoothDevice device, final Image image) { notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_bluetooth_connected_white_48dp) .setContentTitle(getResources().getString(R.string.app_name)).setLights(Color.GREEN, 100, 5000); startForeground(NOTIFICATION_ID, notificationBuilder.build()); bluetoothGatt = device.connectGatt(this, true, new FirmwareUpdater(image)); }
From source file:com.megster.cordova.ble.central.Peripheral.java
public void connect(CallbackContext callbackContext, Activity activity) { BluetoothDevice device = getDevice(); connectCallback = callbackContext;/*from ww w .ja v a 2 s . c o m*/ gatt = device.connectGatt(activity, false, this); PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); result.setKeepCallback(true); callbackContext.sendPluginResult(result); }
From source file:com.vehicle.uart.UartService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback./* www. ja va 2 s. c om*/ */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { EVLog.e("BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { EVLog.e("Trying to use an existing mBluetoothGatt for connection."); if (mBluetoothGatt.connect()) { mConnectionState = STATE_CONNECTING; return true; } else { return false; } } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { EVLog.e("Device not found. Unable to connect."); return false; } // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); EVLog.e("Trying to create a new connection."); mBluetoothDeviceAddress = address; mConnectionState = STATE_CONNECTING; return true; }
From source file:com.wolkabout.hexiwear.service.BluetoothService.java
private void createGATT(final BluetoothDevice device) { bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() { @Override/*from w w w. jav a 2 s .c o m*/ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { isConnected = BluetoothProfile.STATE_CONNECTED == newState; if (isConnected) { Log.i(TAG, "GATT connected."); startForeground(442, getNotification(device)); gatt.discoverServices(); } else { Log.i(TAG, "GATT disconnected."); NotificationService_.intent(BluetoothService.this).stop(); notificationManager.notify(442, getNotification(device)); gatt.connect(); } final Intent connectionStateChanged = new Intent(CONNECTION_STATE_CHANGED); connectionStateChanged.putExtra(CONNECTION_STATE, isConnected); sendBroadcast(connectionStateChanged); } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { Log.i(TAG, "Services discovered."); if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) { handleAuthenticationError(gatt); return; } discoverCharacteristics(gatt); } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.i(TAG, "Characteristic written: " + status); if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) { handleAuthenticationError(gatt); return; } final byte command = characteristic.getValue()[0]; switch (command) { case WRITE_TIME: Log.i(TAG, "Time written."); final BluetoothGattCharacteristic batteryCharacteristic = readableCharacteristics .get(Characteristic.BATTERY.getUuid()); gatt.setCharacteristicNotification(batteryCharacteristic, true); for (BluetoothGattDescriptor descriptor : batteryCharacteristic.getDescriptors()) { if (descriptor.getUuid().toString().startsWith("00002904")) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); gatt.writeDescriptor(descriptor); } } break; case WRITE_NOTIFICATION: Log.i(TAG, "Notification sent."); if (notificationsQueue.isEmpty()) { Log.i(TAG, "Reading characteristics..."); readNextCharacteristics(gatt); } else { Log.i(TAG, "writing next notification..."); alertIn.setValue(notificationsQueue.poll()); gatt.writeCharacteristic(alertIn); } break; default: Log.w(TAG, "No such ALERT IN command: " + command); break; } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { readCharacteristic(gatt, Characteristic.MANUFACTURER); } @Override public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic gattCharacteristic, int status) { if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) { handleAuthenticationError(gatt); return; } final String characteristicUuid = gattCharacteristic.getUuid().toString(); final Characteristic characteristic = Characteristic.byUuid(characteristicUuid); switch (characteristic) { case MANUFACTURER: manufacturerInfo.manufacturer = gattCharacteristic.getStringValue(0); readCharacteristic(gatt, Characteristic.FW_REVISION); break; case FW_REVISION: manufacturerInfo.firmwareRevision = gattCharacteristic.getStringValue(0); readCharacteristic(gatt, Characteristic.MODE); break; default: Log.v(TAG, "Characteristic read: " + characteristic.name()); if (characteristic == Characteristic.MODE) { final Mode newMode = Mode.bySymbol(gattCharacteristic.getValue()[0]); if (mode != newMode) { onModeChanged(newMode); } } else { onBluetoothDataReceived(characteristic, gattCharacteristic.getValue()); } if (notificationsQueue.isEmpty()) { readNextCharacteristics(gatt); } else { alertIn.setValue(notificationsQueue.poll()); gatt.writeCharacteristic(alertIn); } break; } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic gattCharacteristic) { final String characteristicUuid = gattCharacteristic.getUuid().toString(); final Characteristic characteristic = Characteristic.byUuid(characteristicUuid); Log.d(TAG, "Characteristic changed: " + characteristic); if (characteristic == Characteristic.BATTERY) { onBluetoothDataReceived(Characteristic.BATTERY, gattCharacteristic.getValue()); } } }); }
From source file:com.example.bluetooth.RFduinoService.java
/** * Connects to the GATT server hosted on the Bluetooth LE device. * * @param address The device address of the destination device. * * @return Return true if the connection is initiated successfully. The connection result * is reported asynchronously through the * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)} * callback./*from w w w .java 2 s. c o m*/ */ public boolean connect(final String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } // Previously connected device. Try to reconnect. if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) { Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection."); return mBluetoothGatt.connect(); } final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. mBluetoothGatt = device.connectGatt(this, false, mGattCallback); Log.d(TAG, "Trying to create a new connection."); mBluetoothDeviceAddress = address; return true; }