Do dot reuse BluetoothDevice, remember BT address instead and lookup the corresponding BluetoothDevice every time

This fixes problems after sleep with bluez on Android.
This commit is contained in:
Andreas Shimokawa 2015-01-09 13:45:47 +01:00
parent dada70e92c
commit cc2b224a71

View File

@ -31,7 +31,7 @@ public class ControlCenter extends ActionBarActivity {
private static final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); private static final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
BluetoothAdapter mBtAdapter; BluetoothAdapter mBtAdapter;
BluetoothDevice mBtDevice; String mBtDeviceAddress = null;
BluetoothSocket mBtSocket; BluetoothSocket mBtSocket;
Button sendButton; Button sendButton;
Button testNotificationButton; Button testNotificationButton;
@ -61,7 +61,7 @@ public class ControlCenter extends ActionBarActivity {
for (BluetoothDevice device : pairedDevices) { for (BluetoothDevice device : pairedDevices) {
if (device.getName().indexOf("Pebble") == 0) { if (device.getName().indexOf("Pebble") == 0) {
// Matching device found // Matching device found
mBtDevice = device; mBtDeviceAddress = device.getAddress();
} }
} }
@ -72,13 +72,14 @@ public class ControlCenter extends ActionBarActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (!mBtAdapter.isEnabled() || mBtDevice == null) if (!mBtAdapter.isEnabled() || mBtDeviceAddress == null)
return; return;
String title = editTitle.getText().toString(); String title = editTitle.getText().toString();
String content = editContent.getText().toString(); String content = editContent.getText().toString();
try { try {
if (mBtSocket == null || !mBtSocket.isConnected()) { if (mBtSocket == null || !mBtSocket.isConnected()) {
mBtSocket = mBtDevice.createRfcommSocketToServiceRecord(SERIAL_UUID); BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(mBtDeviceAddress);
mBtSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID);
mBtSocket.connect(); mBtSocket.connect();
} }
ConnectedTask task = new ConnectedTask(); ConnectedTask task = new ConnectedTask();
@ -203,6 +204,7 @@ public class ControlCenter extends ActionBarActivity {
protected void onPostExecute(String result) { protected void onPostExecute(String result) {
Toast.makeText(ControlCenter.this, result, Toast.makeText(ControlCenter.this, result,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
try { try {
mBtSocket.close(); mBtSocket.close();
} catch (IOException e) { } catch (IOException e) {
@ -215,14 +217,15 @@ public class ControlCenter extends ActionBarActivity {
class NotificationReceiver extends BroadcastReceiver { class NotificationReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (!mBtAdapter.isEnabled() || mBtDevice == null) if (!mBtAdapter.isEnabled() || mBtDeviceAddress == null)
return; return;
String title = intent.getStringExtra("notification_title"); String title = intent.getStringExtra("notification_title");
String content = intent.getStringExtra("notification_content"); String content = intent.getStringExtra("notification_content");
try { try {
if (mBtSocket == null || !mBtSocket.isConnected()) { if (mBtSocket == null || !mBtSocket.isConnected()) {
mBtSocket = mBtDevice.createRfcommSocketToServiceRecord(SERIAL_UUID); BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(mBtDeviceAddress);
mBtSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID);
mBtSocket.connect(); mBtSocket.connect();
} }
ConnectedTask task = new ConnectedTask(); ConnectedTask task = new ConnectedTask();