Package: nxbt-naibaoofficial
NXBTControl your Nintendo Switch through a website, terminal, or macro.Key Features•Installation•Getting Started•Troubleshooting•Credits•LicenseImportantThis program is based on Brikwerk's nxbt project for secondary development, in order to respect the original author, I did not modify all the copyright information in the documentation, only the necessary places to make changes to the documentation.Key FeaturesUse your favourite web browser to control a Nintendo Switch with any keyboard or gamepad.Use your terminal to control a Nintendo Switch with a keyboard.Use a macro from your terminal, browser, or Python scriptUse the NXBT Python API to write programs to control your Nintendo Switch.In-depth command line interface.Support for emulating multiple controllers at once.Support for fast connection or reconnection to a Nintendo Switch.Emulated ontrollers support thread-safe access.** Use macro in loop, support automacic go back to game and press 'YB' together.InstallationLinuxsudopip3installnxbt-naibaoofficialPlease Note:NXBT needs root privileges to toggle the BlueZ Input plugin. If you're not comfortable running this program as root, you can disable the Input plugin manually, and install NXBT as a regular user.Windows and macOSSee the installation guidehere.Getting StartedNote:If you installed NXBT as a non-root user, please omit the use ofsudofrom any of the following commands.Running the demoThe demo is meant to gauge whether or not NXBT is working. To do so, the demo will create a Pro Controller and run through a small loop of commands.NOTE:If this is your first time connecting to an NXBT emulated controller on the specific host computer, youMUSThave the "Change Grip/Order Menu" open on your Nintendo Switch. You can see how to navigate to the "Change Grip/Order Menu"HERE.To start the demo, run the following command in your terminal:sudonxbtdemoIf all is working correctly, the controller should connect, navigate to the settings, test the stick calibration, and navigate back to the "Change Grip/Order Menu".Using the WebappThe NXBT webapp provides a web interface that allows for quick creation of a Nintendo Switch controller and use of a keyboard or gamepad to control the Nintendo Switch. This lets anyone who can access the website control a Nintendo Switch with their favourite keyboard or gamepad.The webapp server can be started with the following command:sudonxbtwebappThe above command boots NXBT and an accompanying web server that allows for controller creation and use over your web browser.The webapp itself will be locally accessible athttp://127.0.0.1:8000or, if you're on the same network as the host computer, http://HOST_COMPUTER_IP:8000. It's also possible to expose your NXBT webapp to the internet, however, you'll need to configure a reverse proxy, which is out of the scope of this readme.You should see a webpage similar to the following image:To create and start a Pro Controller, click the Pro controller graphic. If creation/boot is successful, the website will switch to a loading screen. During this time, you should have the Nintendo Switch you wish to connect to powered on and within range of the host computer.NOTE:If this is your first time connecting to your Nintendo Switch with the specific host computer, make sure you're on the "Change Grip/Order Menu". If you're still unable to connect, try running the demo (in the above section) or refer to the troubleshooting documentation.Once you've successfully connected to the Nintendo Switch, you should see a webpage similar to below:Here, you can change your input method, shutdown or restart the controller, and run an NXBT macro.A few other functions to note:If you exit the webpage, the controller will shutdown.Once you've connected over the "Change Grip/Order Menu", NXBT will automatically reconnect. This applies on a per-Bluetooth-adapter basis.Most gamepads should be usable over the browser. To get started with a gamepad, click a button and it should show up under the input dropdown list. If it doesn't show up, try another browser. Chrome is the recommended standard as it seems to have the best gamepad support currently (as of September 2020)Using the TUIThe TUI (Terminal User Interface) allows for local or remote (SSH/Mosh) terminal sessions to control a Nintendo Switch with a keyboard.The TUI can be started with:sudonxbttuiNOTE:If this is your first time connecting to your Nintendo Switch with the specific host computer, make sure you're on the "Change Grip/Order Menu". If you're still unable to connect, try running the demo (in the above section) or refer to the troubleshooting documentation.A loading screen should open and, once connected, the main TUI control screen should load. This should look something like below:There are two types of NXBT TUI sessions:Remote Mode (pictured above):When connecting over an SSH (or Mosh) connection, "Remote Mode" is used to compensate for keyup events not being sent over remote terminal sessions. This functionally means that "Remote Mode" is a bit less responsive than "Direct Mode".Direct Mode:When running the NXBT TUI directly on the host computer, keyboard key presses are taken directly from any keyboard plugged in.Once you've successfully connected to a Nintendo Switch over the "Change Grip/Order Menu", you can reconnect quickly to the same Switch with the following command:sudonxbttui-rA couple other funcionality notes:Press 'q' to exit the TUI.In Direct Mode, press Escape to toggle input to the Nintendo Switch.NXBT looks for SSH and Mosh connections before deciding whether or note Remote Mode should be used. If you use another method for creating a remote terminal instance, NXBT likely won't detect it. Please open an issue if this happens to you!Running MacrosNXBT provides three ways to run macros on your Nintendo Switch:The NXBT Webapp (easiest)The CLIThe Python APIFor the first method, refer to the "Using the Webapp" section for more info.For info on writing macros, check out the documentationhere.Running Macros with the Command Line InterfaceTo run a simple, inline macro, you can use the following command:sudonxbtmacro-c"B 0.1s\n 0.1s"The above command will press the B button for 0.1 seconds and release all buttons for 0.1 seconds. The-cflag specifies the commands you would like to run. You'll need to be on the "Change Grip/Order Menu" for the above command to work. If you've already connected to the Switch on the host computer, you can reconnect and run the macro by adding the-ror--reconnectflag:sudonxbtmacro-c"B 0.1s\n 0.1s"-rSince it can be a little cumbersome typing out a large macro in the terminal, the macro command also supports reading from text files instead!commands.txt file:B 0.1s
0.1ssudonxbtmacro-c"commands.txt"-rIf you want more information on NXBT's CLI arguments:sudonxbt-hHere is my version help info$sudonxbtmacro-h-h,--helpshowthishelpmessageandexit-cCOMMANDS,--commandsCOMMANDSUsedinconjunctionwiththemacrocommand.Specifiesamacrostringorafilelocationtoloadamacrostringfrom.-r,--reconnectUsedinconjunctionwiththemacroortuicommand.Ifspecified,nxbtwillattmepttoreconnecttoanypreviouslyconnectedNintendoSwitch.-aADDRESS,--addressADDRESSUsedinconjunctionwiththemacroortuicommand.Ifspecified,nxbtwillattmepttoreconnecttoaspecificBluetoothMACaddressofaNintendoSwitch.-d,--debugEnablesdebugmodeinnxbt.-l,--logfileEnablesloggingtoafileinthecurrentworkingdirectoryinsteadofstderr.-iIP,--ipIPSpecifiestheIPtorunthewebappat.Defaultsto0.0.0.0-pPORT,--portPORTSpecifiestheporttorunthewebappat.Defaultsto8000--usesslEnablesordisablesSSLuseinthewebapp--certpathCERTPATHSpecifiesthefolderlocationforSSLcertificatesusedinthewebapp.Certificatesinthisfoldershouldbeintheformofa'cert.pem'and'key.pem'pair.--repeatREPEATSettingrepeattimesformacro.Defaultsto0,'-1'meansforever--commandinitCOMMANDINITUsedinconjunctionwiththemacrocommandinit.Specifiesamacrostringorafilelocationtoloadamacrostringfrom.Thiscommandwillnotrepeat,ucanuseforbackgametostartmacrorightlyUsing macro command to auto go back game and start macro loop:$sudonxbtmacro-c"macro-02.txt"--commandinit"macro-init.txt"--repeat-1
$catmacro-init
ZR0.1S0.3S
DPAD_UP0.2S
1S
PLUS0.2S0.3S
DPAD_LEFT0.1S0.1S
A0.1S0.1S
DPAD_DOWN0.1S0.1S
A0.1S0.1S
DPAD_RIGHT0.1S0.1S
A0.1S0.1S
A0.1S0.6S
PLUS0.05S0.08S
PLUS0.05S0.3S
DPAD_LEFT0.1S0.2S
A0.1S0.2S
DPAD_DOWN0.1S0.2S
A0.1S0.3S
B0.1S0.3S
L_STICK@+000+1000.1S0.3S
A0.1S0.1S
A0.1S0.1S
L_STICK@+000-1000.1S### Running Macros with the Python API

