Establishing Connections
The Aidlab SDK provides capabilities for discovering and connecting to compatible devices.
Begin by importing the Aidlab SDK module into your project:
from aidlab import AidlabManager, DeviceDelegate, DataType
import com.aidlab.sdk.*
import Aidlab
import 'package:aidlab_sdk/aidlab_sdk.dart';
Following the import of the necessary module, create an instance of the AidlabManager
object and declare relevant listeners:
class MainManager(DeviceDelegate):
def did_connect(self, device):
print("Connected to:", device.address)
class MainActivity : ComponentActivity(), DeviceDelegate, AidlabManagerDelegate {
private lateinit var aidlabManager: AidlabManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
aidlabManager = AidlabManager(this, this)
}
}
let aidlabManager = AidlabManager(delegate: self)
aidlabManager = AidlabManager(this);
aidlab = new Aidlab(this);
The next step involves initiating the discovery process and, if a compatible device is detected nearby, establishing a connection to it. Before initiating the discovery process, ensure that your device is correctly positioned (indicated by a green diode).
import asyncio
class MainManager(DeviceDelegate):
async run(self):
devices = await AidlabManager().scan()
if len(devices) > 0:
print("Connecting to: ", devices[0].address)
await devices[0].connect(self)
while True:
await asyncio.sleep(1)
# ...
asyncio.run(MainManager().run())
fun onBluetoothRequestsGranted() {
aidlbManager.scan()
}
override fun didDiscover(device: Device, rssi: Int) {
device.connect(this)
}
// The `scan` function enables scanning for nearby devices. If one is found, the `didDiscover` function is called:
aidlabManager.scan()
func didDiscover(_ device: Device) {
device.connect(delegate: self)
}
@override
void didDiscover(Device device, int rssi) {
device.connect(this);
}
void Start() {
enableBluetooth();
setLocationPermission();
}
void onAidlabDetected(AndroidJavaObject device, int rssi) {
if (this.aidlab.isAidlabDetected()) return;
this.aidlab.onDeviceDetectedEvent();
Signal[] signals = { Signal.respirationRate, Signal.temperature };
device.Call("connect", signals.Select(x => (int) x).ToArray(), false, this.aidlab);
}
This process enables your application to communicate with Aidlab or Aidmed One and access the valuable health data they generate.
Since Bluetooth detection and name lookups are probabilistic, the scanning process may occasionally fail to detect devices that are within range. In such cases, it may be advisable to retry once or twice before considering other options.
If you want to connect to a specific device, read Connecting to a Specified Device section.