0

I used Bluetooth API and following code is for on/off, show paired devices list, and connect paired devices. other functions work well, but connecting devices doesn't work . I try to use thread bidirectional, when bluetooth turns on, serverthread(acceptthread) generated and started. And when I choose device on the list, clientthread(connectthread) generated and started. This is my intent, but when I try to connect on the phone, app is turned off(almost at the client, occasionally at the server). I thought maybe it's UUID problem so I checked UUID 00enter code here001101-0000-1000-8000-00805F9B34FB, but it didn't help.enter code here sorry for the import code chunks, I would really appreciate your help.

'''
package com.practice.bluetoothmodule;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    TextView bthStatus;
    Button bthOn;
    Button bthOff;
    Button bthCon;

    BluetoothAdapter bthAdapter;
    Set<BluetoothDevice> pairedDevices;
    List<String> listofPairedDevices;
    private String btAddress=null;

    BluetoothDevice bthDevice;

    AcceptThread acceptThread;
    ConnectThread connectThread;
    private Handler mHandler = null;

    final static int BT_REQUEST_ENABLE=1;
    final static UUID BT_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bthStatus = (TextView)findViewById(R.id.text_check);
        bthOn = (Button)findViewById(R.id.bth_on);
        bthOff=(Button)findViewById(R.id.bth_off);
        bthCon=(Button)findViewById(R.id.dev_con);

        bthAdapter=BluetoothAdapter.getDefaultAdapter();

        bthOn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view){
                bluetoothOn();
            }
        });

        bthOff.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view){
                bluetoothOff();
            }
        });

        bthCon.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view){
                listPairedDevices();
            }
        });

    }

    @Override
    public void onStart(){
        super.onStart();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();

        if(acceptThread != null){
            acceptThread.cancel();
            acceptThread=null;
        }

        if(connectThread != null){
            connectThread.cancel();
            connectThread=null;
        }


    }

    void bluetoothOn(){
        if(bthAdapter == null){
            Toast.makeText(getApplicationContext(),"블루투스를 지원하지 않는 기기입니다",Toast.LENGTH_SHORT).show();
        }
        else{
            if(bthAdapter.isEnabled()){
                Toast.makeText(getApplicationContext(),"블루투스가 이미 활성화된 상태입니다",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getApplicationContext(),"블루투스가 활성화 되어 있지 않습니다",Toast.LENGTH_SHORT).show();
                Intent intentBthEnable = new Intent(bthAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(intentBthEnable,BT_REQUEST_ENABLE);
            }
        }
    }

    void bluetoothOff(){
        if(bthAdapter.isEnabled()){
            bthAdapter.disable();
            Toast.makeText(getApplicationContext(),"블루투스가 비활성화 되었습니다.",Toast.LENGTH_SHORT).show();
            bthStatus.setText("비활성화");
        }
        else{
            Toast.makeText(getApplicationContext(),"블루투스가 이미 비활성화 상태입니다",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        switch(requestCode){
            case BT_REQUEST_ENABLE:
                if(resultCode == RESULT_OK){
                    Toast.makeText(getApplicationContext(),"블루투스 활성화",Toast.LENGTH_LONG).show();
                    bthStatus.setText("활성화");
                    start();
                } else if(resultCode == RESULT_CANCELED){
                    Toast.makeText(getApplicationContext(),"취소",Toast.LENGTH_LONG).show();
                    bthStatus.setText("비활성화");
                }
                break;
        }
        super.onActivityResult(requestCode,resultCode,data);
    }

    void listPairedDevices(){
        if(bthAdapter.isEnabled()){
            pairedDevices = bthAdapter.getBondedDevices();

            if(pairedDevices.size()>0){
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("장치 선택");

                listofPairedDevices = new ArrayList();
                for(BluetoothDevice device: pairedDevices){
                    listofPairedDevices.add(device.getName());
                }
                final CharSequence[] items= listofPairedDevices.toArray(new CharSequence[listofPairedDevices.size()]);
                listofPairedDevices.toArray(new CharSequence[listofPairedDevices.size()]);

                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        connectSelectedDevice(items[item].toString());
                    }
                });
                AlertDialog alert=builder.create();
                alert.show();
            }
            else{
                Toast.makeText(getApplicationContext(),"페어링된 장치가 없습니다",Toast.LENGTH_LONG).show();
            }
        }
        else{
            Toast.makeText(getApplicationContext(),"블루투스가 비활성화 되어 있습니다.",Toast.LENGTH_LONG).show();
        }
    }

    void connectSelectedDevice(String selectedDeviceName){
        for(BluetoothDevice tempDevice: pairedDevices){
            if(selectedDeviceName.equals(tempDevice.getName())){
                bthDevice=tempDevice;
                btAddress=bthDevice.getAddress();
                break;
            }
        }
        connect(bthDevice);
        //Toast.makeText(getApplicationContext(),"연결 시도"+bthDevice.getName(),Toast.LENGTH_LONG).show();
    }

    public synchronized void start(){
        acceptThread = new AcceptThread();
        acceptThread.start();
    }

    public synchronized void connect(BluetoothDevice device){
        connectThread=new ConnectThread(device);
        connectThread.start();
    }

    private class AcceptThread extends Thread{
        private final BluetoothServerSocket mmServerSocket;

        public AcceptThread(){
            BluetoothServerSocket tmp=null;
            try{
                tmp=bthAdapter.listenUsingRfcommWithServiceRecord("Listen",BT_UUID);
                Toast.makeText(getApplicationContext(),"서버 열림",Toast.LENGTH_LONG).show();
            }catch(IOException e){
                Toast.makeText(getApplicationContext(),"서버 안열림",Toast.LENGTH_LONG).show();
            }
            mmServerSocket=tmp;
        }

        public void run(){
            BluetoothSocket socket=null;
            while(true){
                try{
                    socket=mmServerSocket.accept();
                } catch(IOException e){
                    break;
                }
                if(socket != null){
                    Toast.makeText(getApplicationContext(),"연결됨",Toast.LENGTH_SHORT).show();
                    try{
                        sleep(3000);
                    } catch (Exception e){}
                }
            }
        }

        public void cancel(){
            try{
                mmServerSocket.close();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
    }


    private class ConnectThread extends  Thread{
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device){
            BluetoothSocket tmp=null;
            mmDevice=device;
            try{
                tmp=device.createRfcommSocketToServiceRecord(BT_UUID);
                //Toast.makeText(getApplicationContext(),"클라 초기화 됨.",Toast.LENGTH_LONG).show();
            }catch (IOException e){
                //Toast.makeText(getApplicationContext(),"클라 초기화 실패.",Toast.LENGTH_LONG).show();
            }
            mmSocket=tmp;
            //Toast.makeText(getApplicationContext(),"클라 초기화",Toast.LENGTH_LONG).show();
        }

        public void run() {
            try {
                bthAdapter.cancelDiscovery();
                mmSocket.connect();
                Toast.makeText(getApplicationContext(), "클라이언트 연결", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "연결실패", Toast.LENGTH_LONG).show();
            }
        }

        public void cancel(){
            try{
                mmSocket.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}

'''

1 Answers1

0

With Android device, Enable BluetoothHCISnoop log to know , what is happening . It will give the sequential flow on what is happening with Bluetooth communication. https://www.androidcentral.com/all-about-your-phones-developer-options

The following reasons may be the cause: 1. In your code the device address may be wrong 2. The service UUID which you are trying to connect should be present in the service discovery database of remote bluetooth device 3. The app should wait for connect to happen How to programmatically tell if a Bluetooth device is connected?