Android app, receiving data over BLE Module sent by Microcontroller via UART












0















I want to create android app, which can connect with bluetooth low energy module and can receive data. In my system microcontroller stm32f1 send data of measurements over UART to the BT LE module.



My question is how to start? I read a lot about GATT and UART Service, but still don't know how to start. Give me some information please.










share|improve this question

























  • Do you have the services and characteristic structure? We use a Andoid app called "BLE Scanner". This apps shows the structure and you can read and write values in the characteristics.

    – JMA
    Nov 13 '18 at 13:48













  • Yes, i used this program and got list with UUID adresses, u talked about Custom Service overlap? what is the next step, how to connect characteristics with data send by UART?

    – Adrian M
    Nov 13 '18 at 13:56











  • What is you BLE Module, HM-10?

    – Augusto
    Nov 13 '18 at 14:04











  • Bolutek BLE CC41A

    – Adrian M
    Nov 13 '18 at 14:08











  • Create a custom service with custom characteristics with read + notify properties, then every time the measurement changes update the characteristics. I think this module uses AT commands to create and update characteristics. The andoid app can subscribe to the characteristics and will recive a notification every time your device updates it.

    – JMA
    Nov 13 '18 at 15:00
















0















I want to create android app, which can connect with bluetooth low energy module and can receive data. In my system microcontroller stm32f1 send data of measurements over UART to the BT LE module.



My question is how to start? I read a lot about GATT and UART Service, but still don't know how to start. Give me some information please.










share|improve this question

























  • Do you have the services and characteristic structure? We use a Andoid app called "BLE Scanner". This apps shows the structure and you can read and write values in the characteristics.

    – JMA
    Nov 13 '18 at 13:48













  • Yes, i used this program and got list with UUID adresses, u talked about Custom Service overlap? what is the next step, how to connect characteristics with data send by UART?

    – Adrian M
    Nov 13 '18 at 13:56











  • What is you BLE Module, HM-10?

    – Augusto
    Nov 13 '18 at 14:04











  • Bolutek BLE CC41A

    – Adrian M
    Nov 13 '18 at 14:08











  • Create a custom service with custom characteristics with read + notify properties, then every time the measurement changes update the characteristics. I think this module uses AT commands to create and update characteristics. The andoid app can subscribe to the characteristics and will recive a notification every time your device updates it.

    – JMA
    Nov 13 '18 at 15:00














0












0








0








I want to create android app, which can connect with bluetooth low energy module and can receive data. In my system microcontroller stm32f1 send data of measurements over UART to the BT LE module.



My question is how to start? I read a lot about GATT and UART Service, but still don't know how to start. Give me some information please.










share|improve this question
















I want to create android app, which can connect with bluetooth low energy module and can receive data. In my system microcontroller stm32f1 send data of measurements over UART to the BT LE module.



My question is how to start? I read a lot about GATT and UART Service, but still don't know how to start. Give me some information please.







android bluetooth-lowenergy microcontroller stm32 uart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:00









ked2liz

601210




601210










asked Nov 13 '18 at 13:43









Adrian MAdrian M

11




11













  • Do you have the services and characteristic structure? We use a Andoid app called "BLE Scanner". This apps shows the structure and you can read and write values in the characteristics.

    – JMA
    Nov 13 '18 at 13:48













  • Yes, i used this program and got list with UUID adresses, u talked about Custom Service overlap? what is the next step, how to connect characteristics with data send by UART?

    – Adrian M
    Nov 13 '18 at 13:56











  • What is you BLE Module, HM-10?

    – Augusto
    Nov 13 '18 at 14:04











  • Bolutek BLE CC41A

    – Adrian M
    Nov 13 '18 at 14:08











  • Create a custom service with custom characteristics with read + notify properties, then every time the measurement changes update the characteristics. I think this module uses AT commands to create and update characteristics. The andoid app can subscribe to the characteristics and will recive a notification every time your device updates it.

    – JMA
    Nov 13 '18 at 15:00



















  • Do you have the services and characteristic structure? We use a Andoid app called "BLE Scanner". This apps shows the structure and you can read and write values in the characteristics.

    – JMA
    Nov 13 '18 at 13:48













  • Yes, i used this program and got list with UUID adresses, u talked about Custom Service overlap? what is the next step, how to connect characteristics with data send by UART?

    – Adrian M
    Nov 13 '18 at 13:56











  • What is you BLE Module, HM-10?

    – Augusto
    Nov 13 '18 at 14:04











  • Bolutek BLE CC41A

    – Adrian M
    Nov 13 '18 at 14:08











  • Create a custom service with custom characteristics with read + notify properties, then every time the measurement changes update the characteristics. I think this module uses AT commands to create and update characteristics. The andoid app can subscribe to the characteristics and will recive a notification every time your device updates it.

    – JMA
    Nov 13 '18 at 15:00

















