简体   繁体   English

Android中的BluetoothSocket无法连接

[英]BluetoothSocket in Android doesn't connect

I'm trying to connect to nearby Bluetooth devices via an app I'm working on. 我正在尝试通过正在使用的应用程序连接到附近的蓝牙设备。

Following is my MainActivity.java [ UPDATED ] 以下是我的MainActivity.java [ 更新 ]

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();


        }



    }
}

What's happening here is, the line System.out.println("#####THIS IS BEFORE SOCKET CONNECT#####"); 这里发生的是行System.out.println("#####THIS IS BEFORE SOCKET CONNECT#####"); before bluetoothSocket.connect(); bluetoothSocket.connect();之前bluetoothSocket.connect(); is getting executed. 正在执行。

But the line System.out.println("#####THIS IS AFTER SOCKET CONNECT#####"); 但是行System.out.println("#####THIS IS AFTER SOCKET CONNECT#####"); after that is never getting executed. 之后永远不会执行。

And I end up getting the following message in my logcat: 我最终在logcat中收到以下消息:

07-13 17:47:33.482 32466-32466/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@19e8271a 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.

Updated logcat message 更新了logcat消息

Getting some 得到一些

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

this time along with what I was getting before. 这次和我以前得到的一样。

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: android.bluetooth.IBluetoothManagerCallback$Stub$Proxy@26f776c3: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

You should not execute the run() in Thread. 您不应在线程中执行run() This won't create a proper thread, instead you are just running the run() -method in a standard class. 这不会创建适当的线程,而是您仅在标准类中运行run()方法。

Instead you should call the start() -method "which creates a thread in which the run() -method is called". 相反,您应该调用start()方法“创建一个在其中调用run()方法的线程”。

See following link for more info: When would you call java's thread.run() instead of thread.start()? 有关更多信息,请参见以下链接: 什么时候调用Java的thread.run()而不是thread.start()?

Hence your code will be: 因此,您的代码将是:

new ConnectingThread(btd).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM