Arijit Banerjee
Lurker
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:
This is my SearchBTDevice.java
And here's my ConnectedBTDevice.java that should connect and show the name of the device
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
Problems are:
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:
- SearchBTDevices.java - that takes care of the scanning / discovery
- ConnectedBTDevice.java - that connects to the device.
This is my SearchBTDevice.java
Code:
package vertex2016.mvjce.edu.bluealert;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattDescriptor;
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.bluetooth.BluetoothAdapter;
import android.os.Parcelable;
import android.provider.Settings;
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.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.Set;
import java.util.UUID;
[INDENT]
publicclassSearchBTDeviceextendsAppCompatActivity{
publicBluetoothAdapterBlueAdapter=BluetoothAdapter.getDefaultAdapter();
publicArrayAdapterPairedArrayAdapter;
publicArrayAdapterBTArrayAdapter;
BluetoothDevice btd;
public UUID uuID;
publicListView devicesFound;
privatefinalBroadcastReceiverBTReceiver=newBroadcastReceiver(){
publicvoid onReceive(Context context,Intent intent)
{
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
uuID = intent.getParcelableExtra(BluetoothDevice.EXTRA_UUID);
BTArrayAdapter.add(btd.getName()+"\t"+ btd.getAddress()+"\n"+ uuID);
}
}
};
IntentFilter filter1 =newIntentFilter(BluetoothDevice.ACTION_FOUND);
@Override
protectedvoid onResume(){
super.onResume();
BlueAdapter.cancelDiscovery();
BlueAdapter.startDiscovery();
BTArrayAdapter.clear();
this.registerReceiver(BTReceiver,filter1);
}
@Override
protectedvoid onPause(){
super.onPause();
BlueAdapter.cancelDiscovery();
this.unregisterReceiver(BTReceiver);
Toast.makeText(SearchBTDevice.this,"Discovery Stopped!!",Toast.LENGTH_SHORT).show();
}
@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_btdevice);
Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
searchBTDevices();
}
publicvoid searchBTDevices()
{
if(!BlueAdapter.startDiscovery())
Toast.makeText(SearchBTDevice.this,"Failed to Start Discovery",Toast.LENGTH_SHORT).show();
else
Toast.makeText(SearchBTDevice.this,"Discovery Startred",Toast.LENGTH_SHORT).show();
BTArrayAdapter=newArrayAdapter(this, android.R.layout.simple_list_item_1);
devicesFound =(ListView)findViewById(R.id.searchpagelistView);
devicesFound.setAdapter(BTArrayAdapter);
devicesFound.setOnItemClickListener(newAdapterView.OnItemClickListener(){
@Override
publicvoid onItemClick(AdapterView<?> parent,View view,int position,long id){
Intent connectedBTintent =newIntent(SearchBTDevice.this,ConnectedBTDevice.class);
connectedBTintent.putExtra("BluetoothDevice", btd);
startActivity(connectedBTintent);
}
});
}}
And here's my ConnectedBTDevice.java that should connect and show the name of the device
Code:
package vertex2016.mvjce.edu.bluealert;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
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.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
public class ConnectedBTDevice extends AppCompatActivity {
public BluetoothDevice btd;
public BluetoothSocket btSocket, tempSocket;
public UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") ;
ArrayAdapter arr;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connected_btdevice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
arr = new ArrayAdapter(this, android.R.layout.simple_list_item_2);
btd = getIntent().getParcelableExtra("BluetoothDevice");
connectBT();
displayStuff();
}
public void connectBT() {
Thread myThread = new Thread() {
public void run() {
tempSocket = null;
btSocket = null;
try {
tempSocket = btd.createRfcommSocketToServiceRecord(myUUID);
System.out.println(btd.getName());
btSocket = tempSocket;
} catch (IOException e) {
e.printStackTrace();
}
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
try {
btSocket.connect();
System.out.println("Connected65495334343");
arr.add("CONNECTED TO-->" + btd.getName());
} catch (IOException e) {
e.printStackTrace();
try {
btSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
};
myThread.start();
}
public void displayStuff()
{
lv = (ListView)findViewById(R.id.connectedBTlistView);
System.out.println("HIBIJIBIHIBIJIBI");
lv.setAdapter(arr);
}
}
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:
- 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.
- 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.
Code:
03-2413:18:04.06213091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@c46a6ad time:93528927
03-2413:18:06.37613091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_launch_request id:vertex2016.mvjce.edu.bluealert time:93531241
03-2413:18:06.73813091-19550/vertex2016.mvjce.edu.bluealert I/System.out: AURO-PC
03-2413:18:06.76513091-19550/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called withnoBluetoothManagerCallback
03-2413:18:07.13013091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@3f2b2e4a time:93531995
03-2413:18:07.13113091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@3f2b2e4a time:93531995
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}
java.lang.RuntimeException:Performing stop of activity that isnot resumed:{vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.SearchBTDevice}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3377)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3458)
at android.app.ActivityThread.access$1200(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invoke(NativeMethod)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
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
03-2413:18:12.31113091-19550/vertex2016.mvjce.edu.bluealert W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
03-2413:18:12.31213091-19550/vertex2016.mvjce.edu.bluealert W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:584)
03-2413:18:12.31213091-19550/vertex2016.mvjce.edu.bluealert W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:321)
03-2413:18:12.31213091-19550/vertex2016.mvjce.edu.bluealert W/System.err: at vertex2016.mvjce.edu.bluealert.ConnectedBTDevice$1.run(ConnectedBTDevice.java:67)
03-2413:18:19.74913091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@3f2b2e4a time:93544614
03-2413:18:20.92613091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@c13bdfa time:93545791
03-2413:18:28.32313091-13091/vertex2016.mvjce.edu.bluealert I/Timeline:Timeline:Activity_idle id: android.os.BinderProxy@c13bdfa time:93553187
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)
OpenGL ES ShaderCompilerVersion: E031.25.03.04
BuildDate:06/11/15Thu
LocalBranch: mybranch10882158
RemoteBranch: quic/LA.BR.1.1.3_rb1.16
LocalPatches: NONE
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
03-2413:18:29.30513091-13175/vertex2016.mvjce.edu.bluealert I/OpenGLRenderer:Initialized EGL, version 1.4
03-2413:18:29.38513091-13175/vertex2016.mvjce.edu.bluealert V/RenderScript:Application requested CPU execution
03-2413:18:29.38513091-13175/vertex2016.mvjce.edu.bluealert V/RenderScript:0x55898526a0Launching thread(s),CPUs8
[\CODE]