I have started recently playing with a BLE devices. When I wrote B4A app to connect to them, I found out that I couldn't connect to the device. I have wasted 2 days, thinking that the problem is the device. I have modified firmware of the device, flashed to the device but not luck. Then I realised, that since I can connect with other application like "nRF Connect" and "BLE Scanner", the problem must be my B4A application. I have described the problem and a solution in the thread here: https://www.b4x.com/android/forum/threads/solved-ble-connection-problems.133142/.
So to fix the problem I had to add an additional function that used BTtransport parameter. This is a java function:
This fixed the problem.
Next problem (maybe not a problem, but something I didn't like), was, that to connect to BLE device, you had to scan to find the device first, then connect to the found device. This takes time - depending on the scan process.
So I wanted to connect to a specific device (with a known MAC address) without scanning.
So here is another java function that allows you to connect to the paired device with a MAC Address. Note, the device has to be paired with the phone!!!
I have posted those 2 functions in the "Wish forum", but it doesn't look like anything will happen, so I decided to post the guide how to upgrade the BLE2 library.
1. Thanks to Erel, you can download the library source code (of all internal libraries) here: https://github.com/AnywhereSoftware/B4A
2. Then, on your PC, go to the Libs_BLE2 folder and navigate through subfolders, until you find BleManager2.java
3. Open the file with any text editor and add the 2 functions above.
4. Add to the import section in the file:
5. You can modify the library version number - line 54 in this file:
6. Download SCL library compiler here: https://www.b4x.com/android/forum/t...uild-libraries-without-eclipse.29918/#content
7. Run LibraryCompiler.exe.
8. In "Project Folder" - click Browse, and select Libs_BLE2 folder
9. Library Name - give the name for the library that you want to have. Note: BLE2 library is an internal library, so you want to give modified library a different name.
10. Click Compile - and the compiled library will be put in your external libraries folder. This will also create an XML file for the library.
In your B4A app you can call those functions, for example like this:
So to fix the problem I had to add an additional function that used BTtransport parameter. This is a java function:
Java:
/**
* Connects to a device with the given id. You can only connect to previously discovered devices.
* Note that the Disconnected event will be raised if the connection has failed.
* AutoConnect - disable/enable auto connect
* BtTransport - AUTO 0, BR_EDR 1, LE 2
*/
public void Connect3(String DeviceId, boolean AutoConnect, int BtTransport) {
charsToReadQueue.clear();
BluetoothDevice bd = this.devices.get(DeviceId);
int Transport = BtTransport;
if(Transport > 2){
Transport = 0;
}
if (bd == null)
throw new RuntimeException("MacAddress not found. Make sure to call Scan before trying to connect.");
bd.connectGatt(BA.applicationContext, AutoConnect, new GattCallback(), Transport);
}
This fixed the problem.
Next problem (maybe not a problem, but something I didn't like), was, that to connect to BLE device, you had to scan to find the device first, then connect to the found device. This takes time - depending on the scan process.
So I wanted to connect to a specific device (with a known MAC address) without scanning.
So here is another java function that allows you to connect to the paired device with a MAC Address. Note, the device has to be paired with the phone!!!
Java:
/**
* Connects to a device with a paired device with the given id.
* AutoConnect - disable/enable auto connect
* BtTransport - AUTO 0, BR_EDR 1, LE 2
*/
public void ConnectToAddress(String DeviceId, boolean AutoConnect, int BtTransport){
BluetoothDevice device = blueAdapter.getRemoteDevice(DeviceId);
BluetoothDevice bd = this.devices.get(DeviceId);
int Transport = BtTransport;
if(Transport > 2){
Transport = 0;
}
int bs = device.getBondState();
if(bs == BluetoothDevice.BOND_BONDED){
device.connectGatt(BA.applicationContext, AutoConnect, new GattCallback(), Transport);
}
else{
String sMac = device.getAddress();
Toast.makeText(BA.applicationContext, "Device " + sMac + " not Bonded", Toast.LENGTH_SHORT).show();
}
}
I have posted those 2 functions in the "Wish forum", but it doesn't look like anything will happen, so I decided to post the guide how to upgrade the BLE2 library.
1. Thanks to Erel, you can download the library source code (of all internal libraries) here: https://github.com/AnywhereSoftware/B4A
2. Then, on your PC, go to the Libs_BLE2 folder and navigate through subfolders, until you find BleManager2.java
3. Open the file with any text editor and add the 2 functions above.
4. Add to the import section in the file:
Java:
import android.widget.Toast;
Java:
@Version(1.39f)
7. Run LibraryCompiler.exe.
8. In "Project Folder" - click Browse, and select Libs_BLE2 folder
9. Library Name - give the name for the library that you want to have. Note: BLE2 library is an internal library, so you want to give modified library a different name.
10. Click Compile - and the compiled library will be put in your external libraries folder. This will also create an XML file for the library.
In your B4A app you can call those functions, for example like this:
B4X:
Sub ConnectDevAddress(Name As String, Id As String)
Log("name " & Name & ", Id " & Id)
ConnectedName = Name
manager.ConnectToAddress(Id, True, 2) 'btTransport parameter Auto = 0
End Sub
Sub ConnectDevice(Name As String, Id As String)
Log("name " & Name & ", Id " & Id)
ConnectedName = Name
manager.StopScan
Sleep(1000)
manager.Connect3(Id, True, 2) 'btTransport parameter Auto = 0
End Sub