-1

In my project i use an Android and an HC-05 Bluetooth. I need to send a message to the HC-05 module and then I send me back some data.

So I create a project like this :

public class MainActivity extends AppCompatActivity{

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

    if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
    {
        Toast.makeText(this, "BLE not supported", Toast.LENGTH_SHORT).show();
        finish();
    }

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

    if(bluetoothAdapter == null)
    {
        Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    scanLeDevice();
}

private BluetoothLeScanner bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
private boolean mScanning;
private Handler handler = new Handler();
UUID u1 = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");

private static final long SCAN_PERIOD = 2000;

private void scanLeDevice()
{
    if(!mScanning)
    {
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                mScanning = false;
                bluetoothLeScanner.stopScan(leScanCallBack);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        bluetoothLeScanner.startScan(leScanCallBack);
    }
    else
    {
        mScanning = false;
        bluetoothLeScanner.stopScan(leScanCallBack);
    }
}

private  String Device_name = "";
private String Device_address;

private ScanCallback leScanCallBack = new ScanCallback()
{
    @Override
    public void onScanResult(int callbackType, ScanResult result)
    {
        super.onScanResult(callbackType, result);

        if(result.getDevice().getName() != null && result.getDevice().getName().contains("ETNA") && result.getDevice().getName() != Device_name)
        {
            Device_name = String.valueOf(result.getDevice().getName());
            Device_address = String.valueOf(result.getDevice().getAddress());
            result.getDevice().connectGatt(MainActivity.this, false, bluetoothGattCallback);
            Log.e("Device Name : ", Device_name);
            Log.e("Device address", Device_address);
            Button button = findViewById(R.id.button);
            button.setText(Device_name);
            //bluetoothLeScanner.stopScan(leScanCallBack);
            //Log.e("Scan", "STOP");
        }
    }
};

BluetoothGattCallback bluetoothGattCallback =
        new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                                int newState) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.i("Message", "Connected to" + Device_name);
                    gatt.discoverServices();

                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                    Log.i("Message", "Disconnected from GATT server.");
                }
            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status)
            {
                super.onServicesDiscovered(gatt, status);
                Log.i("Message", "Discover");
                if(status == BluetoothGatt.GATT_SUCCESS)
                {
                    List<BluetoothGattService> services = gatt.getServices();
                    for(BluetoothGattService service : services)
                    {
                        List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                        for(BluetoothGattCharacteristic characteristic : characteristics)
                        {
                            if(characteristic.getUuid().compareTo(u1) == 0)
                            {
                                characteristic.setValue("up2" + '\r' + '\n');
                                characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                                gatt.writeCharacteristic(characteristic);;
                                Log.d("Message", "Write done");
                            }
                        }
                    }
                }
            }

            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
            {
                super.onCharacteristicWrite(gatt, characteristic, status);
                Log.d("Message","Characteristic " + characteristic.getValue() + " written");
                gatt.readCharacteristic(characteristic);
            }

            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
                byte[] value = characteristic.getValue();

                for(int i = 0; i < value.length; i++)
                {
                    Log.d("Size : ", String.valueOf(value.length));
                    Log.d("Variable : " + i, String.valueOf(value[i]));
                }
            }
        };}

I send to my HC-05 module the string code "up2" on the ServicesDiscovered function

characteristic.setValue("up2" + '\r' + '\n');

The probleme is I only received 1 number... you can see that on variable = 11