Do you have the services and characteristic structure? We use a Andoid app called "BLE Scanner". This apps shows the structure and you can read and write values in the characteristics.

– JMA
Nov 13 '18 at 13:48







Do you have the services and characteristic structure? We use a Andoid app called "BLE Scanner". This apps shows the structure and you can read and write values in the characteristics.

– JMA
Nov 13 '18 at 13:48















Yes, i used this program and got list with UUID adresses, u talked about Custom Service overlap? what is the next step, how to connect characteristics with data send by UART?

– Adrian M
Nov 13 '18 at 13:56





Yes, i used this program and got list with UUID adresses, u talked about Custom Service overlap? what is the next step, how to connect characteristics with data send by UART?

– Adrian M
Nov 13 '18 at 13:56













What is you BLE Module, HM-10?

– Augusto
Nov 13 '18 at 14:04





What is you BLE Module, HM-10?

– Augusto
Nov 13 '18 at 14:04













Bolutek BLE CC41A

– Adrian M
Nov 13 '18 at 14:08





Bolutek BLE CC41A

– Adrian M
Nov 13 '18 at 14:08













Create a custom service with custom characteristics with read + notify properties, then every time the measurement changes update the characteristics. I think this module uses AT commands to create and update characteristics. The andoid app can subscribe to the characteristics and will recive a notification every time your device updates it.

– JMA
Nov 13 '18 at 15:00





Create a custom service with custom characteristics with read + notify properties, then every time the measurement changes update the characteristics. I think this module uses AT commands to create and update characteristics. The andoid app can subscribe to the characteristics and will recive a notification every time your device updates it.

– JMA
Nov 13 '18 at 15:00












1 Answer
1






active

oldest

votes


















0














You need:




  • UUIDs of your Service, Characteristic and Descriptor;


  • Permissions' Bluetooth Low Energy



The program must:




  1. Scan BLE devices

  2. Connect a device

  3. Set callbacks' BLE



AndroidManifest.xml




<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



Activity.java




// Bluetooth's variables
BluetoothAdapter bluetoothAdapter;
BluetoothLeScanner bluetoothLeScanner;
BluetoothManager bluetoothManager;
BluetoothScanCallback bluetoothScanCallback;
BluetoothGatt gattClient;

BluetoothGattCharacteristic characteristicID; // To get Value

// UUID's (set yours)
final UUID SERVICE_UUID = UUID.fromString("ab0828b1-198e-4351-b779-901fa0e0371e");
final UUID CHARACTERISTIC_UUID_ID = UUID.fromString("1a220d0a-6b06-4767-8692-243153d94d85");
final UUID DESCRIPTOR_UUID_ID = UUID.fromString("ec6e1003-884b-4a1c-850f-1cfce9cf6567");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Bluetooth
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
startScan();
}

// BLUETOOTH SCAN

private void startScan(){
Log.i(TAG,"startScan()");
bluetoothScanCallback = new BluetoothScanCallback();
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(bluetoothScanCallback);
}

// BLUETOOTH CONNECTION
private void connectDevice(BluetoothDevice device) {
if (device == null) Log.i(TAG,"Device is null");
GattClientCallback gattClientCallback = new GattClientCallback();
gattClient = device.connectGatt(this,false,gattClientCallback);
}

// BLE Scan Callbacks
private class BluetoothScanCallback extends ScanCallback {

@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "onScanResult");
if (result.getDevice().getName() != null){
if (result.getDevice().getName().equals(YOUR_DEVICE_NAME)) {
// When find your device, connect.
connectDevice(result.getDevice());
bluetoothLeScanner.stopScan(bluetoothScanCallback); // stop scan
}
}
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.i(TAG, "onBathScanResults");
}

@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "ErrorCode: " + errorCode);
}
}

// Bluetooth GATT Client Callback
private class GattClientCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.i(TAG,"onConnectionStateChange");

if (status == BluetoothGatt.GATT_FAILURE) {
Log.i(TAG, "onConnectionStateChange GATT FAILURE");
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "onConnectionStateChange != GATT_SUCCESS");
return;
}

if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange CONNECTED");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange DISCONNECTED");
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG,"onServicesDiscovered");
if (status != BluetoothGatt.GATT_SUCCESS) return;

// Reference your UUIDs
characteristicID = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_UUID_ID);
gatt.setCharacteristicNotification(characteristicID,true);

BluetoothGattDescriptor descriptor = characteristicID.getDescriptor(DESCRIPTOR_UUID_ID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicRead");
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicWrite");
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG,"onCharacteristicChanged");
// Here you can read the characteristc's value
// new String(characteristic.getValue();
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG,"onDescriptorRead");
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG,"onDescriptorWrite");
}
}


Libs:



import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;


Notes:




  • The UUIDs must be same seted on the module.

  • In your program you must have two callbacks: ScanCallback and GattCallback. Scan callback is to manage the scan results and the GattCallback you can manage the data input/output.

  • This code the basic to show how use BLE on Android, works fine for me.

  • You can generate UUID here: https://www.uuidgenerator.net/






share|improve this answer


























  • please send me a list of libs which you import and let me check it is working or not.

    – Adrian M
    Nov 13 '18 at 15:01











  • updated @AdrianM .....

    – Augusto
    Nov 13 '18 at 15:10











  • for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

    – Adrian M
    Nov 13 '18 at 15:31











  • Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

    – Augusto
    Nov 13 '18 at 15:35











  • I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

    – Augusto
    Nov 13 '18 at 15:37













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282386%2fandroid-app-receiving-data-over-ble-module-sent-by-microcontroller-via-uart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need:




  • UUIDs of your Service, Characteristic and Descriptor;


  • Permissions' Bluetooth Low Energy



The program must:




  1. Scan BLE devices

  2. Connect a device

  3. Set callbacks' BLE



AndroidManifest.xml




<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



Activity.java




// Bluetooth's variables
BluetoothAdapter bluetoothAdapter;
BluetoothLeScanner bluetoothLeScanner;
BluetoothManager bluetoothManager;
BluetoothScanCallback bluetoothScanCallback;
BluetoothGatt gattClient;

BluetoothGattCharacteristic characteristicID; // To get Value

// UUID's (set yours)
final UUID SERVICE_UUID = UUID.fromString("ab0828b1-198e-4351-b779-901fa0e0371e");
final UUID CHARACTERISTIC_UUID_ID = UUID.fromString("1a220d0a-6b06-4767-8692-243153d94d85");
final UUID DESCRIPTOR_UUID_ID = UUID.fromString("ec6e1003-884b-4a1c-850f-1cfce9cf6567");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Bluetooth
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
startScan();
}

// BLUETOOTH SCAN

private void startScan(){
Log.i(TAG,"startScan()");
bluetoothScanCallback = new BluetoothScanCallback();
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(bluetoothScanCallback);
}

// BLUETOOTH CONNECTION
private void connectDevice(BluetoothDevice device) {
if (device == null) Log.i(TAG,"Device is null");
GattClientCallback gattClientCallback = new GattClientCallback();
gattClient = device.connectGatt(this,false,gattClientCallback);
}

// BLE Scan Callbacks
private class BluetoothScanCallback extends ScanCallback {

@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "onScanResult");
if (result.getDevice().getName() != null){
if (result.getDevice().getName().equals(YOUR_DEVICE_NAME)) {
// When find your device, connect.
connectDevice(result.getDevice());
bluetoothLeScanner.stopScan(bluetoothScanCallback); // stop scan
}
}
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.i(TAG, "onBathScanResults");
}

@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "ErrorCode: " + errorCode);
}
}

// Bluetooth GATT Client Callback
private class GattClientCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.i(TAG,"onConnectionStateChange");

if (status == BluetoothGatt.GATT_FAILURE) {
Log.i(TAG, "onConnectionStateChange GATT FAILURE");
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "onConnectionStateChange != GATT_SUCCESS");
return;
}

if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange CONNECTED");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange DISCONNECTED");
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG,"onServicesDiscovered");
if (status != BluetoothGatt.GATT_SUCCESS) return;

// Reference your UUIDs
characteristicID = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_UUID_ID);
gatt.setCharacteristicNotification(characteristicID,true);

BluetoothGattDescriptor descriptor = characteristicID.getDescriptor(DESCRIPTOR_UUID_ID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicRead");
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicWrite");
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG,"onCharacteristicChanged");
// Here you can read the characteristc's value
// new String(characteristic.getValue();
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG,"onDescriptorRead");
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG,"onDescriptorWrite");
}
}


Libs:



import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;


