106106 return 1 ;
107107 }
108108
109- OSXPAIR_DEBUG (" Switching Bluetooth %s...\n " , powered?" on" :" off" );
109+ OSXPAIR_DEBUG (" Switching Bluetooth %s..." , powered?" on" :" off" );
110110 IOBluetoothPreferenceSetControllerPowerState (powered);
111111
112112 // Wait a bit for Bluetooth to be (de-)activated
@@ -207,47 +207,57 @@ explicit MacOSVersion(int major=-1, int minor=-1) : major(major), minor(minor) {
207207bool
208208psmove_port_register_psmove (char *addr, char *host, enum PSMove_Model_Type model)
209209{
210- // TODO: Host is ignored for now
210+ char *tmp = psmove_port_get_host_bluetooth_address ();
211+ std::string mac_host_addr = tmp;
212+ free (tmp);
213+
214+ // We can have this check here, since macOS doesn't support multiple BT adapters
215+ // at the same time (connecting an external one might disable the internal one)
216+ if (!_psmove_btaddrs_equal (host, mac_host_addr.c_str ())) {
217+ OSXPAIR_DEBUG (" Not registering controller: Host address %s is not this machine (%s)" ,
218+ host, mac_host_addr.c_str ());
219+ return false ;
220+ }
211221
212222 // TODO: FIXME: If necessary, handle different controller models differently.
213223
214224 ScopedNSAutoreleasePool pool;
215225 std::string btaddr = _psmove_normalize_btaddr_inplace (addr, true , ' -' );
216226
217227 if (btaddr.length () == 0 ) {
218- OSXPAIR_DEBUG (" Not a valid Bluetooth address: %s\n " , addr);
228+ OSXPAIR_DEBUG (" Not a valid Bluetooth address: %s" , addr);
219229 return false ;
220230 }
221231
222232 auto macos_version = MacOSVersion::running ();
223233 if (!macos_version.valid ()) {
224- OSXPAIR_DEBUG (" Cannot detect macOS version.\n " );
234+ OSXPAIR_DEBUG (" Cannot detect macOS version." );
225235 return false ;
226236 } else if (macos_version >= MacOSVersion (13 , 0 )) {
227237 PSMOVE_WARNING (" Pairing not yet supported on macOS Ventura, see https://github.com/thp/psmoveapi/issues/457" );
228238 return false ;
229239 } else if (macos_version < MacOSVersion (10 , 7 )) {
230- OSXPAIR_DEBUG (" No need to add entry for macOS before 10.7.\n " );
240+ OSXPAIR_DEBUG (" No need to add entry for macOS before 10.7." );
231241 return false ;
232242 }
233243
234- OSXPAIR_DEBUG (" Detected: macOS %d.%d\n " , macos_version.major , macos_version.minor );
244+ OSXPAIR_DEBUG (" Detected: macOS %d.%d" , macos_version.major , macos_version.minor );
235245
236246 if (macosx_blued_is_paired (btaddr)) {
237- OSXPAIR_DEBUG (" Entry for %s already present.\n " , btaddr.c_str ());
247+ OSXPAIR_DEBUG (" Entry for %s already present." , btaddr.c_str ());
238248 return true ;
239249 }
240250
241251 if (macos_version < MacOSVersion (10 , 10 )) {
242252 if (!macosx_bluetooth_set_powered (0 )) {
243- OSXPAIR_DEBUG (" Cannot shutdown Bluetooth (shut it down manually).\n " );
253+ OSXPAIR_DEBUG (" Cannot shutdown Bluetooth (shut it down manually)." );
244254 }
245255
246- OSXPAIR_DEBUG (" Waiting for blued shutdown (takes ca. 42s) ...\n " );
256+ OSXPAIR_DEBUG (" Waiting for blued shutdown (takes ca. 42s) ..." );
247257 while (macosx_blued_running ()) {
248258 psmove_port_sleep_ms (1000 );
249259 }
250- OSXPAIR_DEBUG (" blued successfully shutdown.\n " );
260+ OSXPAIR_DEBUG (" blued successfully shutdown." );
251261 }
252262
253263 std::string command = format (" /usr/bin/defaults write %s HIDDevices -array-add %s" ,
@@ -258,7 +268,7 @@ explicit MacOSVersion(int major=-1, int minor=-1) : major(major), minor(minor) {
258268 command.c_str ());
259269 }
260270
261- OSXPAIR_DEBUG (" Running: '%s'\n " , command.c_str ());
271+ OSXPAIR_DEBUG (" Running: '%s'" , command.c_str ());
262272 bool result = true ;
263273 if (system (command.c_str ()) != 0 ) {
264274 OSXPAIR_DEBUG (" Could not run the command." );
0 commit comments