Example #1
0
 public boolean isConnected(BluetoothDevice device) {
   log("isConnetected");
   if (!isEnabled()) {
     log("error, the service has not been ready ");
   }
   return mManager.isConnected(device);
 }
Example #2
0
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          log("receive intent:" + action);
          if (action.equals(BluetoothMap.ACTION_AUTHORIZE_RESULT)) {
            boolean allow = intent.getBooleanExtra(BluetoothMap.EXTRA_RESULT, false);
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            authorizeResponse(device, allow);
          } else if (action.equals(BluetoothMap.ACTION_DISCONNECT_DEVICE)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            disconnect(device);
          } else if (action.equals(BluetoothDevice.ACTION_NAME_CHANGED)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int state = mNotification.getDeviceState(device);
            if (state == BluetoothMap.STATE_AUTHORIZING) {
              mNotification.createNotification(
                  BluetoothMapNotification.ALERT_TYPE_AUTHORIZE, device, false);
            } else if (state == BluetoothMap.STATE_CONNECTED) {
              mNotification.createNotification(
                  BluetoothMapNotification.ALERT_TYPE_CONNECT, device, false);
            }
          } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
            int state =
                intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (state == BluetoothDevice.BOND_NONE && mManager.isConnected(device)) {
              disconnect(device);
            }
          } else if (action.equals(BluetoothMapNotification.ACTION_MAP_CLEAR_NOTIFICATION)) {
            clearAuthorizingDevices();
          } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            // when proxy binds to the service using BIND_AUTO_CREATE, the onDestroy will not be
            // called
            int state = BluetoothAdapter.getDefaultAdapter().getState();
            if (state == BluetoothAdapter.STATE_TURNING_OFF) {
              clearService();
            } else if (state == BluetoothAdapter.STATE_ON) {
              initService();
            }
          }
        }