I/BluetoothDevice: connectGatt E/Device Name :: ETNA 1-1234 E/Device address: 6C:EC:EB:22:37:8D I/Message: Connected toETNA 1-1234 I/Message: Discover D/Message: Write done D/Message: Characteristic [B@66005a2 written D/Size :: 1 D/Variable : 0: 11

My C code on my HC-05 is :

if (buffUART[0] == 'u' && buffUART[1] == 'p' && buffUART[2] == '2')
{
  uint8_t string[30]={0, };
  string[0] = (uint8_t)(nb_up->value >> 24);
  string[1] = (uint8_t)(nb_up->value >> 16);
  string[2] = (uint8_t)(nb_up->value >> 8);
  string[3] = (uint8_t)(nb_up->value >> 0);
  string[4] = (uint8_t)(nb_down->value >> 24);
  string[5] = (uint8_t)(nb_down->value >> 16);
  string[6] = (uint8_t)(nb_down->value >> 8);
  string[7] = (uint8_t)(nb_down->value >> 0);
  string[8] = (uint8_t)(nb_reset->value >> 24);
  string[9] = (uint8_t)(nb_reset->value >> 16);
  string[10] = (uint8_t)(nb_reset->value >> 8);
  string[11] = (uint8_t)(nb_reset->value >> 0);
  string[12] = (uint8_t)(nb_stop_lvl_0->value >> 24);
  string[13] = (uint8_t)(nb_stop_lvl_0->value >> 16);
  string[14] = (uint8_t)(nb_stop_lvl_0->value >> 8);
  string[15] = (uint8_t)(nb_stop_lvl_0->value >> 0);
  string[16] = (uint8_t)(nb_stop_lvl_1->value >> 24);
  string[17] = (uint8_t)(nb_stop_lvl_1->value >> 16);
  string[18] = (uint8_t)(nb_stop_lvl_1->value >> 8);
  string[19] = (uint8_t)(nb_stop_lvl_1->value >> 0);
  string[20] = (uint8_t)(nb_stop_lvl_2->value >> 24);
  string[21] = (uint8_t)(nb_stop_lvl_2->value >> 16);
  string[22] = (uint8_t)(nb_stop_lvl_2->value >> 8);
  string[23] = (uint8_t)(nb_stop_lvl_2->value >> 0);
  string[24] = (uint8_t)(nb_stop_lvl_3->value >> 24);
  string[25] = (uint8_t)(nb_stop_lvl_3->value >> 16);
  string[26] = (uint8_t)(nb_stop_lvl_3->value >> 8);
  string[27] = (uint8_t)(nb_stop_lvl_3->value >> 0);
  string[16] = '\r';
  string[17] = '\n';
  HAL_UART_Transmit(&husart3, string, 18, HAL_MAX_DELAY);
  buffUART[0] = '0';
  buffUART[1] = '0';
}

I don't know where I'm wrong. And how much data I can send to my phone like 8,16,32 or more bits.

Wolf
  • 57
  • 7

1 Answers1

2

According to the HC-05 datasheet it is using Bluetooth Version 2.0 (Bluetooth Classic) while you are trying to access it with Bluetooth Low Energy (BLE), which was introduced in Bluetooth Version 4.0. You have to use a different module or write to it using classic Bluetooth.

Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23
  • Thx for the answer, if i use an HC-06 bluetooth module, this code might work ? – Wolf Jan 14 '21 at 06:35
  • 1
    The HC-06 module is also not a BLE module. Have a look at the Bluetooth classic way of finding and connecting to devices: https://developer.android.com/guide/topics/connectivity/bluetooth#DiscoverDevices – Michael Kotzjan Jan 14 '21 at 07:21
  • 1
    The HM-10 module seems to be a BLE mdoule and might work for you. Or you could switch your microcontroller to an esp32 which also offers BLE out of the box – Michael Kotzjan Jan 14 '21 at 07:22
  • 2
    If the device is Bluetooth Classic only, what device has the Android BLE code connected to? – Codo Jan 14 '21 at 08:08
  • 1
    I never tried connecting via BLE to a classics device, so I'm not sure about that. I can only assume it managed to connect to the correct device, just writing and reading works differently – Michael Kotzjan Jan 14 '21 at 08:41
  • 1
    You can use a BLE Scanner like nRF Connect to search for BLE devices near you – Michael Kotzjan Jan 14 '21 at 08:46
  • I try it, and i see my hc-05 bluetooth module on it, it's write Manufacturer data(Bluetooth Core 4.1) – Wolf Jan 14 '21 at 09:02
  • I buy an HM-10, next week i'll try my code with this BLE module. – Wolf Jan 14 '21 at 09:04
  • You try my code with a 4.0 Bluetooth and this work well ? Or i need to change something ? – Wolf Jan 14 '21 at 09:05
  • 1
    @M.Kotzjan i ask my colleague, he put on the elevator I work with this Bluetooth Module and it's write 2.0 like you say (https://fr.aliexpress.com/item/33025340093.html?spm=a2g0o.productlist.0.0.68824bbcb6gdpT&algo_pvid=52f4016d-ebbd-4f2b-a043-090491cf038a&algo_expid=52f4016d-ebbd-4f2b-a043-090491cf038a-3&btsid=2100bddb16106166739477035edb79&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_) – Wolf Jan 14 '21 at 09:33
  • @Codo My project in my company is to create an Android application, which must connect to the Bluetooth of an elevator. The elevator has a bluetooth module to communicate elevator information to detect faults. – Wolf Jan 14 '21 at 09:38
  • @Wolf I did not try your code, but it looks fine to me! Keep in mind that you are only writing your value, you are not reading anything. BLE is capable of notifications which you can subscribe to. Have a look at those – Michael Kotzjan Jan 14 '21 at 10:20
  • 1
    @M.Kotzjan Like this "gatt.setCharacteristicNotification(characteristic,true);" ? – Wolf Jan 14 '21 at 10:50
  • This is not enough for android and only turns on notifications locally. You also need to subscribe to the notifications of the remote device: BluetoothGattDescriptor descriptor = characteristic.getDescriptor( GattAttributes.CLIENT_CHARACTERISTIC_UUID); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); – Michael Kotzjan Jan 14 '21 at 11:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227334/discussion-between-wolf-and-m-kotzjan). – Wolf Jan 14 '21 at 12:48