java类android.bluetooth.BluetoothProfile的实例源码

AgoraActivity.java 文件源码 项目:lrs_android 阅读 31 收藏 0 点赞 0 评论 0
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);
    }
BluetoothLeService.java 文件源码 项目:Make-A-Pede-Android-App 阅读 40 收藏 0 点赞 0 评论 0
@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);
    }
}
BluetoothManager.java 文件源码 项目:Linphone4Android 阅读 32 收藏 0 点赞 0 评论 0
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();
    }
}
BluetoothLeService.java 文件源码 项目:TrainAppTFG 阅读 26 收藏 0 点赞 0 评论 0
@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");
    }
}
ArduinoCommBle.java 文件源码 项目:arduator 阅读 26 收藏 0 点赞 0 评论 0
@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();
    }
}
BluetoothManager.java 文件源码 项目:nc-android-webrtcpeer 阅读 41 收藏 0 点赞 0 评论 0
/**
 * 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);
}
AgoraActivity.java 文件源码 项目:lrs_android 阅读 27 收藏 0 点赞 0 评论 0
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);
}
BluetoothLeClass.java 文件源码 项目:BLE-PEPS 阅读 33 收藏 0 点赞 0 评论 0
@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");

}
BluetoothLeClass.java 文件源码 项目:BLE-PEPS 阅读 39 收藏 0 点赞 0 评论 0
@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");

}
MainActivity.java 文件源码 项目:sdc-1-quickstart-android 阅读 31 收藏 0 点赞 0 评论 0
/**
 * 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);
}
BluetoothHDPService.java 文件源码 项目:buildAPKsSamples 阅读 31 收藏 0 点赞 0 评论 0
/**
 * 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;
    }
}
BluetoothLeService.java 文件源码 项目:igrow-android 阅读 46 收藏 0 点赞 0 评论 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;
        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);
    }
}
AppRTCBluetoothManager.java 文件源码 项目:AppRTC-Android 阅读 39 收藏 0 点赞 0 评论 0
/** 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);
}
AppRTCBluetoothManager.java 文件源码 项目:AndroidRTC 阅读 37 收藏 0 点赞 0 评论 0
/**
 * 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);
}
BluetoothClientService.java 文件源码 项目:bluewatcher 阅读 29 收藏 0 点赞 0 评论 0
@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;
    }
}
MainActivity.java 文件源码 项目:PairingExample 阅读 37 收藏 0 点赞 0 评论 0
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);
    }
}
BluetoothLeService.java 文件源码 项目:Android-BLE-to-Arduino 阅读 34 收藏 0 点赞 0 评论 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;
        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);
    }
}
BLEManager.java 文件源码 项目:Snach-Android 阅读 34 收藏 0 点赞 0 评论 0
@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.");
    }
}
UdooBluService.java 文件源码 项目:UDOOBluLib-android 阅读 40 收藏 0 点赞 0 评论 0
/**
     * 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);
            }
        }
    }
UdooBluService.java 文件源码 项目:UDOOBluLib-android 阅读 32 收藏 0 点赞 0 评论 0
/**
 * 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);
        }
    }
}
BluetoothLeService.java 文件源码 项目:MagicLight-Controller 阅读 52 收藏 0 点赞 0 评论 0
@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.");
    }
}
BLEMiBand2Helper.java 文件源码 项目:mi-band-2 阅读 36 收藏 0 点赞 0 评论 0
@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;
    }
}
BluetoothHDPService.java 文件源码 项目:AndroidthingsStudy 阅读 39 收藏 0 点赞 0 评论 0
/**
 * 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;
    }
}
A2dpSinkActivity.java 文件源码 项目:sample-bluetooth-audio 阅读 26 收藏 0 点赞 0 评论 0
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);
            }
        }
    }
}
UpdateService.java 文件源码 项目:Android-nRF-Beacon-for-Eddystone 阅读 29 收藏 0 点赞 0 评论 0
/**
 * 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);
}
UpdateFragment.java 文件源码 项目:Android-nRF-Beacon-for-Eddystone 阅读 31 收藏 0 点赞 0 评论 0
@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);
}
BLESensorListener.java 文件源码 项目:ssj 阅读 29 收藏 0 点赞 0 评论 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.");

    }
}
ANCSGattCallback.java 文件源码 项目:BLEServerSimple 阅读 28 收藏 0 点赞 0 评论 0
@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);
            }

        }
    }
BluetoothLeService.java 文件源码 项目:Grandroid2 阅读 38 收藏 0 点赞 0 评论 0
@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());
    }
}
XiotBluetoothLeManager.java 文件源码 项目:android-xmpp-iot-demo 阅读 30 收藏 0 点赞 0 评论 0
@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();
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号