Notes:




  • The UUIDs must be same seted on the module.

  • In your program you must have two callbacks: ScanCallback and GattCallback. Scan callback is to manage the scan results and the GattCallback you can manage the data input/output.

  • This code the basic to show how use BLE on Android, works fine for me.

  • You can generate UUID here: https://www.uuidgenerator.net/






share|improve this answer


























  • please send me a list of libs which you import and let me check it is working or not.

    – Adrian M
    Nov 13 '18 at 15:01











  • updated @AdrianM .....

    – Augusto
    Nov 13 '18 at 15:10











  • for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

    – Adrian M
    Nov 13 '18 at 15:31











  • Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

    – Augusto
    Nov 13 '18 at 15:35











  • I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

    – Augusto
    Nov 13 '18 at 15:37


















0














You need:




  • UUIDs of your Service, Characteristic and Descriptor;


  • Permissions' Bluetooth Low Energy



The program must:




  1. Scan BLE devices

  2. Connect a device

  3. Set callbacks' BLE



AndroidManifest.xml




<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



Activity.java




// Bluetooth's variables
BluetoothAdapter bluetoothAdapter;
BluetoothLeScanner bluetoothLeScanner;
BluetoothManager bluetoothManager;
BluetoothScanCallback bluetoothScanCallback;
BluetoothGatt gattClient;

BluetoothGattCharacteristic characteristicID; // To get Value

// UUID's (set yours)
final UUID SERVICE_UUID = UUID.fromString("ab0828b1-198e-4351-b779-901fa0e0371e");
final UUID CHARACTERISTIC_UUID_ID = UUID.fromString("1a220d0a-6b06-4767-8692-243153d94d85");
final UUID DESCRIPTOR_UUID_ID = UUID.fromString("ec6e1003-884b-4a1c-850f-1cfce9cf6567");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Bluetooth
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
startScan();
}

// BLUETOOTH SCAN

private void startScan(){
Log.i(TAG,"startScan()");
bluetoothScanCallback = new BluetoothScanCallback();
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(bluetoothScanCallback);
}

// BLUETOOTH CONNECTION
private void connectDevice(BluetoothDevice device) {
if (device == null) Log.i(TAG,"Device is null");
GattClientCallback gattClientCallback = new GattClientCallback();
gattClient = device.connectGatt(this,false,gattClientCallback);
}

// BLE Scan Callbacks
private class BluetoothScanCallback extends ScanCallback {

@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "onScanResult");
if (result.getDevice().getName() != null){
if (result.getDevice().getName().equals(YOUR_DEVICE_NAME)) {
// When find your device, connect.
connectDevice(result.getDevice());
bluetoothLeScanner.stopScan(bluetoothScanCallback); // stop scan
}
}
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.i(TAG, "onBathScanResults");
}

@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "ErrorCode: " + errorCode);
}
}

// Bluetooth GATT Client Callback
private class GattClientCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.i(TAG,"onConnectionStateChange");

if (status == BluetoothGatt.GATT_FAILURE) {
Log.i(TAG, "onConnectionStateChange GATT FAILURE");
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "onConnectionStateChange != GATT_SUCCESS");
return;
}

if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange CONNECTED");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange DISCONNECTED");
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG,"onServicesDiscovered");
if (status != BluetoothGatt.GATT_SUCCESS) return;

// Reference your UUIDs
characteristicID = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_UUID_ID);
gatt.setCharacteristicNotification(characteristicID,true);

BluetoothGattDescriptor descriptor = characteristicID.getDescriptor(DESCRIPTOR_UUID_ID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicRead");
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicWrite");
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG,"onCharacteristicChanged");
// Here you can read the characteristc's value
// new String(characteristic.getValue();
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG,"onDescriptorRead");
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG,"onDescriptorWrite");
}
}


Libs:



import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;


Notes:




  • The UUIDs must be same seted on the module.

  • In your program you must have two callbacks: ScanCallback and GattCallback. Scan callback is to manage the scan results and the GattCallback you can manage the data input/output.

  • This code the basic to show how use BLE on Android, works fine for me.

  • You can generate UUID here: https://www.uuidgenerator.net/






share|improve this answer


























  • please send me a list of libs which you import and let me check it is working or not.

    – Adrian M
    Nov 13 '18 at 15:01











  • updated @AdrianM .....

    – Augusto
    Nov 13 '18 at 15:10











  • for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

    – Adrian M
    Nov 13 '18 at 15:31











  • Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

    – Augusto
    Nov 13 '18 at 15:35











  • I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

    – Augusto
    Nov 13 '18 at 15:37
















