/**
  * sendMessage will send a message represented as a String, over the BLE if server was
  * successfully started and client has connected.
  *
  * @param msg message to be sent
  */
 public void sendMessage(String msg) {
   if (mConnectedDevice != null) {
     BluetoothGattCharacteristic characteristic =
         ServiceFactory.generateService()
             .getCharacteristic(UUID.fromString(Constants.CHAT_CHARACTERISTIC_UUID));
     characteristic.setValue(msg);
     mGattserver.notifyCharacteristicChanged(mConnectedDevice, characteristic, false);
   }
 }
Beispiel #2
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);
  }
  /**
   * 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!");
    }
  }
 /**
  * destroy will close the gatt server if it was opened. Method should be called once we're done
  * with the BLE communication, to clear up the resources.
  */
 public void destroy() {
   if (mGattserver != null) {
     mGattserver.clearServices();
     mGattserver.close();
   }
 }