1. Download our Official Android App: Forums for Android!

Apps Android Bluetooth app doesn't list connected device

Discussion in 'Android Development' started by Arijit Banerjee, Mar 24, 2016.

  1. Arijit Banerjee

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 24, 2016

    Mar 24, 2016
    2
    0
    5
    Male
    First of all, I'd like to say that I'm new to Android programming as well this forum. Got stuck while developing an application, which is what brought me here. So if duplicates of my question already exist or my question doesn't conform to the standards here, I sincerely apologize.

    Now to the question:
    I'm trying to develop this simple app that scans for bluetooth devices nearby & connects to the user wishes to connect to.

    I have two JAVA files for that:
    1. SearchBTDevices.java - that takes care of the scanning / discovery
    2. ConnectedBTDevice.java - that connects to the device.

    This is my SearchBTDevice.java

    Code (Text):
    1.  
    2. package vertex2016.mvjce.edu.bluealert;
    3.  
    4. import android.bluetooth.BluetoothDevice;
    5. import android.bluetooth.BluetoothGattDescriptor;
    6. import android.content.BroadcastReceiver;
    7. import android.content.Context;
    8. import android.content.Intent;
    9. import android.content.IntentFilter;
    10. import android.content.pm.ActivityInfo;
    11. import android.os.Bundle;
    12. import android.bluetooth.BluetoothAdapter;
    13. import android.os.Parcelable;
    14. import android.provider.Settings;
    15. import android.support.design.widget.FloatingActionButton;
    16. import android.support.design.widget.Snackbar;
    17. import android.support.v7.app.AppCompatActivity;
    18. import android.support.v7.widget.Toolbar;
    19. import android.util.AttributeSet;
    20. import android.view.LayoutInflater;
    21. import android.view.View;
    22. import android.view.ViewGroup;
    23. import android.widget.AdapterView;
    24. import android.widget.ArrayAdapter;
    25. import android.widget.ListView;
    26. import android.widget.Toast;
    27.  
    28. import java.util.Set;
    29. import java.util.UUID;
    30. [INDENT]
    31. publicclassSearchBTDeviceextendsAppCompatActivity{
    32.  
    33.     publicBluetoothAdapterBlueAdapter=BluetoothAdapter.getDefaultAdapter();
    34.     publicArrayAdapterPairedArrayAdapter;
    35.     publicArrayAdapterBTArrayAdapter;
    36.     BluetoothDevice btd;
    37.     public UUID uuID;
    38.  
    39.     publicListView devicesFound;
    40.  
    41.  
    42.     privatefinalBroadcastReceiverBTReceiver=newBroadcastReceiver(){
    43.  
    44.        publicvoid onReceive(Context context,Intent intent)
    45.        {
    46.            String action = intent.getAction();
    47.  
    48.            if(BluetoothDevice.ACTION_FOUND.equals(action)){
    49.                    btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    50.                    uuID = intent.getParcelableExtra(BluetoothDevice.EXTRA_UUID);
    51.  
    52.                    BTArrayAdapter.add(btd.getName()+"\t"+ btd.getAddress()+"\n"+ uuID);
    53.  
    54.                }
    55.            }
    56.  
    57.     };
    58.  
    59.     IntentFilter filter1 =newIntentFilter(BluetoothDevice.ACTION_FOUND);
    60.  
    61.  
    62.  
    63.  
    64.     @Override
    65.     protectedvoid onResume(){
    66.         super.onResume();
    67.         BlueAdapter.cancelDiscovery();
    68.         BlueAdapter.startDiscovery();
    69.         BTArrayAdapter.clear();
    70.         this.registerReceiver(BTReceiver,filter1);
    71.  
    72.  
    73.  
    74.     }
    75.  
    76.     @Override
    77.     protectedvoid onPause(){
    78.         super.onPause();
    79.         BlueAdapter.cancelDiscovery();
    80.         this.unregisterReceiver(BTReceiver);
    81.         Toast.makeText(SearchBTDevice.this,"Discovery Stopped!!",Toast.LENGTH_SHORT).show();
    82.     }
    83.  
    84.  
    85.     @Override
    86.     protectedvoid onCreate(Bundle savedInstanceState){
    87.         super.onCreate(savedInstanceState);
    88.         setContentView(R.layout.activity_search_btdevice);
    89.         Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar);
    90.         setSupportActionBar(toolbar);
    91.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    92.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
    93.  
    94.  
    95.  
    96.  
    97.        searchBTDevices();
    98.  
    99.  
    100.     }
    101.  
    102.  
    103.     publicvoid searchBTDevices()
    104.     {
    105.         if(!BlueAdapter.startDiscovery())
    106.             Toast.makeText(SearchBTDevice.this,"Failed to Start Discovery",Toast.LENGTH_SHORT).show();
    107.         else
    108.             Toast.makeText(SearchBTDevice.this,"Discovery Startred",Toast.LENGTH_SHORT).show();
    109.         BTArrayAdapter=newArrayAdapter(this, android.R.layout.simple_list_item_1);
    110.         devicesFound =(ListView)findViewById(R.id.searchpagelistView);
    111.         devicesFound.setAdapter(BTArrayAdapter);
    112.         devicesFound.setOnItemClickListener(newAdapterView.OnItemClickListener(){
    113.             @Override
    114.             publicvoid onItemClick(AdapterView<?> parent,View view,int position,long id){
    115.                Intent connectedBTintent =newIntent(SearchBTDevice.this,ConnectedBTDevice.class);
    116.                 connectedBTintent.putExtra("BluetoothDevice", btd);
    117.                 startActivity(connectedBTintent);
    118.             }
    119.         });
    120. }}
    And here's my ConnectedBTDevice.java that should connect and show the name of the device

    Code (Text):
    1.  
    2. package vertex2016.mvjce.edu.bluealert;
    3.  
    4. import android.bluetooth.BluetoothAdapter;
    5. import android.bluetooth.BluetoothDevice;
    6. import android.bluetooth.BluetoothSocket;
    7. import android.content.pm.ActivityInfo;
    8. import android.os.Bundle;
    9. import android.support.design.widget.FloatingActionButton;
    10. import android.support.design.widget.Snackbar;
    11. import android.support.v7.app.AppCompatActivity;
    12. import android.support.v7.widget.Toolbar;
    13. import android.view.View;
    14. import android.widget.ArrayAdapter;
    15. import android.widget.ListView;
    16. import android.widget.Toast;
    17.  
    18. import java.io.IOException;
    19. import java.util.UUID;
    20.  
    21. public class ConnectedBTDevice extends AppCompatActivity {
    22.  
    23.     public BluetoothDevice btd;
    24.     public BluetoothSocket btSocket, tempSocket;
    25.     public UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") ;
    26.     ArrayAdapter arr;
    27.     ListView lv;
    28.  
    29.     @Override
    30.     protected void onCreate(Bundle savedInstanceState) {
    31.         super.onCreate(savedInstanceState);
    32.         setContentView(R.layout.activity_connected_btdevice);
    33.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    34.         setSupportActionBar(toolbar);
    35.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    36.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
    37.  
    38.  
    39.         arr  = new ArrayAdapter(this, android.R.layout.simple_list_item_2);
    40.  
    41.         btd = getIntent().getParcelableExtra("BluetoothDevice");
    42.  
    43.         connectBT();
    44.         displayStuff();
    45.  
    46.     }
    47.  
    48.     public void connectBT() {
    49.         Thread myThread = new Thread() {
    50.  
    51.             public void run() {
    52.                 tempSocket = null;
    53.                 btSocket = null;
    54.  
    55.                 try {
    56.                     tempSocket = btd.createRfcommSocketToServiceRecord(myUUID);
    57.                     System.out.println(btd.getName());
    58.  
    59.                     btSocket = tempSocket;
    60.  
    61.                 } catch (IOException e) {
    62.                     e.printStackTrace();
    63.                 }
    64.  
    65.                 BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
    66.  
    67.                 try {
    68.                     btSocket.connect();
    69.                     System.out.println("Connected65495334343");
    70.                     arr.add("CONNECTED TO-->" + btd.getName());
    71.  
    72.                 } catch (IOException e) {
    73.                     e.printStackTrace();
    74.                     try {
    75.                        btSocket.close();
    76.                     } catch (IOException e1) {
    77.                         e1.printStackTrace();
    78.                     }
    79.                 }
    80.  
    81.  
    82.             }
    83.  
    84.         };
    85.         myThread.start();
    86.     }
    87.  
    88.  
    89.  
    90.  
    91.     public void displayStuff()
    92.     {
    93.  
    94.         lv = (ListView)findViewById(R.id.connectedBTlistView);
    95.         System.out.println("HIBIJIBIHIBIJIBI");
    96.         lv.setAdapter(arr);
    97.     }
    98.  
    99. }
    100.  
    What I want:

    The device I'm selecting from the ListView of SearchBTDevice.java, I want its name to be displayed in the ListView of ConnectedBTDevice.java as well as in the logcat.

    I'm testing with three BT devices

    • Auro-PC
    • Droid-PC
    • AuroLava

    Problems are:

    1. The device name won't show up in the ListView of ConnectedBTDevice.java. I have used a System.out.println("Connected") after btSocket.connect() in ConnectedBTDevice.java to see if that part of the code executes, but it never shows up in the console.

    2. logcat doesn't display the correct name of the device being selected. In the 2nd case, I have used System.out.println(device.getName()); after tempSocket = btd.createRfcommSocketToServiceRecord(myUUID) in ConnectedBTDevice.java just to see if the correct device is being passed to ConnectedBTDevice.java from SearchBTDevice.java. It shows different names, like if I select Droid-PC, it sometimes shows Auro-PC & vice-versa.
    This is what my logcat throws -

    Code (Text):
    1.  
    2. 03-2413:18:04.06213091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@c46a6ad time:93528927
    3. 03-2413:18:06.37613091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_launch_request id:vertex2016.mvjce.edu.bluealert time:93531241
    4. 03-2413:18:06.73813091-19550/vertex2016.mvjce.edu.bluealert I/System.out: AURO-PC
    5. 03-2413:18:06.76513091-19550/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called withnoBluetoothManagerCallback
    6. 03-2413:18:07.13013091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@3f2b2e4a time:93531995
    7. 03-2413:18:07.13113091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@3f2b2e4a time:93531995
    8. 03-2413:18:07.56613091-13091/vertex2016.mvjce.edu.bluealert E/ActivityThread:Performing stop of activity that isnot resumed:{vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.SearchBTDevice}
    9.                                                                                 java.lang.RuntimeException:Performing stop of activity that isnot resumed:{vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.SearchBTDevice}
    10.                                                                                     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3377)
    11.                                                                                     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3458)
    12.                                                                                     at android.app.ActivityThread.access$1200(ActivityThread.java:154)
    13.                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
    14.                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
    15.                                                                                     at android.os.Looper.loop(Looper.java:135)
    16.                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5292)
    17.                                                                                     at java.lang.reflect.Method.invoke(NativeMethod)
    18.                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
    19.                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    20.                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
    21. 03-2413:18:12.31113091-19550/vertex2016.mvjce.edu.bluealert W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret:-1
    22. 03-2413:18:12.31113091-19550/vertex2016.mvjce.edu.bluealert W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
    23. 03-2413:18:12.31213091-19550/vertex2016.mvjce.edu.bluealert W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:584)
    24. 03-2413:18:12.31213091-19550/vertex2016.mvjce.edu.bluealert W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:321)
    25. 03-2413:18:12.31213091-19550/vertex2016.mvjce.edu.bluealert W/System.err:     at vertex2016.mvjce.edu.bluealert.ConnectedBTDevice$1.run(ConnectedBTDevice.java:67)
    26. 03-2413:18:19.74913091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@3f2b2e4a time:93544614
    27. 03-2413:18:20.92613091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@c13bdfa time:93545791
    28. 03-2413:18:28.32313091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@c13bdfa time:93553187
    29. 03-2413:18:29.30513091-13175/vertex2016.mvjce.edu.bluealert I/Adreno-EGL:<qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU (Iac7c2e2837)
    30.                                                                             OpenGL ES ShaderCompilerVersion: E031.25.03.04
    31.                                                                             BuildDate:06/11/15Thu
    32.                                                                             LocalBranch: mybranch10882158
    33.                                                                             RemoteBranch: quic/LA.BR.1.1.3_rb1.16
    34.                                                                             LocalPatches: NONE
    35.                                                                             ReconstructBranch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017+26a3cba+6f69ea6+8bc2bc8+649fcde+ a52cccf + dbf281f +15f0bf8+8d02f76+9b2cb1a+25f3b04+7cd8c84+ b54906e +675fd74+7c22ef4+  NOTHING
    36. 03-2413:18:29.30513091-13175/vertex2016.mvjce.edu.bluealert I/OpenGLRenderer:Initialized EGL, version 1.4
    37. 03-2413:18:29.38513091-13175/vertex2016.mvjce.edu.bluealert V/RenderScript:Application requested CPU execution
    38. 03-2413:18:29.38513091-13175/vertex2016.mvjce.edu.bluealert V/RenderScript:0x55898526a0Launching thread(s),CPUs8
    39. [\CODE]
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #16
    Points:
    1,488
    Posts:
    6,706
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,706
    9,422
    1,488
    Male
    Software developer
    South West of England

Share This Page

Loading...