2

I am working on Bluetooth low energy GATT to communicate with the chip. I can able to read the response from chip but i could not able to send the characteristics into that chip and also notify some characteristics. Can any only help.

Thanks in advance.

Shine
  • 309
  • 3
  • 13
  • Please specify what Bluetooth device / chip you are using. Remember if its a custom chip you got from a vendor - check with the vendor for the specifics. No one can help with this limited info. – Morrison Chang Mar 29 '16 at 04:46
  • Hi they have given some MAC address in that BoreCam Chip and CoreCam chip – Shine Mar 29 '16 at 04:55
  • I don't know what BoreCam is unless its Broadcom. Without specifics such as actual manufacturer and model # as well as showing what you've tried in some detail no one can help you. – Morrison Chang Mar 29 '16 at 05:04
  • Is it like beacon and provides you services like ff2/ ff1 ? – Lalit Jadav Feb 28 '17 at 05:10

2 Answers2

0

Assuming you have your BluetoothGattServer setup correctly, the characteristics registered with the service and the service added to the BluetoothGattServer, here is an example of sending some data to a notifying characteristic:

    private static final UUID serviceUuid   = UUID.fromString("SOME-SERVICE-UUID");
    private static final UUID characteristicUuid = UUID.fromString("SOME-CHAR-UUID");
    private BluetoothGattServer gattServer;
    private BluetoothDevice peerDevice;

    public void sendNotification(byte p1, byte p2, byte p3, byte p4, int correlationid) {
        ByteBuffer bb = ByteBuffer.allocate(8);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.put(p1).put(p2).put(p3).put(p4).putInt(correlationid);
        BluetoothGattCharacteristic notifyingChar = gattServer.getService(serviceUuid).getCharacteristic(characteristicUuid);
        notifyingChar.setValue(bb.array());
        gattServer.notifyCharacteristicChanged(peerDevice, notifyingChar, false);
    }

You will receive an event when the data has been sent in the BluetoothGattServerCallback.onNotificationSent method:

    @Override
    public void onNotificationSent(BluetoothDevice device, int status) {
        super.onNotificationSent(device, status);
        Log.d("SVC", "BluetoothGattServerCallback.onNotificationSent");
    }
BitByteDog
  • 3,074
  • 2
  • 26
  • 39
0

Well, first of all, I strongly recommend that you use the amazing Bluetooth LE open-source library called RxAndroidBle. It will make the whole process way easier.

Once you have included that library in your project, you will want to do the following:

  1. Make sure that the Bluetooth is enabled and that you have already asked the user for Location permissions.
  2. Scan for devices

Example:

RxBleClient rxBleClient = RxBleClient.create(context);

Disposable scanSubscription = rxBleClient.scanBleDevices(
        new ScanSettings.Builder()
            // .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
            // .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
            .build()
        // add filters if needed
)
    .subscribe(
        scanResult -> {
            // Process scan result here.
        },
        throwable -> {
            // Handle an error here.
        }
    );

// When done, just dispose.
scanSubscription.dispose();
  1. Connect to the desired device and use the writeCharacteristic() method to write the bytes you want.

Example:

device.establishConnection(false)
    .flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
    .subscribe(
        characteristicValue -> {
            // Characteristic value confirmed.
        },
        throwable -> {
            // Handle an error here.
        }
    ); 
  1. If instead, you want to set up the notification/indication on a characteristic, you can do the following:

Example:

device.establishConnection(false)
    .flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid))
    .doOnNext(notificationObservable -> {
        // Notification has been set up
    })
    .flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
    .subscribe(
        bytes -> {
            // Given characteristic has been changes, here is the value.
        },
        throwable -> {
            // Handle an error here.
        }
    );

There is plenty of information in their Github page and also they have their own dedicated tag in Stackoverflow.

dglozano
  • 6,369
  • 2
  • 19
  • 38