Exemplo n.º 1
0
  public void createServer() {
    server = bluetoothManager.openGattServer(activity, new Callback());

    service =
        new BluetoothGattService(
            UUID.fromString(B4UUID), BluetoothGattService.SERVICE_TYPE_SECONDARY);
    server.addService(service);
    System.out.println("server: " + server);
  }
Exemplo n.º 2
0
  /**
   * startAdvertising will open a Gatt server, add our chat services to it and start advertisement
   * with default settings. Advertising won't start if bluetooth is disabled or device doesn't
   * support the Peripheral mode.
   */
  public void startAdvertising() {
    if (mBluetoothManager.getAdapter().isEnabled()) {
      if (mBluetoothManager.getAdapter().isMultipleAdvertisementSupported()) {
        mGattserver =
            mBluetoothManager.openGattServer(
                mContext,
                new BluetoothGattServerCallback() {
                  @Override
                  public void onConnectionStateChange(
                      BluetoothDevice device, int status, int newState) {
                    super.onConnectionStateChange(device, status, newState);
                    if (newState == BluetoothProfile.STATE_CONNECTED) {
                      if (mLogger != null) {
                        mLogger.log("Client connected: " + device.getAddress());
                      }
                      mConnectedDevice = device;
                    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                      if (mLogger != null) {
                        mLogger.log("Client disconnected: " + device.getAddress());
                      }
                      mConnectedDevice = null;
                    }
                  }

                  @Override
                  public void onCharacteristicReadRequest(
                      BluetoothDevice device,
                      int requestId,
                      int offset,
                      BluetoothGattCharacteristic characteristic) {
                    super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
                    if (mLogger != null) {
                      mLogger.log("onCharacteristicReadRequest");
                    }
                  }

                  @Override
                  public void onServiceAdded(int status, BluetoothGattService service) {
                    super.onServiceAdded(status, service);
                    BleAdvertiser.this.onServiceAdded();
                  }

                  @Override
                  public void onNotificationSent(BluetoothDevice device, int status) {
                    super.onNotificationSent(device, status);
                    if (mLogger != null) {
                      mLogger.log("onNotificationSent");
                    }
                  }

                  @Override
                  public void onCharacteristicWriteRequest(
                      BluetoothDevice device,
                      int requestId,
                      BluetoothGattCharacteristic characteristic,
                      boolean preparedWrite,
                      boolean responseNeeded,
                      int offset,
                      byte[] value) {
                    super.onCharacteristicWriteRequest(
                        device,
                        requestId,
                        characteristic,
                        preparedWrite,
                        responseNeeded,
                        offset,
                        value);
                    if (characteristic
                        .getUuid()
                        .equals(UUID.fromString(Constants.CHAT_CHARACTERISTIC_UUID))) {
                      String msg = "";
                      if (value != null) {
                        msg = new String(value);
                      }
                      mLogger.log("onCharacteristicWriteRequest: " + msg);
                    }
                  }
                });
        mGattserver.addService(ServiceFactory.generateService());
      } else {
        mLogger.log("Central mode not supported by the device!");
      }
    } else {
      mLogger.log("Bluetooth is disabled!");
    }
  }