public bool SendDeviceMonitoringRequest() { byte[] request = DeviceMonitorHelper.GetInstance().FormAdbRequest(RequestCommands.HOST_TRACK_DEVICES); if (DeviceMonitorHelper.GetInstance().Write(_socket, request) == false) { _socket.Close(); throw new IOException("Sending Tracking request failed!"); } SocketResponse resp = DeviceMonitorHelper.GetInstance().ReadAdbResponse(_socket, false /* readDiagString */); if (!resp.IOSuccess) { _socket.Close(); throw new IOException("Failed to read the adb response!"); } if (!resp.Okay) { // } return(resp.Okay); }
/// <summary> /// Sets the device. /// </summary> /// <param name="adbChan">The adb chan.</param> /// <param name="device">The device.</param> public void SetDevice(Socket adbChan, IDevice device) { // if the device is not null, then we first tell adb we're looking to talk // to a specific device if (device != null) { String msg = string.Format("{0}{1}", RequestCommands.HOST_TRANSPORT, device.SerialNumber); byte[] device_query = FormAdbRequest(msg); if (!Write(adbChan, device_query)) { throw new Exception("failed submitting device (" + device + ") request to ADB"); } SocketResponse resp = ReadAdbResponse(adbChan, false /* readDiagString */); if (!resp.Okay) { if (String.Compare("device not found", resp.Message, true) == 0) { throw new Exception(device.SerialNumber); } else { throw new Exception("device (" + device + ") request rejected: " + resp.Message); } } } }
/// <summary> /// Reads the adb response. /// </summary> /// <param name="socket">The socket.</param> /// <param name="readDiagString">if set to <c>true</c> [read diag string].</param> /// <returns></returns> public SocketResponse ReadAdbResponse(Socket socket, bool readDiagString) { SocketResponse resp = new SocketResponse(); byte[] reply = new byte[4]; if (!Read(socket, reply)) { return(resp); } resp.IOSuccess = true; if (IsOkay(reply)) { resp.Okay = true; } else { readDiagString = true; // look for a reason after the FAIL resp.Okay = false; } // not a loop -- use "while" so we can use "break" while (readDiagString) { // length string is in next 4 bytes byte[] lenBuf = new byte[4]; if (!Read(socket, lenBuf)) { Console.WriteLine("Expected diagnostic string not found"); break; } String lenStr = ReplyToString(lenBuf); int len; try { len = int.Parse(lenStr, System.Globalization.NumberStyles.HexNumber); } catch (FormatException) { break; } byte[] msg = new byte[len]; if (!Read(socket, msg)) { break; } resp.Message = ReplyToString(msg); break; } return(resp); }