0












0








0







You need:




  • UUIDs of your Service, Characteristic and Descriptor;


  • Permissions' Bluetooth Low Energy



The program must:




  1. Scan BLE devices

  2. Connect a device

  3. Set callbacks' BLE



AndroidManifest.xml




<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



Activity.java




// Bluetooth's variables
BluetoothAdapter bluetoothAdapter;
BluetoothLeScanner bluetoothLeScanner;
BluetoothManager bluetoothManager;
BluetoothScanCallback bluetoothScanCallback;
BluetoothGatt gattClient;

BluetoothGattCharacteristic characteristicID; // To get Value

// UUID's (set yours)
final UUID SERVICE_UUID = UUID.fromString("ab0828b1-198e-4351-b779-901fa0e0371e");
final UUID CHARACTERISTIC_UUID_ID = UUID.fromString("1a220d0a-6b06-4767-8692-243153d94d85");
final UUID DESCRIPTOR_UUID_ID = UUID.fromString("ec6e1003-884b-4a1c-850f-1cfce9cf6567");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Bluetooth
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
startScan();
}

// BLUETOOTH SCAN

private void startScan(){
Log.i(TAG,"startScan()");
bluetoothScanCallback = new BluetoothScanCallback();
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(bluetoothScanCallback);
}

// BLUETOOTH CONNECTION
private void connectDevice(BluetoothDevice device) {
if (device == null) Log.i(TAG,"Device is null");
GattClientCallback gattClientCallback = new GattClientCallback();
gattClient = device.connectGatt(this,false,gattClientCallback);
}

// BLE Scan Callbacks
private class BluetoothScanCallback extends ScanCallback {

@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "onScanResult");
if (result.getDevice().getName() != null){
if (result.getDevice().getName().equals(YOUR_DEVICE_NAME)) {
// When find your device, connect.
connectDevice(result.getDevice());
bluetoothLeScanner.stopScan(bluetoothScanCallback); // stop scan
}
}
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.i(TAG, "onBathScanResults");
}

@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "ErrorCode: " + errorCode);
}
}

// Bluetooth GATT Client Callback
private class GattClientCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.i(TAG,"onConnectionStateChange");

if (status == BluetoothGatt.GATT_FAILURE) {
Log.i(TAG, "onConnectionStateChange GATT FAILURE");
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "onConnectionStateChange != GATT_SUCCESS");
return;
}

if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange CONNECTED");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange DISCONNECTED");
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG,"onServicesDiscovered");
if (status != BluetoothGatt.GATT_SUCCESS) return;

// Reference your UUIDs
characteristicID = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_UUID_ID);
gatt.setCharacteristicNotification(characteristicID,true);

BluetoothGattDescriptor descriptor = characteristicID.getDescriptor(DESCRIPTOR_UUID_ID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicRead");
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicWrite");
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG,"onCharacteristicChanged");
// Here you can read the characteristc's value
// new String(characteristic.getValue();
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG,"onDescriptorRead");
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG,"onDescriptorWrite");
}
}


Libs:



import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;


Notes:




  • The UUIDs must be same seted on the module.

  • In your program you must have two callbacks: ScanCallback and GattCallback. Scan callback is to manage the scan results and the GattCallback you can manage the data input/output.

  • This code the basic to show how use BLE on Android, works fine for me.

  • You can generate UUID here: https://www.uuidgenerator.net/






share|improve this answer















You need:




  • UUIDs of your Service, Characteristic and Descriptor;


  • Permissions' Bluetooth Low Energy



The program must:




  1. Scan BLE devices

  2. Connect a device

  3. Set callbacks' BLE



AndroidManifest.xml




<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



Activity.java




// Bluetooth's variables
BluetoothAdapter bluetoothAdapter;
BluetoothLeScanner bluetoothLeScanner;
BluetoothManager bluetoothManager;
BluetoothScanCallback bluetoothScanCallback;
BluetoothGatt gattClient;

BluetoothGattCharacteristic characteristicID; // To get Value

// UUID's (set yours)
final UUID SERVICE_UUID = UUID.fromString("ab0828b1-198e-4351-b779-901fa0e0371e");
final UUID CHARACTERISTIC_UUID_ID = UUID.fromString("1a220d0a-6b06-4767-8692-243153d94d85");
final UUID DESCRIPTOR_UUID_ID = UUID.fromString("ec6e1003-884b-4a1c-850f-1cfce9cf6567");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Bluetooth
bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
startScan();
}

