2016-07-13 3 views
0

현재 작업중인 앱을 통해 주변의 Bluetooth 장치에 연결하려고합니다.Android의 BluetoothSocket이 연결되지 않습니다.

다음 내 MainActivity.java이다 업데이트] bluetoothSocket.connect(); 전에

여기 무슨 일
package vertex2016.mvjce.edu.bluealert; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Gravity; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.*; 

import java.io.IOException; 
import java.util.UUID; 

import static java.lang.Thread.sleep; 

public class MainActivity extends AppCompatActivity { 

    //To get the default Bluetooth adapter on the Android device 
    public BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter(); 

    //A request code to identify which activity was executed 
    private int REQ_CODE = 1; 

    private int backPressedCount; 

    private boolean on = false; 

    //The Search button on the main screen 
    private Button searchButton; 

    //The View that lists all the nearby Bluetooth devices found 
    private ListView listBTDevices; 


    private UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 


    //Store the recently found Bluetooth devices & pass them on to the ListView 
    private ArrayAdapter BTArrayAdapter; 

    //A variable that points to the actual Bluetooth on the device 
    private BluetoothDevice btd; 
    private BluetoothSocket bSocket; 

    //Container for the TextViews 
    private ViewGroup containerVG; 

    //UUID to specify the services it can provide 


    //Intent Filter to detect the discovery of nearby Bluetooth devices 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Lock the rotation of the screen 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     searchButton = (Button) findViewById(R.id.searchButton); 
     listBTDevices = (ListView) findViewById(R.id.listBTDevices); 
     backPressedCount = 1; 


     containerVG = (ViewGroup) findViewById(R.id.containerVG); 



     searchButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (!on) { 
        connect(); 

       } else if (on) { 
        stopDiscovery(); 
        on = false; 
        searchButton.setText("Search"); 
       } 

      } 
     }); 

     listBTDevices.setOnItemClickListener(clickListener); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    //A method that checks if targeted device supports Bluetoth or not 
    //In case it does, execute the SearchBTDevices method to search 
    public void connect() { 
     //Registering the IntentFilter 
     this.registerReceiver(receiver, filter); 

     //If the device doesn't have Bluetooth, the Bluetooth Adapter BA returns NULL 
     if (BA == null) 
      Toast.makeText(MainActivity.this, "System Doesn't Support Bluetooth", Toast.LENGTH_SHORT).show(); 

      //In case the device has Bluetooth, but Bluetooth isn't enabled 
      //Enables the Bluetooth on the device 
      //startActivityForResult() takes in the Intent & a REQUEST CODE to specifically identify that intent 
     else if (!BA.isEnabled()) { 
      Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBT, REQ_CODE); 
     } 
     //In case Bluetooth is enabled on the device, start the discovery 
     else { 
      searchBTDevices(); 
     } 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode != RESULT_CANCELED) { 
      Toast.makeText(MainActivity.this, "TURNED ON!", Toast.LENGTH_SHORT).show(); 
      searchBTDevices(); 
     } else 
      Toast.makeText(MainActivity.this, "FAILED TO ENABLE BLUETOOTH", Toast.LENGTH_LONG).show(); 
    } 


    public void searchBTDevices() { 
     //As soon as the search starts, the Welcome screen TextView disappears & ListView appears 
     containerVG.setVisibility(View.GONE); 
     listBTDevices.setVisibility(View.VISIBLE); 


     BTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1); 


     //In case the discovery fails to begin 
     if (!BA.startDiscovery()) 
      Toast.makeText(MainActivity.this, "Failed to start discovery", Toast.LENGTH_SHORT).show(); 

     else { 
      Toast.makeText(MainActivity.this, "Discovery Started", Toast.LENGTH_SHORT).show(); 
      on = true; 
      searchButton.setText("Stop Discovery"); 

     } 

     listBTDevices.setAdapter(BTArrayAdapter); 


     //Setting the onItemClick for selecting a Bluetooth device to connect to 


    } 


    private final BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //Get the device details 

       BTArrayAdapter.add(btd.getName() + "\t\t" + btd.getAddress()); 
      } 

     } 
    }; 

    private void stopDiscovery() { 
     BA.cancelDiscovery(); 
     Toast.makeText(MainActivity.this, "Discovery Stopped", Toast.LENGTH_SHORT).show(); 
     this.unregisterReceiver(receiver); 
    } 

    @Override 
    protected void onStop() 
    { 
     super.onStop(); 
     BTArrayAdapter.clear(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     BA.cancelDiscovery(); 
     BA.startDiscovery(); 
    } 

    @Override 
    protected void onRestart() { 
     super.onRestart(); 
     Toast.makeText(MainActivity.this, "Discovery Resumed", Toast.LENGTH_SHORT).show(); 
    } 

    public final AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     { 
      /* 
      Intent connectedBT = new Intent(MainActivity.this, Connected.class); 
      connectedBT.putExtra("Bluetooth Device", btd); 
      startActivity(connectedBT); 
      */ 

      new ConnectingThread(btd).start(); 
     } 
    }; 

    @Override 
    public void onBackPressed() { 
     if (backPressedCount == 2) { 
      super.onBackPressed(); 
      return; 
     } 
     else { 
      backPressedCount++; 
      Toast.makeText(this,"Press BACK again to exit", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    public class ConnectingThread extends Thread{ 

     private final BluetoothDevice bluetoothDevice; 
     private final BluetoothSocket bluetoothSocket; 

     boolean ConnectionFlag = false; 


     public ConnectingThread(BluetoothDevice btd) 
     { 
      BluetoothSocket temp = null; 
      bluetoothDevice = btd; 

      try { 
       temp = btd.createInsecureRfcommSocketToServiceRecord(myUUID); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      bluetoothSocket = temp; 

     } 

     public void run() 
     { 
      BA.cancelDiscovery(); 

      try { 
       Toast.makeText(MainActivity.this,"REACHED BEFORE SOCKET CONNECTION", Toast.LENGTH_SHORT).show(); 
       System.out.println("#####THIS IS BEFORE SOCKET CONNECT#####"); 
       bluetoothSocket.connect(); 
       System.out.println("#####THIS IS AFTER SOCKET CONNECT#####"); 
       Toast.makeText(MainActivity.this,"REACHED AFTER SOCKET CONNECTION", Toast.LENGTH_SHORT).show(); 
       ConnectionFlag = true; 

      } catch (IOException e) { 

       try { 
        bluetoothSocket.close(); 
       } catch (IOException e1) { 
        e1.printStackTrace();} 
       return; 
      } 

      if(ConnectionFlag) 
       Toast.makeText(MainActivity.this, "Connected to" + bluetoothDevice.getName(),Toast.LENGTH_SHORT).show(); 
      else 
       Toast.makeText(MainActivity.this, "Failed to connect",Toast.LENGTH_SHORT).show(); 


     } 



    } 
} 

, 라인 System.out.println("#####THIS IS BEFORE SOCKET CONNECT#####"); 실행지고 있습니다.

하지만 그 후에는 System.out.println("#####THIS IS AFTER SOCKET CONNECT#####"); 라인이 실행되지 않습니다.

그리고 나는 나의 로그 캣에 다음과 같은 메시지를 받고 결국 :

07-13 17:47:33.482 32466-32466/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: [email protected] time:59429753 
07-13 17:48:16.276 32466-32466/vertex2016.mvjce.edu.bluealert I/System.out: #####THIS IS BEFORE SOCKET CONNECT##### 
07-13 17:48:16.276 32466-32466/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-13 17:48:22.789 32466-32466/vertex2016.mvjce.edu.bluealert I/Choreographer: Skipped 392 frames! The application may be doing too much work on its main thread. 
07-13 17:48:22.889 32466-32466/vertex2016.mvjce.edu.bluealert I/System.out: #####THIS IS BEFORE SOCKET CONNECT##### 
07-13 17:48:22.889 32466-32466/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-13 17:48:29.415 32466-32466/vertex2016.mvjce.edu.bluealert I/Choreographer: Skipped 392 frames! The application may be doing too much work on its main thread. 

업데이트 로그 캣 메시지

내가 무엇과 함께 일부

07-14 12:24:23.559 25623-25662/? E/bt-btif: DISCOVERY_COMP_EVT slot id:7, failed to find channle,status:1, scn:0

이 시간을 얻기 전에지고.

07-14 12:24:21.820 25368-25368/vertex2016.mvjce.edu.bluealert I/System.out: #####THIS IS BEFORE SOCKET CONNECT##### 
07-14 12:24:21.820 25368-25368/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-14 12:24:21.824 25623-25652/? D/BTIF_SOCK: service_uuid: 00001101-0000-1000-8000-00805f9b34fb 
07-14 12:24:21.824 25623-25662/? W/bt-l2cap: L2CA_ErtmConnectReq() PSM: 0x0001 BDA: b01041928d1e p_ertm_info: 0x00000000 allowed:0x0 preferred:0 
07-14 12:24:21.824 781-1154/? E/AudioTrack: AudioTrack::set : Exit 
07-14 12:24:21.836 295-923/? D/audio_hw_primary: start_output_stream: enter: stream(0xab02e8e8)usecase(1: low-latency-playback) devices(0x2) 
07-14 12:24:21.836 295-923/? D/audio_hw_primary: select_devices: out_snd_device(2: speaker) in_snd_device(0:) 
07-14 12:24:21.836 295-923/? W/msm8916_platform: Codec backend bitwidth 16, samplerate 48000 
07-14 12:24:21.836 295-923/? D/hardware_info: hw_info_append_hw_type : device_name = speaker 
07-14 12:24:21.842 295-923/? W/audio_hw_utils: audio_extn_utils_update_stream_app_type_cfg: App type could not be selected. Falling back to default 
07-14 12:24:21.842 295-923/? I/audio_hw_primary: select_devices Selected apptype: 69936 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_audio_cal, acdb_id = 15, path = 0 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_asm_topology 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> ACDB_CMD_GET_AUDPROC_STREAM_TOPOLOGY_ID 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_adm_topology 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> ACDB_CMD_GET_AUDPROC_COMMON_TOPOLOGY_ID 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_audtable 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> ACDB_CMD_GET_AUDPROC_COMMON_TABLE 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> AUDIO_SET_AUDPROC_CAL 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_audvoltable 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> ACDB_CMD_GET_AUDPROC_GAIN_DEP_STEP_TABLE 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> AUDIO_SET_AUDPROC_VOL_CAL 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_afe_cal 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> ACDB_CMD_GET_AFE_COMMON_TABLE 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> AUDIO_SET_AFE_CAL 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> send_hw_delay : acdb_id = 15 path = 0 
07-14 12:24:21.842 295-923/? D/ACDB-LOADER: ACDB -> ACDB_AVSYNC_INFO: ACDB_CMD_GET_DEVICE_PROPERTY 
07-14 12:24:21.843 295-923/? I/audio_hw_utils: audio_extn_utils_send_app_type_cfg app_type 69936, acdb_dev_id 15, sample_rate 48000 
07-14 12:24:21.844 295-923/? D/audio_hw_primary: select_devices: done 
07-14 12:24:22.758 25623-25662/? W/bt-btm: btm_acl_role_changed: BDA: b0-10-41-92-8d-1e 
07-14 12:24:22.758 25623-25662/? W/bt-btm: btm_acl_role_changed: New role: 0 
07-14 12:24:22.876 25623-25662/? W/bt-btm: btm_acl_created hci_handle=2 link_role=1 transport=1 
07-14 12:24:22.876 25623-25662/? W/bt-btm: btm_acl_created hci_handle=2 link_role=0 transport=1 
07-14 12:24:22.876 25623-25662/? W/bt-l2cap: L2CAP - st: CLOSED evt: 0 
07-14 12:24:22.876 25623-25662/? W/bt-l2cap: L2CAP - st: ORIG_W4_SEC_COMP evt: 7 
07-14 12:24:23.126 25623-25662/? W/bt-btm: btm_read_remote_version_complete: BDA: b0-10-41-92-8d-1e 
07-14 12:24:23.126 25623-25662/? W/bt-btm: btm_read_remote_version_complete lmp_version 7 manufacturer 69 lmp_subversion 1 
07-14 12:24:23.249 25623-25662/? W/bt-btm: btm_process_remote_ext_features_page 0: BDA: b0-10-41-92-8d-1e 
07-14 12:24:23.249 25623-25662/? W/bt-btm: ext_features_complt page_num:1 f[0]:x03, sm4:11, pend:0 
07-14 12:24:23.250 25623-25662/? W/bt-btm: btm_process_remote_ext_features_page 1: BDA: b0-10-41-92-8d-1e 
07-14 12:24:23.250 25623-25662/? W/bt-btif: info:x10 
07-14 12:24:23.250 25623-25662/? W/bt-l2cap: L2CA_SetDesireRole() new:x0, disallow_switch:0 

              [ 07-14 12:24:23.250 25623:25648 D/   ] 
              remote version info [b0:10:41:92:8d:1e]: 7, 45, 1 
07-14 12:24:23.267 781-930/? D/BluetoothManagerService: Message: 20 
07-14 12:24:23.267 781-930/? D/BluetoothManagerService: Added callback: [email protected]:true 
07-14 12:24:23.438 25623-25662/? W/bt-l2cap: L2CAP - st: W4_L2CAP_CON_RSP evt: 19 
07-14 12:24:23.493 25623-25662/? W/bt-l2cap: L2CAP - st: W4_L2CAP_CON_RSP evt: 11 
07-14 12:24:23.493 25623-25662/? W/bt-l2cap: L2CAP - st: CONFIG evt: 24 
07-14 12:24:23.494 25623-25662/? W/bt-l2cap: L2CAP - st: CONFIG evt: 14 
07-14 12:24:23.494 25623-25662/? W/bt-l2cap: L2CAP - st: CONFIG evt: 25 
07-14 12:24:23.494 25623-25662/? W/bt-l2cap: L2CAP-Upper layer Config_Rsp,Local CID: 0x0041,Remote CID: 0x0040,PSM: 1,our MTU present:1,our MTU:672 
07-14 12:24:23.499 25623-25662/? W/bt-l2cap: L2CAP - st: CONFIG evt: 15 
07-14 12:24:23.499 25623-25662/? W/bt-l2cap: L2CAP-peer_Config_Rsp,Local CID: 0x0041,Remote CID: 0x0040,PSM: 1,peer MTU present: 0,peer MTU: 672 
07-14 12:24:23.554 25623-25662/? W/bt-sdp: process_service_search_attr_rsp 
07-14 12:24:23.554 25623-25662/? W/bt-l2cap: L2CA_DisconnectReq() CID: 0x0041 
07-14 12:24:23.559 25623-25662/? W/bt-l2cap: L2CAP - st: W4_L2CAP_DISC_RSP evt: 18 
07-14 12:24:23.559 25623-25662/? E/bt-btif: DISCOVERY_COMP_EVT slot id:7, failed to find channle,          status:1, scn:0 
07-14 12:24:23.560 25623-25711/? W/bt-btif: invalid rfc slot id: 7 
07-14 12:24:23.560 25368-25368/vertex2016.mvjce.edu.bluealert I/Choreographer: Skipped 105 frames! The application may be doing too much work on its main thread. 
07-14 12:24:24.974 295-1069/? D/audio_hw_primary: out_standby: enter: stream (0xab02e8e8) usecase(1: low-latency-playback) 
07-14 12:24:25.075 295-1069/? D/hardware_info: hw_info_append_hw_type : device_name = speaker 
07-14 12:24:26.834 2506-2608/? I/GCoreUlr: Starting BLE scan: mode: 2 
07-14 12:24:26.884 25623-25662/? E/bt-btm: btm_sec_disconnected - Clearing Pending flag 
07-14 12:24:26.884 25623-25662/? W/bt-l2cap: L2CA_SetDesireRole() new:x0, disallow_switch:0 
07-14 12:24:26.887 25623-25623/? D/BluetoothMapService: onReceive 
07-14 12:24:26.887 25623-25623/? D/BluetoothMapService: onReceive, action android.bluetooth.device.action.ACL_DISCONNECTED 
07-14 12:24:26.893 2506-26096/? D/BluetoothAdapter: startLeScan(): null 

답변

0

스레드에서 run()을 실행하면 안됩니다. 이렇게하면 적절한 스레드가 생성되지 않고 대신 표준 클래스에서 run() 메서드를 실행하고있는 것입니다.

대신 run() -method가 호출되는 스레드를 만드는 start() -method를 호출해야합니다.

더 많은 정보를 원하시면 아래 링크를 참조하십시오 :

When would you call java's thread.run() instead of thread.start()?는 따라서 코드가 될 것이다 : 나는 그런 식으로 시도

new ConnectingThread(btd).start(); 
+0

, 작동하지 않았다. 이제 '채널을 찾지 못했습니다'라는 메시지가 나타납니다. 제발 내 코드를 좀 봐주세요. 업데이트했습니다. – Auro

+0

작동했는데, 이제 스레드가 성공적으로 실행 중입니다. '여전히 [차단 전화] (https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect())는 연결을 기다리고있다, 그러나 bluetoothSocket.connect()가'기억 확립/실패 메소드에 대한 개발자 문서를 확인하십시오.이 메소드는 메소드를 실행할 때 수행 할 작업에 대한 유용한 링크와 힌트를 제공합니다. – Mazze

+0

또한, 하드 코딩 된 UUID에 대해서도 확실합니까? 장치가 연결 피어를 시작할 수 있도록 내가 [링크] (https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#에서 UUID를 가지고 통신 – Mazze

관련 문제