Macros are supported with the `macro` function in the Python API. All macros are expected as strings (multiline strings are accepted).

Minimal working example:

```python
import nxbt

macro = """
B 0.1s
0.1s
"""

# Start the NXBT service
nx = nxbt.Nxbt()

# Create a Pro Controller and wait for it to connect
controller_index = nx.create_controller(nxbt.PRO_CONTROLLER)
nx.wait_for_connection(controller_index)

# Run a macro on the Pro Controller
nx.macro(controller_index, macro)The above example uses a blocking macro call, however, multiple macros can be queued (or other actions taken) with the non-blocking syntax. Queued macros are processed in FIFO (First-In-First-Out) order.# Run a macro on the Pro Controller but don't block.# In this instance, we record the macro ID so we can keep track of its status later on.macro_id=nx.macro(controller_index,macro,block=False)fromtimeimportsleepwhilemacro_idnotinnx.state[controller_index]["finished_macros"]:print("Macro hasn't finished")sleep(1/10)print("Macro has finished")Using the APINXBT provides a Python API for use in Python applications or code.If you're someone that learns by example, check out thedemo.pyfile located at the root of this project.For a more in-depth look at all the functionality provided by the API, checkout thenxbt/nxbt.pyfile.For those looking to get started with a few simple examples: Read on!Creating a Controller and Waiting for it to Connectimportnxbt# Start the NXBT servicenx=nxbt.Nxbt()# Create a Pro Controller and wait for it to connectcontroller_index=nx.create_controller(nxbt.PRO_CONTROLLER)nx.wait_for_connection(controller_index)print("Connected")Pressing a Button# Press the B button# press_buttons defaults to pressing a button for 0.1s and releasing for 0.1snx.press_buttons(controller_idx,[nxbt.Buttons.B])# Pressing the B button for 1.0s instead of 0.1snx.press_buttons(controller_idx,[nxbt.Buttons.B],down=1.0)Tilting a Analog Stick# Tilt the right stick fully to the left.# tilt_stick defaults to tilting the stick for 0.1s and releasing for 0.1snx.tilt_stick(controller_idx,Sticks.RIGHT_STICK,-100,0)# Tilting the stick for 1.0s instead of 0.1snx.tilt_stick(controller_idx,Sticks.RIGHT_STICK,-100,0,tilted=1.0)Getting the available Bluetooth adapters# This prints the device paths for each available adapter.# If a controller is in use, an adapter will be removed from this list.print(nx.get_available_adapters)Shutting Down a running Controller# This frees up the adapter that was in use by this controllernx.remove_controller(controller_index)Reconnecting to a Switch# Get a list of all previously connected Switches and pass it as a reconnect_address argumentcontroller_index=nx.create_controller(nxbt.PRO_CONTROLLER,reconnect_address=nx.get_switch_addresses())Stopping or Clearing Macros# Stops/deletes a single macro from a specified controllernx.stop_macro(controller_index,macro_id)# Clears all macros from a given controllernx.clear_macros(controller_index)# Clears all macros from every created controllernx.clear_all_macros()TroubleshootingMy controller disconnects after exiting the "Change Grip/Order" MenuThis can occasionally occur due to timing sensitivites when transitioning off of the "Change Grip/Order" menu. To avoid disconnections when exiting this menu, please only press A (or B) a single time and wait until the menu has fully exited. If a disconnect still occurs, you should be able to reconnect your controller and use NXBT as normal."No Available Adapters"This means that NXBT wasn't able to find a suitable Bluetooth adapter to use for Nintendo Switch controller emulation. Only one controller can be emulated per adapter on the system, so if you've got one Bluetooth adapter available, you'll only be able to emulate one Nintendo Switch controller. The general causes (and solutions) to the above error follows:Cause:All available adapters are currently emulating a controller.Solution:End one of the other controller sessions (either through the webapp or command line) or plug in another Bluetooth adapter.Cause:No Bluetooth adapters are available to NXBT.Solution:Ensure that you've installed the relevant Bluetooth stack for your operating system (BlueZ on Linux) and check that your Bluetooh adapter is visible within to your OS."Address already in use"This means that another service has already bound itself to the Control and Interrupt ports on the specified Bluetooth adapter. Causes/solutions follow:Cause:(Linux specific solution) This is typically the BlueZ input plugin binding itself to the Control/Interrupt ports for your adapter.Solution:Either disable the input plugin (you will lose access to Bluetooth keyboards/mice while it is disabled) or install NXBT as root to allow for temporary toggling of the Input plugin.Future PlansAllows for rebinding keys within the TUI and webappAdd a touchscreen input option for the webapp to enable input on smartphonesTransition the webapp to a more maintainable React buildAllow for recording macros from direct input within the webappAllow for replaying recorded inputWrite a full testing suitePlans that Need More TestingUse mouse movement as right stick inputIssuesSwitching from the slow frequency mode on the "Change Grip/Order" menu to the full input report frequency is still a bit of a frail process. Some game start menus have a frequency of 15Hz but specifically only allow exiting by pressing the A button. The "Change Grip/Order" menu allows for exiting with A, B, or the Home button, however.The webapp can sometimes have small amounts of input lag (<8ms).CreditsA big thank you goes out to all the contributors at thedekuNukem/Nintendo_Switch_Reverse_Engineeringrepository! Almost all information pertaining to the innerworkings of the Nintendo Switch Controllers comes from the documentation in that repo. Without it, NXBT wouldn't have been possible.LicenseMIT