Skip to content

Commit

Permalink
Added activity for bt discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrosado committed May 19, 2017
1 parent d326db4 commit 9d74fa3
Show file tree
Hide file tree
Showing 10 changed files with 913 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Bluetooth-Communication/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".PairingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
Expand All @@ -20,14 +21,13 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private static final String TAG = "MainActivity";
public class PairingActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private static final String TAG = "PairingActivity";

BluetoothAdapter mBluetoothAdapter;
Button btnEnableDisable_Discoverable;
Expand Down Expand Up @@ -79,6 +79,44 @@ public void onReceive(Context context, Intent intent) {
}
};

/**
* Broadcast Receiver for changes made to bluetooth states such as:
* 1) Discoverability mode on/off or expire.
*/
private final BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();

if (action.equals(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)) {

int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.ERROR);

switch (mode) {
//Device is in Discoverable Mode
case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
Log.d(TAG, "mBroadcastReceiver2: Discoverability Enabled.");
break;
//Device not in discoverable mode
case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
Log.d(TAG, "mBroadcastReceiver2: Discoverability Disabled. Able to receive connections.");
break;
case BluetoothAdapter.SCAN_MODE_NONE:
Log.d(TAG, "mBroadcastReceiver2: Discoverability Disabled. Not able to receive connections.");
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.d(TAG, "mBroadcastReceiver2: Connecting....");
break;
case BluetoothAdapter.STATE_CONNECTED:
Log.d(TAG, "mBroadcastReceiver2: Connected.");
break;
}

}
}
};




Expand Down Expand Up @@ -147,7 +185,7 @@ protected void onDestroy() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_paring);

profiles.add(new Profile("clube", "sporting"));
profiles.add(new Profile("país", "portugal"));
Expand All @@ -171,7 +209,7 @@ protected void onCreate(Bundle savedInstanceState) {

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

lvNewDevices.setOnItemClickListener(MainActivity.this);
lvNewDevices.setOnItemClickListener(PairingActivity.this);


btnONOFF.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -262,6 +300,7 @@ public void btnEnableDisable_Discoverable(View view) {

}

@RequiresApi(api = Build.VERSION_CODES.M)
public void btnDiscover(View view) {
Log.d(TAG, "btnDiscover: Looking for unpaired devices.");

Expand Down Expand Up @@ -294,6 +333,7 @@ public void btnDiscover(View view) {
*
* NOTE: This will only execute on versions > LOLLIPOP because it is not needed otherwise.
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private void checkBTPermissions() {
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
Expand Down Expand Up @@ -326,7 +366,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mBTDevices.get(i).createBond();

mBTDevice = mBTDevices.get(i);
mBluetoothConnection = new BluetoothConnectionService(MainActivity.this);
mBluetoothConnection = new BluetoothConnectionService(PairingActivity.this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.bluetooth_communication.MainActivity">
tools:context="com.example.user.bluetooth_communication.PairingActivity">

<Button
android:text="ON/OFF"
Expand Down
6 changes: 6 additions & 0 deletions LocMessClient/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity android:name=".PairingActivity"
android:screenOrientation="portrait">

</activity>


<activity
android:name=".AddMessage"
android:screenOrientation="portrait" />
Expand Down
Loading

0 comments on commit 9d74fa3

Please sign in to comment.