// BLUETOOTH SCAN

private void startScan(){
Log.i(TAG,"startScan()");
bluetoothScanCallback = new BluetoothScanCallback();
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(bluetoothScanCallback);
}

// BLUETOOTH CONNECTION
private void connectDevice(BluetoothDevice device) {
if (device == null) Log.i(TAG,"Device is null");
GattClientCallback gattClientCallback = new GattClientCallback();
gattClient = device.connectGatt(this,false,gattClientCallback);
}

// BLE Scan Callbacks
private class BluetoothScanCallback extends ScanCallback {

@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, "onScanResult");
if (result.getDevice().getName() != null){
if (result.getDevice().getName().equals(YOUR_DEVICE_NAME)) {
// When find your device, connect.
connectDevice(result.getDevice());
bluetoothLeScanner.stopScan(bluetoothScanCallback); // stop scan
}
}
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.i(TAG, "onBathScanResults");
}

@Override
public void onScanFailed(int errorCode) {
Log.i(TAG, "ErrorCode: " + errorCode);
}
}

// Bluetooth GATT Client Callback
private class GattClientCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.i(TAG,"onConnectionStateChange");

if (status == BluetoothGatt.GATT_FAILURE) {
Log.i(TAG, "onConnectionStateChange GATT FAILURE");
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "onConnectionStateChange != GATT_SUCCESS");
return;
}

if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange CONNECTED");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange DISCONNECTED");
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG,"onServicesDiscovered");
if (status != BluetoothGatt.GATT_SUCCESS) return;

// Reference your UUIDs
characteristicID = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_UUID_ID);
gatt.setCharacteristicNotification(characteristicID,true);

BluetoothGattDescriptor descriptor = characteristicID.getDescriptor(DESCRIPTOR_UUID_ID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicRead");
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG,"onCharacteristicWrite");
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG,"onCharacteristicChanged");
// Here you can read the characteristc's value
// new String(characteristic.getValue();
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG,"onDescriptorRead");
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG,"onDescriptorWrite");
}
}


Libs:



import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;


Notes:




  • The UUIDs must be same seted on the module.

  • In your program you must have two callbacks: ScanCallback and GattCallback. Scan callback is to manage the scan results and the GattCallback you can manage the data input/output.

  • This code the basic to show how use BLE on Android, works fine for me.

  • You can generate UUID here: https://www.uuidgenerator.net/







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 15:07

























answered Nov 13 '18 at 14:20









AugustoAugusto

455519




455519













  • please send me a list of libs which you import and let me check it is working or not.

    – Adrian M
    Nov 13 '18 at 15:01











  • updated @AdrianM .....

    – Augusto
    Nov 13 '18 at 15:10











  • for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

    – Adrian M
    Nov 13 '18 at 15:31











  • Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

    – Augusto
    Nov 13 '18 at 15:35











  • I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

    – Augusto
    Nov 13 '18 at 15:37





















  • please send me a list of libs which you import and let me check it is working or not.

    – Adrian M
    Nov 13 '18 at 15:01











  • updated @AdrianM .....

    – Augusto
    Nov 13 '18 at 15:10











  • for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

    – Adrian M
    Nov 13 '18 at 15:31











  • Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

    – Augusto
    Nov 13 '18 at 15:35











  • I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

    – Augusto
    Nov 13 '18 at 15:37



















please send me a list of libs which you import and let me check it is working or not.

– Adrian M
Nov 13 '18 at 15:01





please send me a list of libs which you import and let me check it is working or not.

– Adrian M
Nov 13 '18 at 15:01













updated @AdrianM .....

– Augusto
Nov 13 '18 at 15:10





updated @AdrianM .....

– Augusto
Nov 13 '18 at 15:10













for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

– Adrian M
Nov 13 '18 at 15:31





for me descriptor is 0x2902, can i just copy it ino DESCRIPTOR_UUID_ID line?

– Adrian M
Nov 13 '18 at 15:31













Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

– Augusto
Nov 13 '18 at 15:35





Of course. But you must use the fulll UUID in 128bit format String. I don't remember which is, but you can see in the BLE Scanner

– Augusto
Nov 13 '18 at 15:35













I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

– Augusto
Nov 13 '18 at 15:37







I think that is: 00002902-0000-1000-8000-00805F9B34FB. To UUID 0x2902.

– Augusto
Nov 13 '18 at 15:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282386%2fandroid-app-receiving-data-over-ble-module-sent-by-microcontroller-via-uart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python