private void optional() { HeadsetPlugManager.getInstance().registerHeadsetPlugListener(this); mHeadsetListener = new HeadsetBroadcastReceiver(); registerReceiver(mHeadsetListener, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); mBluetoothHeadsetBroadcastListener = new BluetoothHeadsetBroadcastReceiver(); mBtAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBtAdapter != null && BluetoothProfile.STATE_CONNECTED == mBtAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)) { // on some devices, BT is not supported boolean bt = mBtAdapter.getProfileProxy(getBaseContext(), mBluetoothHeadsetListener, BluetoothProfile.HEADSET); int connection = mBtAdapter.getProfileConnectionState(BluetoothProfile.HEADSET); } IntentFilter i = new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); i.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); i.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED); registerReceiver(mBluetoothHeadsetBroadcastListener, i); //避免对window添加ui修改参数 // getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); // getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); }
@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); } }
public void stopBluetooth() { Log.w("[Bluetooth] Stopping..."); isBluetoothConnected = false; disableBluetoothSCO(); if (mBluetoothAdapter != null && mProfileListener != null && mBluetoothHeadset != null) { mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset); mProfileListener = null; } mBluetoothDevice = null; Log.w("[Bluetooth] Stopped!"); if (LinphoneManager.isInstanciated()) { LinphoneManager.getInstance().routeAudioToReceiver(); } }
@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"); } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { connected = true; gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { connected = false; btGatt.close(); btGatt = null; charSerial = null; messenger.obtainMessage(MessageConstants.STATUS, StatusCode.Disconnected).sendToTarget(); } else { messenger.obtainMessage(MessageConstants.ERROR, ErrorCode.Connect).sendToTarget(); taskConnectTimeout.cancel(); } }
/** * Stops and closes all components related to Bluetooth audio. */ public void stop() { ThreadUtils.checkIsOnMainThread(); Log.d(TAG, "stop: BT state=" + bluetoothState); if (bluetoothAdapter == null) { return; } // Stop BT SCO connection with remote device if needed. stopScoAudio(); // Close down remaining BT resources. if (bluetoothState == State.UNINITIALIZED) { return; } unregisterReceiver(bluetoothHeadsetReceiver); cancelTimer(); if (bluetoothHeadset != null) { bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); bluetoothHeadset = null; } bluetoothAdapter = null; bluetoothDevice = null; bluetoothState = State.UNINITIALIZED; Log.d(TAG, "stop done: BT state=" + bluetoothState); }
private void optionalDestroy() { if (mBtAdapter != null) { mBtAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothProfile); mBluetoothProfile = null; mBtAdapter = null; } if (mBluetoothHeadsetBroadcastListener != null) { unregisterReceiver(mBluetoothHeadsetBroadcastListener); mBluetoothHeadsetBroadcastListener = null; } if (mHeadsetListener != null) { unregisterReceiver(mHeadsetListener); mHeadsetListener = null; } HeadsetPlugManager.getInstance().unregisterHeadsetPlugListener(this); }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { if(mOnConnectListener!=null) mOnConnectListener.onConnect(gatt); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { if(mOnDisconnectListener!=null) mOnDisconnectListener.onDisconnect(gatt); Log.i(TAG, "Disconnected from GATT server."); } if(mOnConnectStatusChangedListener!=null) mOnConnectStatusChangedListener.onConnectStatusChanged(gatt,status,newState); Log.i(TAG, "Changed"); }
/** * Callback indicating when GATT client has connected/disconnected to/from a remote * GATT server. * * @param gatt GATT client * @param status Status of the connect or disconnect operation. * {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds. * @param newState Returns the new connection state. Can be one of * {@link android.bluetooth.BluetoothProfile#STATE_DISCONNECTED} or * {@link android.bluetooth.BluetoothProfile#STATE_CONNECTED} */ @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); // boolean indicating whether or not the next step is successful, default is false boolean success = false; // Start Service discovery if we're now connected if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED) { success = gatt.discoverServices(); } // else: not connected, continue } // else: not successful onStep(gatt, success); }
/** * Make sure Bluetooth and health profile are available on the Android device. Stop service * if they are not available. */ @Override public void onCreate() { super.onCreate(); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { // Bluetooth adapter isn't available. The client of the service is supposed to // verify that it is available and activate before invoking this service. stopSelf(); return; } if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener, BluetoothProfile.HEALTH)) { Toast.makeText(this, R.string.bluetooth_health_profile_not_available, Toast.LENGTH_LONG); stopSelf(); return; } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); broadcastUpdate(intentAction); } }
/** Stops and closes all components related to Bluetooth audio. */ public void stop() { ThreadUtils.checkIsOnMainThread(); Log.d(TAG, "stop: BT state=" + bluetoothState); if (bluetoothAdapter == null) { return; } // Stop BT SCO connection with remote device if needed. stopScoAudio(); // Close down remaining BT resources. if (bluetoothState == State.UNINITIALIZED) { return; } unregisterReceiver(bluetoothHeadsetReceiver); cancelTimer(); if (bluetoothHeadset != null) { bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); bluetoothHeadset = null; } bluetoothAdapter = null; bluetoothDevice = null; bluetoothState = State.UNINITIALIZED; Log.d(TAG, "stop done: BT state=" + bluetoothState); }
/** * Stops and closes all components related to Bluetooth audio. */ public void stop() { ThreadUtils.checkIsOnMainThread(); unregisterReceiver(bluetoothHeadsetReceiver); Log.d(TAG, "stop: BT state=" + bluetoothState); if (bluetoothAdapter != null) { // Stop BT SCO connection with remote device if needed. stopScoAudio(); // Close down remaining BT resources. if (bluetoothState != State.UNINITIALIZED) { cancelTimer(); if (bluetoothHeadset != null) { bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); bluetoothHeadset = null; } bluetoothAdapter = null; bluetoothDevice = null; bluetoothState = State.UNINITIALIZED; } } Log.d(TAG, "stop done: BT state=" + bluetoothState); }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); if( gatt.getDevice().getBondState() != BluetoothDevice.BOND_BONDED ) { broadcastUpdate(ACTION_NOT_PAIRED); gatt.disconnect(); return; } Log.i(TAG, "Attempting to start service discovery" ); mBluetoothGatt.discoverServices(); connected = true; broadcastUpdate(ACTION_GATT_CLIENT_CONNECTED); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_CLIENT_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); broadcastUpdate(intentAction); connected = false; } }
public void run() { /* In example we don't start discovery, but any case if it is discovering, stop it */ if(failedConnect != true) { /* Create profile listeners for A2DP and Headset profiles */ mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener, BluetoothProfile.A2DP); mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener, BluetoothProfile.HEADSET); /* Paired with Classic BT, update UI */ classic_paired = true; tryBLE = true; TextUpdateHandler.post(updateRunnable); read_ble_handler.postDelayed(runnable, 5000); } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { resetConnectionTimeoutChecker(); isSnachConnected = true; BLEManager.this.gatt = gatt; // TODO check if right device is discovered (instead of another BLE device with a different GATT..) Log.i("BLE", "Connected to GATT server."); Log.i("BLE", "Attempting to start service discovery:" + startServiceDiscovery(gatt)); sendBroadcastConnectionChanged(isSnachConnected); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { isSnachConnected = false; mConnectionListener.ConnectionLost(); Log.i("BLE", "Disconnected from GATT server."); } }
/** * 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. */ public void connect(final String address, IBleDeviceListener iBleDeviceListener) { UdooBluException udooBluException = checkBluetooth(getApplicationContext()); if (udooBluException != null) { if (iBleDeviceListener != null) iBleDeviceListener.onError(udooBluException); } else { final BluetoothDevice device = mBtAdapter.getRemoteDevice(address); int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT); BluetoothGatt bluetoothGatt = checkAndGetGattItem(address); if (connectionState == BluetoothProfile.STATE_DISCONNECTED) { // Previously connected device. Try to reconnect. if (bluetoothGatt != null) { Log.d(TAG, "Re-use GATT connection"); if (bluetoothGatt.connect()) { } else { Log.w(TAG, "GATT re-connect failed."); } } else if (device == null) { Log.w(TAG, "Device not found. Unable to connect."); } else { Log.d(TAG, "Create a new GATT connection."); bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallbackBuilder()); mBluetoothGatts.put(address, bluetoothGatt); } } else { Log.w(TAG, "Attempt to connect in state: " + connectionState); bond(address); bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallbackBuilder()); mBluetoothGatts.put(address, bluetoothGatt); } } }
/** * 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); } } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { Log.e(TAG, "onConnectionStateChanged"); String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { // STATE : connected intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction); // broadcast Gatt connection state : connected Log.e(TAG, "Connected to GATT server."); Log.e(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // STATE : disconnected intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; broadcastUpdate(intentAction); // broadcast Gatt connection state : disconnected Log.e(TAG, "Disconnected from GATT server."); } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { switch(newState) { case BluetoothProfile.STATE_CONNECTED: Log.d(TAG, "Gatt state: connected"); gatt.discoverServices(); isConnectedToGatt = true; raiseonConnect(); break; default: Log.d(TAG, "Gatt state: not connected"); isConnectedToGatt = false; raiseonDisconnect(); break; } }
public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(A2dpSinkHelper.ACTION_CONNECTION_STATE_CHANGED)) { int oldState = A2dpSinkHelper.getPreviousProfileState(intent); int newState = A2dpSinkHelper.getCurrentProfileState(intent); BluetoothDevice device = A2dpSinkHelper.getDevice(intent); Log.d(TAG, "Bluetooth A2DP sink changing connection state from " + oldState + " to " + newState + " device " + device); if (device != null) { String deviceName = Objects.toString(device.getName(), "a device"); if (newState == BluetoothProfile.STATE_CONNECTED) { speak("Connected to " + deviceName); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { speak("Disconnected from " + deviceName); } } } }
/** * Disconnects from the device and closes the Bluetooth GATT object afterwards. */ public void disconnectAndClose() { // This sometimes happen when called from UpdateService.ACTION_GATT_ERROR event receiver in UpdateFragment. if (mBluetoothGatt == null) return; setState(STATE_DISCONNECTING); mBluetoothGatt.disconnect(); // Sometimes the connection gets error 129 or 133. Calling disconnect() method does not really disconnect... sometimes the connection is already broken. // Here we have a security check that notifies UI about disconnection even if onConnectionStateChange(...) has not been called. mHandler.postDelayed(new Runnable() { @Override public void run() { if (mConnectionState == STATE_DISCONNECTING) mGattCallback.onConnectionStateChange(mBluetoothGatt, BluetoothGatt.GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED); } }, 1500); }
@Override public void onDeviceSelected(final BluetoothDevice device, final String name) { if (mConnectionProgressDialog != null) { mConnectionProgressDialog.setTitle(getString(R.string.prog_dialog_connect_title)); mConnectionProgressDialog.setMessage(getString(R.string.prog_dialog_connect_message)); mConnectionProgressDialog.show(); } final Activity activity = getActivity(); final Intent service = new Intent(activity, UpdateService.class); service.putExtra(UpdateService.EXTRA_DATA, device); updateUiForBeacons(BluetoothProfile.STATE_CONNECTED, UpdateService.LOCKED); activity.startService(service); mBounnd = true; activity.bindService(service, mServiceConnection, 0); }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { Log.i(TAG, "onConnectionStateChange,newState " + newState + "status:" + status); try{ mBleState = BleStatus.values()[newState]; }catch(Exception e){ e.printStackTrace(); } notifyListeners(); if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { // mBluetoothGatt = gatt; mBleState = BleStatus.BUILD_DISCOVER_SERVICE; notifyListeners(); gatt.discoverServices(); } else { Log.i(TAG, "onConnectionStateChange,failure"); if(mOnGattDisconnectListener != null){ mOnGattDisconnectListener.onGattDisconnectListener(gatt); } } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { Config.logi("BluetoothGatt connection State Changed: [" + status + "]" + newState); String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction, gatt.getDevice().getAddress()); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to startScan findService discovery:" + gatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; gatt.close();//make 133 not fire. Log.i(TAG, "Disconnected from GATT server."); broadcastUpdate(intentAction, gatt.getDevice().getAddress()); } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { switch (newState) { case BluetoothProfile.STATE_CONNECTED: LOGGER.info("GATT connected: " + gatt); gatt.discoverServices(); break; case BluetoothProfile.STATE_DISCONNECTED: LOGGER.info("GATT disconnected: " + gatt); MainActivity.withMainActivity((ma) -> { Toast.makeText(ma, "Connection to Polar H7 lost", Toast.LENGTH_SHORT).show(); }); mPolarH7BluetoothGatt = null; resetHeartRateInformation(); break; default: throw new AssertionError(); } }
@Override public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); Log.d(TAG, "onConnectionStateChange status="+status+" newState="+newState); if (newState == BluetoothProfile.STATE_CONNECTED) { handler.post(new Runnable() { @Override public void run() { gatt.discoverServices(); } }); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { //Util.sendBroadcast(getApplicationContext(), Constants.INTENT_MISCALE_DISCONNECTED); closeGatt(); } }
private void onNativeConnectFail(final BluetoothGatt gatt, final int gattStatus) { //--- DRK > NOTE: Making an assumption that the underlying stack agrees that the connection state is STATE_DISCONNECTED. //--- This is backed up by basic testing, but even if the underlying stack uses a different value, it can probably //--- be assumed that it will eventually go to STATE_DISCONNECTED, so SweetBlue library logic is sounder "living under the lie" for a bit regardless. m_device.m_nativeWrapper.updateNativeConnectionState(gatt, BluetoothProfile.STATE_DISCONNECTED); final P_Task_Connect connectTask = m_queue.getCurrent(P_Task_Connect.class, m_device); if (connectTask != null) { connectTask.onNativeFail(gattStatus); } else { m_device.onNativeConnectFail((PE_TaskState) null, gattStatus, AutoConnectUsage.UNKNOWN); } }
private void onNativeConnectFail(final BluetoothDevice nativeDevice, final int gattStatus) { //--- DRK > NOTE: Making an assumption that the underlying stack agrees that the connection state is STATE_DISCONNECTED. //--- This is backed up by basic testing, but even if the underlying stack uses a different value, it can probably //--- be assumed that it will eventually go to STATE_DISCONNECTED, so SweetBlue library logic is sounder "living under the lie" for a bit regardless. m_server.m_nativeWrapper.updateNativeConnectionState(nativeDevice.getAddress(), BluetoothProfile.STATE_DISCONNECTED); if( hasCurrentConnectTaskFor(nativeDevice) ) { final P_Task_ConnectServer connectTask = m_queue.getCurrent(P_Task_ConnectServer.class, m_server); connectTask.onNativeFail(gattStatus); } else { m_server.onNativeConnectFail(nativeDevice, BleServer.ConnectionFailListener.Status.NATIVE_CONNECTION_FAILED_EVENTUALLY, gattStatus); } }
public void disconnectDevice(String address) { BluetoothGatt bluetoothGatt = addressToGattClient.get(address); if (btAdapter == null || address == null || bluetoothGatt == null) { // Broadcast the disconnect so BleFlow doesn't hang waiting for it; something else // already disconnected us in this case. sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null); return; } BluetoothDevice device = btAdapter.getRemoteDevice(address); int bleState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT); if (bleState != BluetoothProfile.STATE_DISCONNECTED && bleState != BluetoothProfile.STATE_DISCONNECTING) { bluetoothGatt.disconnect(); } else { bluetoothGatt.close(); addressToGattClient.remove(address); sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null); } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if(gatt.getDevice().getAddress().equals(bleDeviceAddress)) { if (newState == BluetoothProfile.STATE_CONNECTED) { changeState(TappyBleState.CONNECTED); bluetoothGattRef.compareAndSet(null,gatt); BluetoothGatt bluetoothGatt = bluetoothGattRef.get(); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. if (bluetoothGatt != null) { Log.i(TAG, "Attempting to start service discovery:" + bluetoothGatt.discoverServices()); } else { Log.wtf(TAG, "Somehow connected with no gatt"); changeState(TappyBleState.ERROR); } } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { changeState(TappyBleState.DISCONNECTED); Log.i(TAG, "Disconnected from GATT server."); } } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { mGatt = gatt; switch(newState) { case BluetoothProfile.STATE_CONNECTED: Log.d(TAG, "Gatt state: connected"); gatt.discoverServices(); mDeviceConnected = true; if(mConnectionLatch != null) { mConnectionLatch.countDown(); } break; default: Log.d(TAG, "Gatt state: not connected"); mDeviceConnected = false; break; } }
@Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { mBluetoothGatt = gatt; mConnectionState = STATE_CONNECTED; ActiveBluetoothDevice.connected(); Log.i(TAG, "Connected to GATT server."); Log.i(TAG, "Connection state: Bonded - " + device.getBondState()); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { currentGattTask = GATT_SETUP; mBluetoothGatt.discoverServices(); } else { device.setPin("000000".getBytes()); device.createBond(); } } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { mConnectionState = STATE_DISCONNECTED; ActiveBluetoothDevice.disconnected(); Log.w(TAG, "Disconnected from GATT server."); } }