I have created one tab layout in main activity. It sets 3 tabs. In settings tab(which is extending ListFragment) I am handling click event on list. When 1st element is clicked, it creates BlueTooth object bt. Bluetooth extends activity.Inside that I am handling bluetooth functions.But bluetooth functions do not seem to be called. Pasting the code settings tab
//Our class extending fragment
public class settings extends ListFragment /*Fragment*/implements OnItemClickListener {
String[] title = {
"Register",
"Set Password",
"Edit Password",
"Delete Password",
"Add location to turn-off place list",
"Disable turn-off Places",
"Enable turn-off Places",
"Edit Distance",
"Factory Reset"
};
List<String> mStrings = new ArrayList<String>();
List<String> mDevices = new ArrayList<String>();
String[] strings = new String[mStrings.size()];
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
return inflater.inflate(R.layout.fragment_pager_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
for(int i=0;i<title.length;i++) {
mStrings.add(title[i]);
}
strings = mStrings.toArray(strings);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
mStrings));
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
switch(position)
{
case 0:
Intent intent = new Intent(getActivity().getApplicationContext(), BlueTooth.class);
startActivity(Intent intent);
mDevices.add("Dev1");
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
mDevices));
break;
.....
}
}
}
Bluetooth class:
public class BlueTooth extends Activity {
private BluetoothAdapter mBTA;
private SignBroadcastReceiver mReceiver;
boolean discoverFlag = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//register local BT adapter
mBTA = BluetoothAdapter.getDefaultAdapter();
//check to see if there is BT on the Android device at all
if (mBTA == null) {
int duration = Toast.LENGTH_SHORT;
Toast.makeText(this, "No Bluetooth on this handset", duration).show();
}
//let's make the user enable BT if it isn't already
/*if (!mBTA.isEnabled()) {
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, 0xDEADBEEF);
}*/
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivityForResult(discoverableIntent,0);
if(discoverFlag) {
//cancel any prior BT device discovery
if (mBTA.isDiscovering()) {
mBTA.cancelDiscovery();
}
//re-start discovery
mBTA.startDiscovery();
//let's make a broadcast receiver to register our things
mReceiver = new SignBroadcastReceiver();
IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, ifilter);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println(resultCode);
if (resultCode == RESULT_CANCELED) {
System.out.println("one");
Toast.makeText(this, "Item: " + RESULT_CANCELED, Toast.LENGTH_SHORT).show();
} else {
System.out.println("two");
Toast.makeText(this, "Item: " + -1, Toast.LENGTH_SHORT).show();
discoverFlag= true;
}
}
}
Atleast the discovery user interface should show.But it is not showing. I think I need to pass an intent while creating Bluetooth object.How to do that? I have added Activity class because I saw it in an example code. In normal class bluetooth functions are not getting implemented. It shows compiler errors.
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.securityalert">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name="BlueTooth"/>
</application>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
I fixed the code. Now it launches the intent. But I have another problem, launching activity creates a new UI which I do not want.I want to use the previous UI of ListFragment class. So, what should I extend the BlueTooth class from? using plain BlueTooth class gives compile errors at 1.this.registerReceiver(mReceiver, ifilter); and 2.startActivityForResult(discoverableIntent,0); 3.@Override public void onActivityResult(int requestCode, int resultCode, Intent intent)
User contributions licensed under CC BY-SA 3.0