3535import android .app .NotificationChannel ;
3636import android .app .NotificationManager ;
3737import android .app .PendingIntent ;
38+ import android .bluetooth .BluetoothAdapter ;
3839import android .bluetooth .BluetoothDevice ;
3940import android .bluetooth .BluetoothGatt ;
4041import android .bluetooth .BluetoothGattCallback ;
@@ -497,9 +498,22 @@ protected void onCreate(Bundle savedInstanceState) {
497498 defaultSharedPreferences = PreferenceManager .getDefaultSharedPreferences (this );
498499 headphoneWarning = defaultSharedPreferences .getBoolean ("headphone-warning" , true );
499500
501+ midiManager = (MidiManager )context .getSystemService (Context .MIDI_SERVICE );
502+ String mac = defaultSharedPreferences .getString ("last_bt" , null );
503+ if (mac != null ) {
504+ deviceToPair = BluetoothAdapter .getDefaultAdapter ().getRemoteDevice (mac );
505+ if (deviceToPair != null ) {
506+ midiManager .openBluetoothDevice (deviceToPair , new MidiManager .OnDeviceOpenedListener () {
507+ @ Override
508+ public void onDeviceOpened (MidiDevice device ) {
509+
510+ }
511+ }, null );
512+ }
513+ }
514+
500515 midiControls = new ArrayList <>();
501516 midiReciever = new MyReceiver (this );
502- midiManager = (MidiManager )context .getSystemService (Context .MIDI_SERVICE );
503517 MidiDeviceInfo [] midiDeviceInfos = midiManager .getDevices ();
504518 Log .d (TAG , String .format ("[midi] found devices: %d" , midiDeviceInfos .length ));
505519 for (MidiDeviceInfo midiDeviceInfo : midiDeviceInfos ) {
@@ -525,14 +539,20 @@ protected void onCreate(Bundle savedInstanceState) {
525539 midiOutputPort = device .openOutputPort (finalOutputPort );
526540 Log .d (TAG , String .format ("[midi] port opened: port %d" , midiOutputPort .getPortNumber ()));
527541 midiOutputPort .connect (midiReciever );
528- (findViewById (R .id .midi_icon )).setVisibility (VISIBLE );
529-
542+ mainActivity .runOnUiThread (new Runnable () {
543+ @ Override
544+ public void run () {
545+ if (deviceToPair == null )
546+ (findViewById (R .id .midi_icon )).setVisibility (VISIBLE );
547+ else
548+ (findViewById (R .id .bt_icon )).setVisibility (VISIBLE );
549+ }
550+ });
530551 }, null );
531552 }
532553
533554 }
534555
535-
536556 Log .d (TAG , "onCreate: " + String .format ("" +
537557 "%d: %d" , BuildConfig .VERSION_CODE , defaultSharedPreferences .getInt ("currentVersion" , 0 )));
538558// if (BuildConfig.VERSION_CODE > defaultSharedPreferences.getInt("currentVersion", 0)) {
@@ -2102,6 +2122,7 @@ public void run() {
21022122// });
21032123 Log .i (TAG , "onActivityResult: bluetooth device connected" );
21042124 // ... Continue interacting with the paired device.
2125+ defaultSharedPreferences .edit ().putString ("last_bt" , deviceToPair .getAddress ()).apply ();
21052126 midiManager .openBluetoothDevice (deviceToPair , new MidiManager .OnDeviceOpenedListener () {
21062127 @ Override
21072128 public void onDeviceOpened (MidiDevice device ) {
@@ -2196,6 +2217,11 @@ public void run() {
21962217 mainActivity .rack .toggleVideo .setChecked (false );
21972218// mainActivity.rack.toggleVideo.setChecked(true);
21982219 }
2220+
2221+ if (PERMISSION_REQUEST_CODE_BLUETOOTH == requestCode &&
2222+ grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
2223+ scanBLE ();
2224+ }
21992225 }
22002226
22012227 public static int getDominantColor (Bitmap bitmap ) {
0 commit comments