Reputation: 455
Currently I am trying to receive data using the transferIn for webusb api. However this function does not appear to work. Also noticed that my transferOut commands do not execute on my devices. Some info, I am trying to make my flutter web app run ADB commands using the WebUSB api for chromium browsers.
This is my sendADBCommand function:
async function sendADBCommand(command) {
const device = window.adbDevice;
const outEndpoint = window.adbOutEndpoint;
const inEndpoint = window.adbInEndpoint;
if (!device) {
console.error('ADB device is not available.');
return;
}
const encoder = new TextEncoder();
const data = encoder.encode(command + '\n');
try {
const sentResult = await device.transferOut(outEndpoint, data);
console.log('Sent command:', command, 'Result:', sentResult);
try {
const response = await device.transferIn(inEndpoint, 1024);
const decoder = new TextDecoder();
const responseData = decoder.decode(response.data);
console.log(`Respone: `, responseData);
} catch (error) {
console.warn(`No response received: `, error);
}
return sentResult;
} catch (error) {
console.error('Error sending command:', error);
console.error('Error name:', error.name);
console.error('Error message:', error.message);
console.error('Stack trace:', error.stack);
}
}
This is my function to send a install/replace of an APK on the device:
async function updateApk(){
const apkPath = "/var/www/portal/web/apk/app.apk";
for (let device of connectedDevices) {
console.log(`Attempting to update App on device: ${device.serialNumber}`);
try {
const result = await sendADBCommand(`adb -s ${device.serialNumber} install -r ${apkPath}`);
if(result){
console.log(`Successfully updated App on device: ${device.serialNumber}: ${result}`);
} else {
console.error(`Failed to send install command to device: ${device.serialNumber}`);
}
} catch (error) {
console.error(`Error updating App on device ${device.serialNumber}:`, error);
}
}
}
And then my function to connect a device:
let connectedDevices = [];
async function requestADBDevice(filters = []) {
let device;
try {
device = await navigator.usb.requestDevice({ filters });
await device.open();
if (device.configuration === null) {
await device.selectConfiguration(1);
}
await device.claimInterface(0);
const productName = device.productName || 'Unknown product';
const vendorId = device.vendorId.toString(16).padStart(4, '0');
const productId = device.productId.toString(16).padStart(4, '0');
const interfaceNumber = device.configuration.interfaces[0]?.interfaceNumber || 'Unknown interface';
const endpoints = device.configuration.interfaces[0].alternates[0].endpoints;
let inEndpoint = null;
let outEndpoint = null;
for (const endpoint of endpoints){
if(endpoint.direction === 'in'){
inEndpoint = endpoint.endpointNumber;
} else if (endpoint.direction === 'out'){
outEndpoint = endpoint.endpointNumber;
}
}
if(inEndpoint === null || outEndpoint === null){
throw new Error('Could not find appriopriate IN and OUT endpoints');
}
device.inEndpoint = inEndpoint;
device.outEndpoint = outEndpoint;
window.adbDevice = device;
window.adbInEndpoint = inEndpoint;
window.adbOutEndpoint = outEndpoint;
console.log(`Device connected: ${productName}`);
console.log(`Vendor ID: 0x${vendorId}, Product ID: 0x${productId}`);
console.log(`Interface number: ${interfaceNumber}`);
console.log(`IN Endpoint: ${inEndpoint}, OUT Endpoint: ${outEndpoint}`);
connectedDevices.push(device);
return device;
} catch (error) {
if (device) {
try {
if (device.opened) {
await device.close();
}
} catch (closeError) {
console.error('Error closing device after failure:', closeError);
}
}
console.error('Error requesting ADB device:', error);
}
}
Upvotes: 0
Views: 61