     Port.command(port, data)
     |> reply(state)
   end
+  def handle_call({:attach, ble}, _from, state) do
+    new_state = %__MODULE__{state | ble: ble}
+    reply(:ok, new_state)
+  end
+
 
   defp reply(ret_value, state) do
     {:reply, ret_value, state}

    GenServer.call(__MODULE__, {:cmd, cmd})
  end

  defstruct [port: nil, verbose: true, recording: true, log: nil]

  def init([]) do
    path = System.find_executable("bluetoothctl")

    Regex.replace(regexp, s, "")
  end

  def is_parsed_event?({:new, _}), do: true
  def is_parsed_event?({:change, _}), do: true
  def is_parsed_event?(_), do: false

  def parse("[NEW] " <> new_event), do: {:new, parse_device(new_event)}
  def parse("[CHG] " <> chg_event), do: {:change, parse_device(chg_event)}
  def parse(output) do
    case String.split(output, "[bluetooth]#", parts: 2) do
      [_prefix, cmd] -> parse(cmd)
      [_] -> {output}
    end
  end

  def parse_device(<<"Device ", uuid :: binary-size(17), " ", rest::binary>>) do
    {:device, uuid, device_state(rest)}
  end
  def parse_device(<<"Controller ", uuid :: binary-size(17), " ", rest::binary>>) do
    {:controller, uuid, controller_state(rest)}
  end

  def controller_state("Powered: " <> "yes"), do: [powered: true]
  def controller_state("Powered: " <> "no"), do: [powered: false]
  def controller_state("Discovering: " <> "yes"), do: [discovering: true]
  def controller_state("Discovering: " <> "no"), do: [discovering: false]
  def controller_state("Discoverable: " <> "yes"), do: [discoverable: true]
  def controller_state("Discoverable: " <> "no"), do: [discoverable: false]
  def controller_state(any_state), do: any_state

  def device_state("RSSI: " <> number), do: [rssi: Integer.parse(number, 10)]
  def device_state(any_state), do: any_state


end
