Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Add a disconnect button so that we can simulate a peripheral disconne…
Browse files Browse the repository at this point in the history
…cting
  • Loading branch information
g-ortuno committed Dec 11, 2015
1 parent 120c9db commit ceadf8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Expand Up @@ -33,6 +33,9 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -212,6 +215,13 @@ protected void onCreate(Bundle savedInstanceState) {
.build();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_peripheral, menu);
return true /* show menu */;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down Expand Up @@ -255,6 +265,14 @@ protected void onStart() {
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_disconnect_devices) {
disconnectFromDevices();
return true /* event_consumed */;
}
return false /* event_consumed */;
}

@Override
protected void onStop() {
Expand Down Expand Up @@ -292,7 +310,7 @@ private void resetStatusViews() {

private void updateConnectedDevicesStatus() {
final String message = getString(R.string.status_devicesConnected) + " "
+ mBluetoothDevices.size();
+ mBluetoothManager.getConnectedDevices(BluetoothGattServer.GATT).size();
runOnUiThread(new Runnable() {
@Override
public void run() {
Expand All @@ -315,5 +333,12 @@ private void ensureBleFeaturesAvailable() {
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}

private void disconnectFromDevices() {
Log.d(TAG, "Disconnecting devices...");
for (BluetoothDevice device : mBluetoothManager.getConnectedDevices(
BluetoothGattServer.GATT)) {
Log.d(TAG, "Devices: " + device.getAddress() + " " + device.getName());
mGattServer.cancelConnection(device);
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/menu/menu_peripheral.xml
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_disconnect_devices"
android:showAsAction="always"
android:title="@string/menu_disconnect_devices"/>
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -54,4 +54,5 @@
<string name="status_noLeAdv">LE Advertising is not available. Please pair with a device to be
discoverable.
</string>
<string name="menu_disconnect_devices">Disconnect Devices</string>
</resources>

0 comments on commit ceadf8d

Please sign in to comment.