IDO SDK
File Local Import
pubspec.yaml
protocol_lib:
path: <file relative path>
protocol_alexa:
path: <file relative path>
flutter_bluetooth:
path: <file relative path>
SDK Initialization
void main() async{
await registerProtocolSDK();
await registerBluetoothSDK();
await registerProtocolAlexa();
await bridgeConnect();
}
/// Register protocol library
registerProtocolSDK() async {
/// writeToFile: logging to file
/// outputToConsole: logging to console
await IDOProtocolLibManager.initLog(writeToFile: true, outputToConsole: true);
/// Register for listening to update message icon for iOS
libManager.messageIcon.registerListenUpdate();
/// Listen to device notification
libManager.listenDeviceNotification((event) {
print('listenDeviceNotification: ${event.toMap().toString()}');
});
}
/// Register Bluetooth library
registerBluetoothSDK() async {
await bluetoothManager.register();
/// Get the version number
final version = bluetoothManager.getSdkVersion();
}
/// Register Alexa
/// Note: IDOProtocolAlexa depends on the protocol library and should be registered after the protocol library registration
registerProtocolAlexa() async {
final clientId = "amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
await IDOProtocolAlexa.registerAlexa(clientId: clientId);
}
/// Bridge connection between Bluetooth and protocol library
bridgeConnect() async {
// Handle Bluetooth return data
bluetoothManager.receiveData().listen((event) {
if (event.data != null) {
libManager.receiveDataFromBle(event.data!, event.macAddress, event.spp ?? false ? 1 : 0);
}
});
/// Listen to changes in the protocol library status
libManager.listenStatusNotification((status) async {
if (status == IDOStatusNotification.protocolConnectCompleted) {
/// Protocol library device connection and initialization completed
} else if (status == IDOStatusNotification.fastSyncCompleted) {
/// Fast configuration completed
/// Save device information and pair with Bluetooth on Android
bluetoothManager.setPair();
} else if (status == IDOStatusNotification.deviceInfoUpdateCompleted) {
/// Device information update completed
} else if (status == IDOStatusNotification.unbindOnBindStateError) {
/// Binding state error, unbind and delete the current device
} else if (status == IDOStatusNotification.fastSyncFailed) {
/// Fast configuration failed, menu retrieval failed
/// Save device information and pair with Bluetooth on Android, continue fast configuration until successful on the next synchronization
bluetoothManager.setPair();
}
});
// Write data to the Bluetooth device
IDOBluetoothWriteType rs = IDOBluetoothWriteType.withoutResponse;
libManager.registerWriteDataToBle((event) async {
rs = await bluetoothManager.writeData(event.data, type: event.type);
if (rs == IDOBluetoothWriteType.withoutResponse && Platform.isIOS) {
/// Send data without response
libManager.writeDataComplete();
}
});
// Bluetooth write state callback
bluetoothManager.writeState().listen((event) {
if (event.state ?? false) {
if (Platform.isAndroid ||
event.type == IDOBluetoothWriteType.withResponse) {
/// Write completed
libManager.writeDataComplete();
}
}
});
// Listen to connection state
bluetoothManager.deviceState().listen((value) async {
if (value.errorState == IDOBluetoothDeviceConnectErrorType.pairFail) {
// Pairing exception, prompt to ignore the device
}
if ((value.state == IDOBluetoothDeviceStateType.connected &&
(value.macAddress != null && value.macAddress!.isNotEmpty))) {
// Device connected successfully
/// Get OTA enumeration type
final isTlwOta = bluetoothManager.currentDevice?.isTlwOta ?? false
final isOta = bluetoothManager.currentDevice?.isOta ?? false
final otaType = isTlwOta ? IDOOtaType.telink : isOta ? IDOOtaType.nordic : IDOOtaType.none;
/// Get device name
final devicenName = bluetoothManager.currentDevice?.name;
/// Get device UUID (iOS only)
final uuid = bluetoothManager.currentDevice?.uuid;
/// Connect the device using the protocol library
libManager.markConnectedDevice(
macAddress: value.macAddress!,
otaType: otaType,
isBinded: deviceList != null,
deviceName: devicenName,
uuid: uuid);
} else if (value.state == IDOBluetoothDeviceStateType.disconnected) {
// Device disconnected
await libManager.markDisconnectedDevice(
macAddress: value.macAddress, uuid: value.uuid);
}
});
/// Listen to Bluetooth state
bluetoothManager.bluetoothState().listen((event) async {
/// Get device MAC address
final macAddress = bluetoothManager.currentDevice?.macAddress;
/// Get device UUID (iOS only)
final uuid = bluetoothManager.currentDevice?.uuid;
if (event.state == IDOBluetoothStateType.poweredOff) {
/// Bluetooth turned off
await libManager.markDisconnectedDevice(macAddress: macAddress,uuid: uuid);
}
});
}
Alexa Interface Usage
/// Example of using Alexa
useAlexa() {
// Switch Language (optional, default is English)
IDOProtocolAlexa.changeLanguage(AlexaLanguageType.usa);
// Set Delegate
IDOProtocolAlexa().delegate = <IDOAlexaDelegate>;
// Add Listener
IDOProtocolAlexa().listenLoginStateChanged((state) {
// Login state changed
});
// Authorize Alexa
// productId is the product ID registered in Alexa backend
// func is the callback function that returns the userCode and url
IDOProtocolAlexa().authorizeRequest(
productId: 'productId',
func: (userCode, url) {
// userCode is the verification code for Alexa login
// url is the official Alexa login URL to be opened in a WebView
// Add code to open WebView and load the URL here
}).then((rs) {
if (rs == LoginResponse.successful) {
print('Login successful');
} else {
print('Login failed $rs');
}
});
// Cancel login request
IDOProtocolAlexa().stopLogin();
// Logout Alexa
IDOProtocolAlexa().logout();
}
Bluetooth Library Interface Usage
useBluetooth() {
/// Current connected device
/// last connected device
final device = bluetoothManager.currentDevice;
/// Start scanning
/// macAddress (Android): search by macAddress
/// Returns the specified search device, or null if not specified
bluetoothManager.startScan();
/// Stop scanning
bluetoothManager.stopScan();
/// Search results
/// deviceName: search only devices with deviceName
/// deviceID: search only devices with deviceID
bluetoothManager.scanResult(deviceNames, deviceIDs, macAddresses, uuids).listen((event) {
});
/// Monitor device state
bluetoothManager.deviceState().listen((event) {
});
/// Monitor Bluetooth state
bluetoothManager.bluetoothState().listen((event) {
});
/// Connect to device selected from scan list
bluetoothManager.connect(device);
/// Use this to reconnect the device
bluetoothManager.needAutoConnect(true);
bluetoothManager.autoConnect(device);
/// Cancel connection
bluetoothManager.cancelConnect();
/// Get Bluetooth state
final bluetoothState = await getBluetoothState();
/// Get device connection state
final deviceState = await getDeviceState();
/// bt pairing (Android)
bluetoothManager.setBtPair(device);
/// unpair (Android)
bluetoothManager.cancelPair();
/// Send data
/// data: data to be sent
/// device: device to send data to
/// type: 0 for BLE data, 1 for SPP data
bluetoothManager.writeData(data, device, type).listen;
/// Send data status
bluetoothManager.writeState(device).listen;
/// Received data
bluetoothManager.receiveData(device).listen;
/// Connect to SPP (Android)
bluetoothManager.connectSPP(btMacAddress);
/// Disconnect SPP (Android)
bluetoothManager.disconnectSPP(btMacAddress);
/// Initiate Nordic DFU upgrade
bluetoothManager.startNordicDFU(config);
/// Monitor DFU progress, called externally
/// progress: progress
/// state: 'Completed' for upgrade completed
/// error: error message if not null
bluetoothManager.dfuProgress().listen((event) {
if (event['progress'] != null
&& event['progress'] is int) { // Progress
final progress = event['progress'] as int;
} else if (event['state'] != null &&
event['state'] is String &&
event['state'] == 'Completed') { // Upgrade completed
} else if (event['error'] != null
&& event['error'] is String) { // Upgrade error
final error = event['error'] as String;
}
});
/// Log path
bluetoothManager.logPath();
}